Friday, August 8, 2025

unity – Why does this code behave surprisingly with totally different digital camera angles?

Related to this situation are two MonoBehaviour scripts: Actor and Participant. Actor handles velocity, motion, issues like that. Participant makes use of inputs to manage an Actor part. That is the code (notice that performance unrelated to motion has been eliminated for readability):

// Actor.cs
utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;

(RequireComponent(typeof(CharacterController)))
public class Actor : MonoBehaviour
{
    public Vector3 Path
    {
        get
        {
            return _direction;
        }
        set
        {
            _direction = worth.normalized;
        }
    }
    public Vector3 Velocity
    {
        get
        {
            return _velocity;
        }
        set
        {
            _velocity = worth;
        }
    }

    public float acceleration = 48;
    public float deceleration = 56;
    public float skidAcceleration = 72;
    public float topSpeed = 5;

    personal CharacterController controller;

    personal Vector3 _direction;
    personal Vector3 _velocity;

    // Begin is known as earlier than the primary body replace
    void Begin()
    {
        controller = gameObject.GetComponent();
    }

    // Replace is known as as soon as per body
    void Replace()
    {
        if (controller.isGrounded)
        {
            Vector3 planarTarget = new Vector3(_direction.x, 0, _direction.z);
            if (planarTarget.sqrMagnitude > 0.01f)
            {
                Quaternion targetRot = Quaternion.LookRotation(planarTarget);
                rework.rotation = Quaternion.RotateTowards(rework.rotation, targetRot, 600.0f * Time.deltaTime);
            }
        }

        Speed up(ref _velocity.x, ref _direction.x);
        Speed up(ref _velocity.z, ref _direction.z);

        Decelerate(ref _velocity.x, ref _direction.x);
        Decelerate(ref _velocity.z, ref _direction.z);

        controller.Transfer(_velocity * Time.deltaTime);
    }

    void Speed up(ref float velComponent, ref float dirComponent)
    {
        bool transferring = Mathf.Abs(dirComponent) > 0.0f;
        if (transferring)
        {
            float absMotion = Mathf.Abs(velComponent);

            if ((velComponent > 0 && dirComponent < 0) ||
                (velComponent < 0 && dirComponent > 0))
            {
                velComponent += dirComponent * skidAcceleration * Time.deltaTime;
            }
            else if (absMotion < topSpeed)
            {
                velComponent += dirComponent * acceleration * Time.deltaTime;
            }
            else if (absMotion > topSpeed)
            {
                velComponent -= dirComponent * deceleration * Time.deltaTime;
            }
        }
    }

    void Decelerate(ref float velComponent, ref float dirComponent)
    {
        bool transferring = Mathf.Abs(dirComponent) > 0.0f;
        if (!transferring)
        {
            if (velComponent < 0)
            {
                velComponent += deceleration * Time.deltaTime;
                if (velComponent > 0)
                    velComponent = 0;
            }
            else if (velComponent > 0)
            {
                velComponent -= deceleration * Time.deltaTime;
                if (velComponent < 0)
                    velComponent = 0;
            }
        }    
    }
}

// Participant.cs
utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;
utilizing UnityEngine.InputSystem;

public class Participant : MonoBehaviour
{
    public Remodel camTransform;

    personal Actor actor;

    personal InputAction moveAction;
    personal InputAction jumpAction;
    personal InputAction crouchAction;

    // Begin is known as earlier than the primary body replace
    void Begin()
    {
        actor = gameObject.GetComponent();

        moveAction = InputSystem.actions.FindAction("transfer");
        jumpAction = InputSystem.actions.FindAction("soar");
        crouchAction = InputSystem.actions.FindAction("crouch");
    }

    // Replace is known as as soon as per body
    void Replace()
    {
        float oldCamPitch = camTransform.eulerAngles.x;
        float oldCamRoll = camTransform.eulerAngles.z;

        Vector2 moveInput = moveAction.ReadValue();

        Vector3 camForward = camTransform.ahead;
        Vector3 camRight = camTransform.proper;

        camForward.y = 0;
        camRight.y = 0;

        camForward.Normalize();
        camRight.Normalize();

        Vector3 moveDir = camForward * moveInput.y + camRight * moveInput.x;
        actor.Path = moveDir.normalized;
    }
}

The problem I am noticing is that when the digital camera yaw is modified, the corresponding motion route will not be what is predicted. It looks like the participant character strikes in an odd zigzag sample or goes the improper route, particularly when two route keys are pressed on the identical time. I additionally observed how, when the participant holds down the motion keys, the character’s route doesn’t change alongside the digital camera angle.

I do not know why this occurs. Did I do one thing improper with the route transformation math? Is my velocity system simply dangerous? I actually need assistance right here.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles