1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Unity自学笔记——Day01_实现WASD键盘控制移动人物和镜头跟随

Unity自学笔记——Day01_实现WASD键盘控制移动人物和镜头跟随

时间:2023-11-06 10:48:56

相关推荐

Unity自学笔记——Day01_实现WASD键盘控制移动人物和镜头跟随

1.键盘移动控制:

using System.Collections;using System.Collections.Generic;using UnityEngine;public class PlayerMove2 : MonoBehaviour{// Start is called before the first frame updatepublic float speed = 3.5f;private CharacterController controller;void Start(){controller = transform.GetComponent<CharacterController>();}// Update is called once per framevoid Update(){var h = Input.GetAxis("Horizontal");var v = Input.GetAxis("Vertical");if(h!=0 || v!=0){Vector3 direction = new Vector3(h, 0, v);controller.Move(direction * speed * Time.deltaTime);transform.rotation = Quaternion.LookRotation(direction);}}}

2.镜头跟随:

using System.Collections;using System.Collections.Generic;using UnityEngine;public class CameraFollow : MonoBehaviour{// Start is called before the first frame updatepublic float speed = 3.5f;public Transform followtarget;public Vector3 offset;void Start(){offset = transform.position - followtarget.position;}// Update is called once per framevoid Update(){if(followtarget!=null){transform.position = Vector3.Lerp(transform.position, (followtarget.position + offset),speed*Time.deltaTime);}}}

也可以使用unity自带的一个package:cinemachine创建一个虚拟相机,设置相机参数和跟随目标即可

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。