我一直在尝试在Unity 5中为汽车编码,该汽车可以像普通汽车一样行驶,但摄像头绕着它行驶。我可以进行此操作,但汽车转弯时相机无法保持其在轨道上的位置。例子:我看着车后,然后右转。我现在将看汽车的右侧。有什么方法可以使相机相对于汽车保持在同一位置。
我从本教程中学到了汽车的运动,但是我不喜欢其中的摄像头运动,我希望摄像头绕汽车行驶。我跟随这个镜头进行相机运动。但是,我正在尝试对其进行修改,以使相机随汽车一起转动,并且不会重置到汽车后部
CameraManager.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraManager : MonoBehaviour
{
public InputManager im;
public bool lockCursor;
public float distance = 5f;
public float mouseSensitivity = 10f;
public Transform target;
public Vector2 pitchMinMax = new Vector2 (-40, 85);
public float rotationSmoothTime = 0.12f;
Vector3 rotationSmoothVelocity;
Vector3 currentRotation;
float yaw;
float pitch;
void Start()
{
im = GetComponent<InputManager>();
if (lockCursor)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
// Update is called once per frame
void LateUpdate()
{
yaw += (Input.GetAxis("Mouse X")) * mouseSensitivity;
pitch -= Input.GetAxis("Mouse Y") * mouseSensitivity;
pitch = Mathf.Clamp(pitch, pitchMinMax.x, pitchMinMax.y);
currentRotation = Vector3.SmoothDamp(currentRotation, new Vector3(pitch, yaw), ref rotationSmoothVelocity, rotationSmoothTime);
transform.eulerAngles = currentRotation;
print(target.transform.forward);
transform.position = target.position - transform.forward * distance;
}
}
InputManager.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InputManager : MonoBehaviour
{
public float throttle;
public float steer;
// Update is called once per frame
void Update()
{
throttle = Input.GetAxis("Vertical");
steer = Input.GetAxis("Horizontal");
print(steer);
}
}
CarController.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(InputManager))]
[RequireComponent(typeof(Rigidbody))]
public class CarController : MonoBehaviour
{
public InputManager im;
public List<WheelCollider> throttleWheels;
public List<WheelCollider> steeringWheels;
public float strengthCo = 10000f; //Strength Coefficent
public float maxTurn = 20f;
public Transform CM;
public Rigidbody rb;
// Start is called before the first frame update
void Start()
{
im = GetComponent<InputManager>();
rb = GetComponent<Rigidbody>();
if (CM)
{
rb.centerOfMass = CM.position;
}
}
// Update is called once per frame
void FixedUpdate()
{
foreach (WheelCollider wheel in throttleWheels)
{
wheel.motorTorque = strengthCo * Time.deltaTime * im.throttle;
}
foreach (WheelCollider wheel in steeringWheels)
{
wheel.steerAngle = maxTurn * im.steer;
}
}
}
答案 0 :(得分:0)
您可以将Quaternion.Euler(currentRotation)
用作相对旋转值以应用于target.rotation
,并与target.rotation * Quaternion.Euler(currentRotation)
一起应用:
void LateUpdate()
{
yaw += (Input.GetAxis("Mouse X")) * mouseSensitivity;
pitch -= Input.GetAxis("Mouse Y") * mouseSensitivity;
pitch = Mathf.Clamp(pitch, pitchMinMax.x, pitchMinMax.y);
currentRotation = Vector3.SmoothDamp(currentRotation, new Vector3(pitch, yaw), ref rotationSmoothVelocity, rotationSmoothTime);
transform.rotation = target.rotation * Quaternion.Euler(currentRotation);
print(target.transform.forward);
transform.position = target.position - transform.forward * distance;
}