Unity用代码写行走移动(第一人称)

利用代码写物体移动,首先我们要知道按键检测

一般按键检测if (Input.GetKey(KeyCode.按键值))

比如说我按W触发就是

if (Input.GetKey(KeyCode.W)

前进代码如下:

void Update()
    {
       
        if (Input.GetKey(KeyCode.W))
        {
            gameObject.transform.Translate(Vector3.forward * Time.deltaTime * moveSpeed);
        }
        if (Input.GetKey(KeyCode.S))
        {
            gameObject.transform.Translate(-Vector3.forward * Time.deltaTime * moveSpeed);
        }

        if (Input.GetKey(KeyCode.A))
        {
            gameObject.transform.Translate(-Vector3.right * Time.deltaTime * moveSpeed);
        }
        if (Input.GetKey(KeyCode.D))
        {
            gameObject.transform.Translate(Vector3.right * Time.deltaTime * moveSpeed);
        }
    }   

写完脚本拖到要移动的物体上面

不过在这之前,你的物体只能前后左右移动

写视觉控制代码

public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
    public RotationAxes axes = RotationAxes.MouseXAndY;
    public float sensitivityX = 15F;
    public float sensitivityY = 15F;
    public float minimumX = -360F;
    public float maximumX = 360F;
    public float minimumY = -90F;//水平
    public float maximumY = 90F;//水平
    float rotationY = 0F;

    void Start()
    {
		// Make the rigid body not change rotation
		if (GetComponent<Rigidbody>())
			GetComponent<Rigidbody>().freezeRotation = true;
	}

    void Update()
    {
		if (axes == RotationAxes.MouseXAndY)
		{
			float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;

			rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
			rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);

			transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
		}
		else if (axes == RotationAxes.MouseX)
		{
			transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
		}
		else
		{
			rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
			rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);

			transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
		}
        //分割线
		Cursor.visible = false;
		Cursor.lockState = CursorLockMode.Locked;
		//将鼠标隐藏并固定游戏中间
	}

将这些代码写到另一个脚本里面并拖动到摄像机里面

可是你还会发现,能看四周和移动,但是摄像机还是在原地,这里就要用到摄像机跟随了!

public Transform target;
    private Vector3 offset;
    void Start()
    {
        
        offset = target.position - this.transform.position;
    }
    void Update()
    {
        
        this.transform.position = target.position - offset;

    } 

同样写入另一个脚本拖到摄像机里面,会在摄像机有个目标选项,这个选项就是要跟随的目标

不过你还会发现,你摄像机朝着的地方按W还是向固定方向移动

这里就得用到,物体朝向跟随摄像机了

创建新脚本输入:

private GameObject _mainCamera;

    // Start is called before the first frame update
    private void Start()
    {
        _mainCamera = GameObject.FindWithTag("MainCamera");
    }

    // Update is called once per frame
    private void Update()
    {
        //物体始终朝着摄像机所面对方向
        var rotation = Quaternion.LookRotation(_mainCamera.transform.TransformVector(Vector3.forward),
            _mainCamera.transform.TransformVector(Vector3.up));
        rotation = new Quaternion(0, rotation.y, 0, rotation.w);
        gameObject.transform.rotation = rotation;
    }

 然后将这个脚本拖动到移动的物体上面,在移动物体加上rigidbody组建,脚下创建立方体,缩放大小自定义,我设置10,1,10

运行, 就可以移动了

切记,该文章为有一点脚本基础的人直接复制粘贴食用,初学unity可能有的人和我初学会一样犯错在网上搜索:unity脚本写完进入游戏没效果

那是因为你没有将脚本拖动到物体属性界面里面,而写this(这个物体).xxx又不知道你要执行脚本的是哪个物体,所以会报错或没效果  !

猜你喜欢

转载自blog.csdn.net/Luncher_Message/article/details/127588913
今日推荐