物体移动类
using System.Collections; using System.Collections.Generic; using UnityEngine; /* SmallGame 时间:2017.8.12 作者:烧仙草奶茶 */ namespace SmallGame { public class move : MonoBehaviour { public float runspeed = 10; public float turnspeed = 100; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { float x = Input.GetAxis("Horizontal"); float y = Input.GetAxis("Vertical"); this.transform.position += this.transform.forward * Time.deltaTime * y * runspeed; this.transform.Rotate(0, x * Time.deltaTime* turnspeed, 0, Space.Self); } } }相机移动类
using System.Collections; using System.Collections.Generic; using UnityEngine; /* SmallGame 时间:2017.8.12 作者:烧仙草奶茶 */ namespace SmallGame { public class camear : MonoBehaviour { public GameObject car; public float turnSpeed; public float x=0; public float y=5; public float z=-10; private Vector3 offset; private float TurnY; private void Awake() { } void Start() { turnSpeed = car.transform.GetComponent<move>().turnspeed; this.transform.rotation = car.transform.rotation; this.transform.position = car.transform.position; offset = new Vector3(x,y,z);//角色与摄像机位置偏差值,自定义 } void Update() { } private void LateUpdate() { //第三人称跟随,随时保持角色后方跟随 TurnY = car.transform.eulerAngles.y;//实时获取玩家的Y轴旋转 //Debug.Log(_pointY);//0~360 Quaternion rotatiom = Quaternion.Euler(0, TurnY, 0); //将玩家Y轴旋转转换为四元数(代表一个三维旋转) //rotatiom * offset:四元数*一个向量代表=将该向量转四元数代表的角度, //即将该旋转应用到该向量,得到一个新的向量。体现为摄像机随着角色的旋转而左右旋转(但还没以角色为中心注视) transform.position = Vector3.Lerp(transform.position, car.transform.position + (rotatiom * offset), Time.deltaTime * turnSpeed); //让摄像机从自身位置注视玩家位置,功能实现完成。 transform.LookAt(car.transform.position); } } }