1.键盘移动控制:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove2 : MonoBehaviour
{
// Start is called before the first frame update
public float speed = 3.5f;
private CharacterController controller;
void Start()
{
controller = transform.GetComponent<CharacterController>();
}
// Update is called once per frame
void 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 update
public float speed = 3.5f;
public Transform followtarget;
public Vector3 offset;
void Start()
{
offset = transform.position - followtarget.position;
}
// Update is called once per frame
void Update()
{
if(followtarget!=null)
{
transform.position = Vector3.Lerp(transform.position, (followtarget.position + offset),speed*Time.deltaTime);
}
}
}
也可以使用unity自带的一个package:cinemachine创建一个虚拟相机,设置相机参数和跟随目标即可