How Unity rigid body controls the movement and rotation of objects

Insert picture description here
How to control the movement of the tank in this scene? At this time, we need to add a rigid body to the tank, and realize the movement effect of the tank through script control.
Insert picture description here
At the same time, we create a script to achieve the movement effect and place the script on the tank.

public class TankMovement : MonoBehaviour {
    
    
    
    //设置一个移动速度
    public int speed = 5;
    private Rigidbody rigidbody;
    //设置一个角速度  
    private int angularSpeed = 10;
	// Use this for initialization
	void Start () {
    
    
        rigidbody = GetComponent<Rigidbody>();
	}
	 
	// Update is called once per frame
	void Update () {
    
    
		
	}

    //固定帧调用,一般使用场景为物理移动 
    void FixedUpdate()
    {
    
    
        float h = Input.GetAxis("Horizontal");  
        //垂直方向W S 为前后移动
        float v = Input.GetAxis("Vertical"); 
        //刚体自身设置一个移动速度,transform.forward代表自身的前方向移动,
        rigidbody.velocity = transform.forward * v * speed;
        // 刚体自身设置一个角速度 
        rigidbody.angularVelocity = transform.up * h * angularSpeed;
    }
}

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_42708024/article/details/106582844