最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

In unity, how do you ease a cameras rotation to the players in C#? - Stack Overflow

programmeradmin6浏览0评论

I'm bad at math and I'm looking to make a cameras rotation ease into the players, in this case a car so that it feels more noticeable when it drifts.

The concept code would look something like this:

car.rotation = this.rotation / 15

sadly this isn't how the language works and quaternions are too complicated for me to understand.

If anyone could share any methods or code with me to do what I'm trying to do please tell.

I'm bad at math and I'm looking to make a cameras rotation ease into the players, in this case a car so that it feels more noticeable when it drifts.

The concept code would look something like this:

car.rotation = this.rotation / 15

sadly this isn't how the language works and quaternions are too complicated for me to understand.

If anyone could share any methods or code with me to do what I'm trying to do please tell.

Share Improve this question asked Mar 20 at 16:35 paperboymanpaperboyman 253 bronze badges 1
  • Quaternions? You just need some mathematic function. Lerp is commonly used to change value over time creating animation, see e.g. this unity question or here is some implementation. – Sinatr Commented Mar 20 at 16:43
Add a comment  | 

2 Answers 2

Reset to default 1

For a smoother camera you probably want to use SmoothDampAngle as Lerp can be to rough and doesn't account for car velocity.

First you need to get the rotation.

  1. To get the current rotation of the camera on the Y axis use: transform.eulerAngles.y
  2. To get the car's rotation on the Y axis you can use: car.eulerAngles.y

Second you need to interpolate the transition.

  1. To get the camera's current Y rotation use: currentY

  2. To get the car's Y rotation use: targetY

  3. To store the angular velocity use: velocity (it's an internal variable)

  4. To smooth the time use: smoothTime

Complete function: float newY = Mathf.SmoothDampAngle(currentY, targetY, ref velocity, smoothTime);

Third you do the transition

  1. To convert the smooth transition into a Quaternion(what Unity uses for rotation): Quaternion.Euler(0, newY, 0)

  2. To apply the rotation: transform.rotation = Quaternion.Euler(0, newY, 0);

Final code

using UnityEngine;

public class CameraFollow : MonoBehaviour
{
    public Transform car; // Reference to the car's Transform.
    public float smoothTime = 0.3f; // The time it takes to reach the target rotation smoothly.
    private float velocity = 0.0f; // Stores the current angular velocity (needed for SmoothDampAngle).

    void Update()
    {
        if (car == null) return; // Safety check to avoid errors.

        // Get the current Y-axis rotation of the camera
        float currentY = transform.eulerAngles.y;

        // Get the target Y-axis rotation of the car
        float targetY = car.eulerAngles.y;

        // Smoothly interpolate the rotation angle using SmoothDampAngle
        float newY = Mathf.SmoothDampAngle(currentY, targetY, ref velocity, smoothTime);

        // Apply the new rotation to the camera
        transform.rotation = Quaternion.Euler(0, newY, 0);
    }
}

Adjusting Smoothness

  • Increase smoothTime (e.g., 0.5f) → Slower, smoother rotation.

  • Decrease smoothTime (e.g., 0.1f) → Faster response, but less smooth.

I hope this helps :D

You might consider using the Cinemachine package. Without performing complicated computations and using its components' features you may achieve close results.

发布评论

评论列表(0)

  1. 暂无评论