Home > other >  Unity 3d Input.GetKeyDown/Up nullifies character movements
Unity 3d Input.GetKeyDown/Up nullifies character movements

Time:10-30

This script allows that if you press F5, the character's view will be changed (there is the 1st person view and the 3rd person view). Only, at the beginning I used the input.GetKey (KeyCode.F5), but that poses a problem: if you press too long it will change the character's view several times in 1 second, which I don't want. So I decided to use input.GetKeyDown (KeyCode.F5). This formula removes this problem but creates another one: when the character is in 1st person mode (the basic view is the 3rd person) my character no longer performs any movement but if I switch back to the basic view (3rd person) the person repeats all these movements...

using UnityEngine;
using System.Collections;

// AimBehaviour inherits from GenericBehaviour. This class corresponds to aim and strafe behaviour.
public class AimBehaviourBasic : GenericBehaviour
{

    public Texture2D crosshair;                                           // Crosshair texture.
    public float aimTurnSmoothing = 0.15f;                                // Speed of turn response when aiming to match camera facing.
    public Vector3 aimPivotOffset = new Vector3(0.5f, 1.2f,  0f);         // Offset to repoint the camera when aiming.
    public Vector3 aimCamOffset   = new Vector3(0f, 0.4f, -0.7f);         // Offset to relocate the camera when aiming.

    private int aimBool;                                                  // Animator variable related to aiming.
    private bool aim;                                                     // Boolean to determine whether or not the player is aiming.

    // Start is always called after any Awake functions.
    void Start ()
    {
        // Set up the references.
        aimBool = Animator.StringToHash("Aim");
    }

    // Update is used to set features regardless the active behaviour.
    void Update ()
    {
        // Activate/deactivate aim by input.
        if (Input.GetKeyDown (KeyCode.F5) && !aim)
        {
            StartCoroutine(ToggleAimOn());
        }
        else 
        if (Input.GetKeyDown (KeyCode.F5) && aim)
        {           
            StartCoroutine(ToggleAimOff());         
        }
    }

    // Co-rountine to start aiming mode with delay.
    private IEnumerator ToggleAimOn()
    {
        yield return new WaitForSeconds(0.05f);
        // Aiming is not possible.
        if (behaviourManager.GetTempLockStatus(this.behaviourCode) || behaviourManager.IsOverriding(this))
            yield return false;
        // Start aiming.
        else
        {
            aim = true;
            int signal = 1;
            aimCamOffset.x = Mathf.Abs(aimCamOffset.x) * signal;
            aimPivotOffset.x = Mathf.Abs(aimPivotOffset.x) * signal;
            yield return new WaitForSeconds(0.1f);
            behaviourManager.GetAnim.SetFloat(speedFloat, 0);
            // This state overrides the active one.
            behaviourManager.OverrideWithBehaviour(this);
        }
    }

    // Co-rountine to end aiming mode with delay.
    private IEnumerator ToggleAimOff()
    {
        
        aim = false;
        yield return new WaitForSeconds(0.3f);
        behaviourManager.GetCamScript.ResetTargetOffsets();
        behaviourManager.GetCamScript.ResetMaxVerticalAngle();
        yield return new WaitForSeconds(0.05f);
        behaviourManager.RevokeOverridingBehaviour(this);
    }

    // LocalFixedUpdate overrides the virtual function of the base class.
    public override void LocalFixedUpdate()
    {
        // Set camera position and orientation to the aim mode parameters.
        if(aim)
        {
  behaviourManager.GetCamScript.SetTargetOffsets (aimPivotOffset, aimCamOffset);
        }
    }

    // LocalLateUpdate: manager is called here to set player rotation after camera rotates, avoiding flickering.
    public override void LocalLateUpdate()
    {
        AimManagement();
    }

    // Handle aim parameters when aiming is active.
    void AimManagement()
    {
        // Deal with the player orientation when aiming.
        Rotating();
    }

    // Rotate the player to match correct orientation, according to camera.
    void Rotating()
    {
        Vector3 forward = behaviourManager.playerCamera.TransformDirection(Vector3.forward);
        // Player is moving on ground, Y component of camera facing is not relevant.
        forward.y = 0.0f;
        forward = forward.normalized;

        // Always rotates the player according to the camera horizontal rotation in aim mode.
        Quaternion targetRotation =  Quaternion.Euler(0, behaviourManager.GetCamScript.GetH, 0);

        float minSpeed = Quaternion.Angle(transform.rotation, targetRotation) * aimTurnSmoothing;

        // Rotate entire player to face camera.
        behaviourManager.SetLastDirection(forward);
        transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, minSpeed * Time.deltaTime);

    }

    // Draw the crosshair when aiming.
    void OnGUI () 
    {
        if (crosshair)
        {
            float mag = behaviourManager.GetCamScript.GetCurrentPivotMagnitude(aimPivotOffset);
                GUI.DrawTexture(new Rect(Screen.width / 2 - (crosshair.width * 0.5f),
                                         Screen.height / 2 - (crosshair.height * 0.5f),
                                         crosshair.width, crosshair.height), crosshair);
        }
    }
}

Is there a problem in the script ?

CodePudding user response:

// Attempt to override any active behaviour with the behaviours on queue.
// Use to change to one or more behaviours that must overlap the active one (ex.: aim behaviour).
public bool OverrideWithBehaviour(GenericBehaviour behaviour)
{
    // Behaviour is not on queue.
    if (!overridingBehaviours.Contains(behaviour))
    {
        // No behaviour is currently being overridden.
        if (overridingBehaviours.Count == 0)
        {
            // Call OnOverride function of the active behaviour before overrides it.
            foreach (GenericBehaviour overriddenBehaviour in behaviours)
            {
                if (overriddenBehaviour.isActiveAndEnabled && currentBehaviour == overriddenBehaviour.GetBehaviourCode())
                {
                    overriddenBehaviour.OnOverride();
                    break;
                }
            }
        }
        // Add overriding behaviour to the queue.
        overridingBehaviours.Add(behaviour);
        return true;
    }
    return false;
}

CodePudding user response:

For more information, I invite you to go Coroutine activated thanks with input.GetKeyDown/Up block the movements of my player in 1st person mode

  • Related