Unity——生命周期函数

事件函数执行顺序:

Unity——生命周期函数

 

 

1. 移动

Unity——生命周期函数
 1 public class Test : MonoBehaviour
 2 {
 3     public float speed;
 4 
 5     void Update()
 6     {
 7         float hor = Input.GetAxis("Horizontal");
 8         float ver = Input.GetAxis("Vertical");
 9         transform.Translate(new Vector3(hor, 0, ver) * Time.deltaTime * speed);
10 
11         //按世界坐标系移动
12         if (Input.GetKey(KeyCode.W))
13             transform.position += Vector3.forward * Time.deltaTime * speed;
14         if (Input.GetKey(KeyCode.S))
15             transform.position += Vector3.back * Time.deltaTime * speed;
16         if (Input.GetKey(KeyCode.A))
17             transform.position += Vector3.left * Time.deltaTime * speed;
18         if (Input.GetKey(KeyCode.D))
19             transform.position += Vector3.right * Time.deltaTime * speed;
20 
21 
22         if (Input.GetKey(KeyCode.W))
23             transform.Translate(Vector3.forward * Time.deltaTime * speed, Space.World);
24         if (Input.GetKey(KeyCode.S))
25             transform.Translate(Vector3.back * Time.deltaTime * speed, Space.World);
26         if (Input.GetKey(KeyCode.A))
27             transform.Translate(Vector3.left * Time.deltaTime * speed, Space.World);
28         if (Input.GetKey(KeyCode.D))
29             transform.Translate(Vector3.right * Time.deltaTime * speed, Space.World);
30 
31 
32         //按自身坐标系移动
33         if (Input.GetKey(KeyCode.W))
34             transform.position += transform.forward * Time.deltaTime * speed;
35         if (Input.GetKey(KeyCode.S))
36             transform.position += -transform.forward * Time.deltaTime * speed;
37         if (Input.GetKey(KeyCode.A))
38             transform.position += -transform.right * Time.deltaTime * speed;
39         if (Input.GetKey(KeyCode.D))
40             transform.position += transform.right * Time.deltaTime * speed;
41 
42         //默认Space.Self,按自身坐标系移动
43         if (Input.GetKey(KeyCode.W))
44             transform.Translate(Vector3.forward * Time.deltaTime * speed);
45         if (Input.GetKey(KeyCode.S))
46             transform.Translate(Vector3.back * Time.deltaTime * speed);
47         if (Input.GetKey(KeyCode.A))
48             transform.Translate(Vector3.left * Time.deltaTime * speed);
49         if (Input.GetKey(KeyCode.D))
50             transform.Translate(Vector3.right * Time.deltaTime * speed);
51     }
52 }
transform.Translate
Unity——生命周期函数
 1 public class Test : MonoBehaviour
 2 {
 3     public float force;
 4     Rigidbody rd;
 5     private void Start()
 6     {
 7         rd = GetComponent<Rigidbody>();
 8     }
 9 
10     void Update()
11     {
12         float hor = Input.GetAxis("Horizontal");
13         float ver = Input.GetAxis("Vertical");
14         Vector3 direction = new Vector3(hor, 0, ver);
15         rd.AddForce(direction * force);
16         rd.velocity = direction * force;
17         rd.MovePosition(transform.position + direction);
18     }
19 }
rigibody
Unity——生命周期函数
 1 public class Test : MonoBehaviour
 2 {
 3     public float speed;
 4     CharacterController cc;
 5     private void Start()
 6     {
 7         cc = GetComponent<CharacterController>();
 8     }
 9     void Update()
10     {
11         float hor = Input.GetAxis("Horizontal");
12         float ver = Input.GetAxis("Vertical");
13         cc.Move(new Vector3(hor, 0, ver) * Time.deltaTime * speed);   //不受重力影响,返回值是枚举值表示碰撞信息
14         cc.SimpleMove(new Vector3(hor, 0, ver) * Time.deltaTime * speed);  //受重力影响,返回值是布尔值表示角色是否着地
15     }
16 }
CharacterController

 

Unity——生命周期函数

上一篇:Java语言基础


下一篇:C语言中的无符号扩展和有符号扩展