赞
踩
摄像机线性插值平滑跟随
车轮碰撞器
线性插值
using System.Collections; using System.Collections.Generic; using UnityEngine; public class FollowPlayer : MonoBehaviour { private Transform followTarget; // 跟随的目标对象 private Vector3 fixedDistance; // 相机与目标的固定距离 private Vector3 tempPos; // 相机的临时位置 void Start () { followTarget = GameObject.FindGameObjectWithTag("Player").transform; // 通过标签获取跟随的目标对象 fixedDistance = new Vector3(0, 3, -6); // 设置相机与目标的固定距离 } void FixedUpdate () { tempPos = followTarget.TransformDirection(fixedDistance) + followTarget.position; // 计算相机的临时位置 transform.position = Vector3.Lerp(transform.position, tempPos, Time.fixedDeltaTime * 3); // 平滑地将相机移动到临时位置 transform.LookAt(followTarget); // 让相机朝向目标对象 //Quaternion q = Quaternion.LookRotation(Vector3.forward,followTarget.position); //transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.fixedDeltaTime * 3); // 上面两行代码是注释掉的,不会被执行 } }
给四个轮胎都添加车轮碰撞器
using System.Collections.Generic; using UnityEngine; public class car : MonoBehaviour { private MeshRenderer[] wheelMesh; private WheelCollider[] wheel; private float maxAngle; private float maxTorque; void Start () { maxAngle = 30; maxTorque = 200; wheelMesh = transform.GetChild(0).GetComponentsInChildren<MeshRenderer>(); wheel = transform.GetChild(1).GetComponentsInChildren<WheelCollider>(); } void Update () { float h = Input.GetAxis("Horizontal"); float v = Input.GetAxis("Vertical"); for (int i = 0; i < 2; i++) { wheel[i].steerAngle = h * maxAngle; } foreach (var o in wheel) { o.motorTorque = maxTorque * v; } for (int i = 0; i < 4; i++) { wheelMesh[i].transform.localRotation = Quaternion.Euler(wheel[i].rpm * 360 / 60, wheel[i].steerAngle, 0); } } }
https://pan.baidu.com/share/init?surl=wveWbVg6BcEoNI1MgPugqw
sniu
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。