版权声明:本文为博主原创文章,转载请注明源地址 https://blog.csdn.net/qq_15505341/article/details/79247353
添加物体
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.AddComponent<Rigidbody>();
cube.transform.position = new Vector3(x, y, z);
添加预制体
public Transform brick;
// Use this for initialization
void Start()
{
Instantiate(brick, new Vector3(x, y, z), Quaternion.identity);
}
给物体施加一个力
Rigidbody rigidbody;
// Use this for initialization
void Start () {
rigidbody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rigidbody.AddForce(movement * 10f);
}
移动物体
// Update is called once per frame
void Update () {
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
this.transform.Translate(movement * 0.04F, Space.Self);
//transform.Translate(Vector3.forward * 0.04F,Space.Self);
}
旋转物体
// Update is called once per frame
void Update () {
transform.Rotate(new Vector3(15, 30, 45) * Time.deltaTime);
//Time.deltaTime为上一帧到这一帧的时间
//乘上Time.deltaTime程序从每帧变化变成每秒变化
}
关于deltaTime的解释
deltaTime是你这一帧到上一帧经历的时间。
假设一秒走10帧,每帧不太稳定,间隔时间不一样,用 t0,t1,t2…t9来表示,他们们满足 t0 + t1 + t2 + … + t9 = 1
现在你定义了一个速度 V=5,意思是想要1秒走5米,现在设每帧所走的路程是 s0,s1,s2,…s9
满足 s0=V*t0,s1=V*t1,s2=V*t2,…s9=V*t9
那么看看1秒走了多少路程:
S = s0+s1+s2+…s9
= V*(t0+t1+t2+…+t9)
=V*1
=V
=5
引用自 百度贴吧- Cairne愷恩(本贴3L)
相机跟随
public GameObject player;
private Vector3 offset;
// Use this for initialization
void Start () {
offset = transform.position - player.transform.position;
}
// Update is called once per frame
void Update () {
transform.position = player.transform.position + offset;
}
禁用物体
this.gameObject.SetActive(false);