Search Unity

Block ThirdPersonControlers movement not when keyboard key is active

Discussion in 'Scripting' started by jo6x, Aug 12, 2017.

  1. jo6x

    jo6x

    Joined:
    Jan 2, 2016
    Posts:
    113
    Hallo,

    I made a script to disable the movements of the thirdpersoncontroler, this only works when the keyboardkeys are going to be pushed down but not when the keyboardkeys are pushed down.
    Also when unpressing the key, the thirdpersoncontroler just keeps moving a bit.

    Code when having a collision:

    Code (CSharp):
    1.  
    2. void OnTriggerEnter (Collider col)
    3.     {
    4.         if (col.tag == "SpiderwebTag")
    5.         {
    6.             spideractive = true;
    7.             Destroy (Spiderweb);
    8. //code to disable the movements of the thirdpersoncontroler
    9.             TCP.gameObject.GetComponent<UnityStandardAssets.Characters.ThirdPerson.ThirdPersonCharacter> ().ChangeMovementState ();
    10.         }
    11.     }
    12.  
    13. void TimerInvoke()
    14.     {
    15.         if (spideractive == true)
    16.         {
    17.  
    18.             if (totalSec [0] < sec2wait)
    19.             {
    20.                 totalSec [0]++;
    21.             } else {
    22.                 CancelInvoke ("TimerInvoke");
    23.             }
    24.  
    25.             if (totalSec [0] == 1)
    26.             {
    27.                 Spin682017cAnimation2.SetActive (true);
    28.                 Zombi_running.SetActive (true);
    29.                 EthanBody.SetActive (false);
    30.             }
    31.  
    32.             if (totalSec [0] == 2)
    33.             {
    34.            
    35.             }
    36.  
    37.             if (totalSec [0] == 4)
    38.             {
    39. //code to enable the movements of the thirdpersoncontroler
    40.             TCP.gameObject.GetComponent<UnityStandardAssets.Characters.ThirdPerson.ThirdPersonCharacter> ().ChangeMovementState ();
    41.                 Spin682017cAnimation2.SetActive (false);
    42.                 EthanBody.SetActive (true);
    43.             }
    44.         }
    45.     }

    Code using changemovement() in gamobject TCP:

    Code (CSharp):
    1. public void ChangeMovementState()
    2.         {
    3.             tcpBlockMovement = !tcpBlockMovement;
    4.         }
    5.  
    6.  
    7. void UpdateAnimator(Vector3 move)
    8.         {
    9.             //if status extra ivm Collision Zombies//
    10.             if (!tcpBlockMovement)
    11.             {
    12.                 // update the animator parameters
    13.                 m_Animator.SetFloat ("Forward", m_ForwardAmount, 0.1f, Time.deltaTime);
    14.                 m_Animator.SetFloat ("Turn", m_TurnAmount, 0.1f, Time.deltaTime);
    15.                 m_Animator.SetBool ("Crouch", m_Crouching);
    16.                 m_Animator.SetBool ("OnGround", m_IsGrounded);
    17.             }
    18.  
    19. void ApplyExtraTurnRotation()
    20.         {
    21.             // help the character turn faster (this is in addition to root rotation in the animation)
    22.  
    23.             //if status extra ivm Collision Zombies//
    24.             if (!tcpBlockMovement)
    25.             {
    26.                 float turnSpeed = Mathf.Lerp(m_StationaryTurnSpeed, m_MovingTurnSpeed, m_ForwardAmount);
    27.                 transform.Rotate(0, m_TurnAmount * turnSpeed * Time.deltaTime, 0);
    28.             }
    29.         }
    30.  
    31. public void OnAnimatorMove()
    32.         {
    33.             // we implement this function to override the default root motion.
    34.             // this allows us to modify the positional speed before it's applied.
    35.  
    36.             if (m_IsGrounded && Time.deltaTime > 0)
    37.             {
    38.                 //if status extra ivm Collision Zombies//
    39.                 if (!tcpBlockMovement)
    40.                 {
    41.                     Vector3 v = (m_Animator.deltaPosition * m_MoveSpeedMultiplier) / Time.deltaTime;
    42.  
    43.                     // we preserve the existing y part of the current velocity.
    44.                     v.y = m_Rigidbody.velocity.y;
    45.                     m_Rigidbody.velocity = v;
    46.                 }
    47.             }
    48.         }
    Also tried the code disable keycode.uparrow:

    Code (CSharp):
    1.     void DisableKey( KeyCode key )
    2.     {
    3.         if( totalSec[0] == 1)
    4.         {
    5.             Event.current.Use();
    6.         }
    7.     }
    8.  
    9.     void OnGui()
    10.     {
    11.         DisableKey (KeyCode.UpArrow);
    12.     }
    Can someone help me to find the wright solution to make this work.

    Thanks.
     
    Last edited: Aug 12, 2017
  2. dpgdemos

    dpgdemos

    Joined:
    Apr 28, 2014
    Posts:
    24
    Are you just trying to stop the player in its tracks when keys are are not depressed? Could you just set isKinematic to true?

    Code (CSharp):
    1. GetComponent<Rigidbody>().isKinematic = true;
     
  3. jo6x

    jo6x

    Joined:
    Jan 2, 2016
    Posts:
    113
    Thanks bcsquared for your reply.
    The third person controler stops (stays on place) but the movement of walking stays. Do you also know how to stop the movements aswell.
     
  4. dpgdemos

    dpgdemos

    Joined:
    Apr 28, 2014
    Posts:
    24
    @jo6x, skip the isKinematic bit. Open your ThirdPersonAnimatorController in the Animator tab and create a new empty state. Create a parameter called "StopPlayer" (or whatever you want to name it). Create a transition to the empty state from the "Grounded" state. Click on the transition. Under Conditions in the Inspector, click on the '+' sign to add a condition. Select "StopPlayer" and it will already be set to true. Create another transition from the empty state back to the "Grounded" state. Click on the transition and add the "StopPlayer" condition again, but set it to false. Click on your empty state and in the Inspector, set Motion to "HumanoidIdle."

    Finally, in your script add the following when you need the character to stop completely and start your player again:

    Code (CSharp):
    1. private Animator _animator;
    2.  
    3. private void Awake()
    4. {
    5.     _animator = GetComponent<Animator>();
    6. }
    7.  
    8. // when you want to stop your player...
    9. _animator.SetBool("StopPlayer", true);
    10.  
    11. // when you want your player to move again...
    12. _animator.SetBool("StopPlayer", false);