19.2 C
New York
Saturday, August 9, 2025

unity – Why does this code behave unusually with totally different digicam angles?


Related to this difficulty 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 (be aware 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;

    non-public CharacterController controller;

    non-public Vector3 _direction;
    non-public Vector3 _velocity;

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

    // Replace 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 shifting = Mathf.Abs(dirComponent) > 0.0f;
        if (shifting)
        {
            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 shifting = Mathf.Abs(dirComponent) > 0.0f;
        if (!shifting)
        {
            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 Rework camTransform;

    non-public Actor actor;

    non-public InputAction moveAction;
    non-public InputAction jumpAction;
    non-public InputAction crouchAction;

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

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

    // Replace 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 digicam yaw is modified, the corresponding motion path will not be what is predicted. It looks as if the participant character strikes in an odd zigzag sample or goes the incorrect path, particularly when two path keys are pressed on the similar time. I additionally seen how, when the participant holds down the motion keys, the character’s path doesn’t change alongside the digicam angle.

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

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles