Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Transform.translate player movement issue [JS]

Discussion in 'Scripting' started by Quist, Feb 26, 2017.

  1. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    So the problem is basicly that my player moves back to the middle lane instantly without a button being pressed which would make it do that. So when I make the player go left, it instantly goes back to the middle like the following video shows, why????!?!?!??



    The script:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var left = false;
    4. var mid = false;
    5. var right = false;
    6.  
    7. function FixedUpdate ()
    8. {
    9.     GetComponent.<Rigidbody>().AddForce (Vector3.down * 35);
    10.     GetComponent.<Rigidbody>().AddForce (Vector3.forward * 5);
    11.        
    12. //   if (Input.GetKey (KeyCode.W))
    13. //   {
    14. //       GetComponent.<Rigidbody>().AddForce (Vector3.forward * 110);
    15. //   }
    16.    // if (Input.GetKey (KeyCode.S))
    17.   //  {
    18.    //     GetComponent.<Rigidbody>().AddForce (Vector3.back * 80);
    19.   //  }
    20.     if (Input.GetKey (KeyCode.A) && mid == true || right == true)
    21.     {
    22.         transform.Translate (Vector3.left * Time.deltaTime * 210);
    23.     }
    24.     if (Input.GetKey (KeyCode.D) && mid == true || left == true)
    25.     {
    26.         transform.Translate (Vector3.right * Time.deltaTime * 210);
    27.     }
    28.     {
    29.         GetComponent.<Rigidbody>().velocity = Vector3.ClampMagnitude(GetComponent.<Rigidbody>().velocity, maxVel);
    30.     }  
    31.    
    32. }
    33.  
    34. var maxVel : float = 16.0;
    35.  
    36.  
    37. function OnCollisionEnter (hit : Collision)
    38. {
    39.   if(hit.gameObject.name == "Left")
    40.   {
    41.       left = true;
    42.       mid = false;
    43.       right = false;
    44.   }
    45.   if(hit.gameObject.name == "Mid")
    46.   {
    47.       left = false;
    48.       mid = true;
    49.       right = false;
    50.   }
    51.   if(hit.gameObject.name == "Right")
    52.   {
    53.       left = false;
    54.       mid = false;
    55.       right = true;
    56.   }
    57. }
    58.  
    59. //function OnCollisionExit (hit : Collision)
    60. //{
    61. // if(hit.gameObject.name == "Floor")
    62. // {
    63. //     yield WaitForSeconds(0.15);
    64. // }
    65. //}
    66.  
     
  2. luke_2

    luke_2

    Joined:
    Nov 20, 2012
    Posts:
    29
  3. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    Thanks a bunch that made it a lot better!

    However, the script still jumps from the left all the way to the right and all the way from the right to the left instead of hitting the middle section first. It is like the ball hits the middle part so fast that it still think the button is being pressed and thereby jumps twice.
    Any ideas?
     
  4. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    Use GetKeyUp instead. GetKey will return true every frame its interacted with.
     
  5. Quist

    Quist

    Joined:
    Feb 25, 2014
    Posts:
    284
    Hmm that worked, but also made it do the opposite - now it is very slow. I cant spam the keys, it needs at least 1 - 2 seconds between each function is called before it can register the next one