Unity controls the movement of the ball through the keyboard keys

First, we create a new scene.
Insert picture description here
The effect we need to achieve is to move the ball, and the movement operation needs to be implemented with the help of scripts.
Insert picture description here
We create a Move script and attach it to the ball at the same time. At the same time, the movement operation needs to be realized with the aid of a rigid body component, so we also need to create a rigid body component and attach the rigid body to the ball.
Insert picture description here
After implementing the above steps, we begin to write the script.
Since moving the ball requires the use of rigid body components, our first step is to obtain the rigid body components and assign values ​​to the rigid body components. Finally, we only need to apply a force to the rigid body components.

public class Move : MonoBehaviour {
    
    

    //定义一个刚体对象
    private Rigidbody rb;
    
	void Start () {
    
    
        //接收刚体组件
        rb = GetComponent<Rigidbody>();
	}

	void Update () {
    
    
        //添加一个力,Vector3为一个三维向量,参数分别为x,y,z,当x = 1时,表示当前x方向发生偏移
        rb.AddForce(new Vector3(1, 0, 0));
	}
}

After we ran it, we found that it can only move in a fixed direction. Is it possible to control the movement according to the buttons?
We need to introduce Input in Update for judgment.

void Update () {
    
    
        //Input.GetAxis用于检测是否按下键盘按键
        //Horizontal表示水平按键
        float h = Input.GetAxis("Horizontal");
        //Vertical表示垂直按键
        float v = Input.GetAxis("Vertical");
        //添加一个力
        rb.AddForce(new Vector3(h, 0, v));
	}

But we found that the ball moves very slowly. We only need to define one parameter.

public class Move : MonoBehaviour {
    
    

    //定义一个刚体对象
    private Rigidbody rb;
    //定义小球移动的速度
    public int speed = 5;

    // Use this for initialization
	void Start () {
    
    
        //接收刚体组件
        rb = GetComponent<Rigidbody>();
	}
	
	    
	void Update () {
    
    
        //Input.GetAxis用于检测是否按下键盘按键
        //Horizontal表示水平按键
        float h = Input.GetAxis("Horizontal");
        //Vertical表示垂直按键
        float v = Input.GetAxis("Vertical");
        //添加一个力
        rb.AddForce(new Vector3(1, 0, 0) * speed);
	}
}

Why do we need to set speed as a common variable?
Insert picture description here
In the later stage, we only need to set the speed of the ball movement here, no need to set it in the script.

We have controlled the movement of the ball, but the ball will disappear and then be within the camera's field of view. If you need the ball to always be in the field of view, you need to set the camera's movement through a script. (That is, the relative distance between the camera and the ball remains unchanged)
We create a FollowMove script and apply for a transform component at the same time.
Insert picture description here
Insert picture description here
Assign the script to the main camera. Since our transform component is set to be shared, we can assign it externally, and we assign the ball to it.
Insert picture description here

public class FollowMove : MonoBehaviour {
    
    

    public Transform SphereTransform;
    //设置一个距离的三维变量,用于保存摄像机于小球之间的距离。
    private Vector3 distance;
	// Use this for initialization
	void Start () {
    
    
        //由于当前脚本添加到主摄像机上,所以transform.position表示主摄像机的位置
        //由于我们给SphereTransform赋值为小球,所以SphereTransform.position为小球的位置
        distance = transform.position - SphereTransform.position;
	}
	
	// Update is called once per frame
	void Update () {
    
    
        //主摄像机的位置为每次小球移动的位置+之间的距离
        transform.position = SphereTransform.position + distance;
	}
}

Guess you like

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