Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Help with my movement script please?

Discussion in 'Scripting' started by gore23, Jul 17, 2011.

  1. gore23

    gore23

    Joined:
    Jan 18, 2011
    Posts:
    324
    I have made a so far succesful movement script for my game. I have however come across a few problems I am having trouble solving.

    1 - Jumping - When I jump I disable the controls of the movement while in air like in real life, you cant change your direction in air. Problem is I cant get it too move in the direction I am currently going after I jump and jump at a consistant height and speed. It will sometimes barely jump at all. Any suggestions please ?.

    2 - I use a ray to detect if i am on the ground, problem is that when just barely half the character is over a edge I stop and get stuck on that edge because the ray only get the very center so as soon as the center is off the edge your stuck.. How do I solve this ?


    Here is the current script

    Code (csharp):
    1. //Set Speed
    2. var MoveSpeed = 3.5;
    3. var BackwardsSpeed = 2.5;
    4. private var CurrentMoveSpeed = 3.5;
    5. var JumpForce = 250;
    6.  
    7.  
    8.  
    9. function Update () {
    10.  
    11. //Identify CameraRotator for Direction Information
    12. var Rotator = GameObject.Find("CameraRotator");
    13.  
    14. //Cast Ray
    15.  
    16.  
    17.  
    18. //MOVING
    19. //Identify Movement Buttons
    20. var w =  Input.GetAxis("Forward");
    21. var s =  Input.GetAxis("Backward");
    22. var a =  Input.GetAxis("Left");
    23. var d =  Input.GetAxis("Right");
    24. var j = Input.GetAxis("Jump");
    25.  
    26. //Move Player
    27. if (Physics.Raycast (transform.position, -transform.up, 1)) {
    28. transform.Translate((d-a)*CurrentMoveSpeed*Time.deltaTime,0,(w-s)*CurrentMoveSpeed*Time.deltaTime, Rotator.transform);
    29. }
    30. //Backwards Speed Setup
    31.     if (Input.GetButton("Backward")){
    32.        
    33.         CurrentMoveSpeed = BackwardsSpeed;
    34.        
    35.     }else{
    36.    
    37. CurrentMoveSpeed = MoveSpeed;
    38.  
    39.     }
    40. }
    41. //END OF MOVING
    42.  
    43.  
    44.  
    45.  
    46.  
    47. //JUMPING
    48. function FixedUpdate () {
    49.  
    50. //Identify CameraRotator for Direction Information
    51. var Rotator = GameObject.Find("CameraRotator");
    52.  
    53. //Jump
    54.     if(Input.GetButtonDown("Jump")){   
    55.    
    56.         rigidbody.AddForce (Vector3.up * JumpForce);   
    57.         rigidbody.AddForce (Rotator.transform.forward * JumpForce);
    58.        
    59.     }
    60. }
    61.  
    62.  
     
  2. gore23

    gore23

    Joined:
    Jan 18, 2011
    Posts:
    324
    anyone?
     
  3. priceap

    priceap

    Joined:
    Apr 18, 2009
    Posts:
    274
    You are doing your transform.Translate() inside of the conditional statement of whether you are grounded, so if you are not grounded, then no translation happens at all.

    If instead, you calculate a moveDirection and continuously apply it, but only accept Input and modify the moveDirection if you are grounded.
    This way you will keep moving in the last "known" moveDirection if you jump.

    Look at the third person character controller example from Unity to see how they are doing it that way. You might even want to switch to using a characterController as what you are doing with translate, physics.raycast, and rigidbody.Addforce, is handled pretty well with it. But I realize you may specifically be designing a rigidbody controller so it will interact directly with other rigidbodies.
     
    Last edited: Jul 18, 2011
  4. gore23

    gore23

    Joined:
    Jan 18, 2011
    Posts:
    324
    I dont exactly know the commands on some of that on how you do that... I already get that I need too get the direction but how I was sorta confused on, and I am trying to make my own scripts as much as possible and not use others.... sorta against the point too use other peoples scripts when your trying too learn to make them =/
     
  5. priceap

    priceap

    Joined:
    Apr 18, 2009
    Posts:
    274
    well basically what I mean is an adjustment to your code like this:

    Code (csharp):
    1. var moveDirection : Vector3 ;  //member variable
    2.  
    3. function Update()
    4. {
    5.   if ( Physics.Raycast (transform.position, -transform.up, 1) )
    6.   {
    7.      moveDirection  = Vector3( (d-a)*CurrentMoveSpeed*Time.deltaTime, 0, (w-s)*CurrentMoveSpeed*Time.deltaTime ) ;
    8.   }
    9.  
    10.   transform.Translate( moveDirection, Rotator.transform );
    11.  
    12.   // other stuff, including jumping, etc.
    13. }
    14.  
    15.  
    so that the transform.Translate continues in whatever direction it was just before hitting jump, but any new key input is ignored unit the raycast becomes true again.

    I was not suggesting you use the other code - I just meant to look at it and get some very helpful clues on how to design your own. It is what I do - I wrote my own but a lot was learned from the Unity version.

    I did suggest using the characterController, which is a component that you still need to design a script to control it, but it has nice features such as an isGrounded variable, and Move function that behaves somewhere between transform.Translate and ridigdbody.AddForce
     
  6. gore23

    gore23

    Joined:
    Jan 18, 2011
    Posts:
    324
    I dont get what the characterController is really. please explain?

    Thanks for the help =)

    Oh and the character doesnt move in the jump direction still =/ Did I do something wrong with this ?

    just stops all movement in other directions.

    Code (csharp):
    1. //Set Speed
    2. var MoveSpeed = 3.5;
    3. var BackwardsSpeed = 2.5;
    4. private var CurrentMoveSpeed = 3.5;
    5. var JumpForce = 250;
    6.  
    7.  
    8.  
    9. function Update () {
    10.    
    11.     //Identify CameraRotator for Direction Information
    12.     var Rotator = GameObject.Find("CameraRotator");
    13.    
    14.     //MOVING
    15.     //Identify Movement Buttons
    16.     var w =  Input.GetAxis("Forward");
    17.     var s =  Input.GetAxis("Backward");
    18.     var a =  Input.GetAxis("Left");
    19.     var d =  Input.GetAxis("Right");
    20.     var j = Input.GetAxis("Jump");
    21.    
    22.     //Move Player
    23.     if (Physics.Raycast (transform.position, -transform.up, 1)) {
    24.        
    25.         MoveDirection = Vector3((d-a)*CurrentMoveSpeed*Time.deltaTime,0,(w-s)*CurrentMoveSpeed*Time.deltaTime);
    26.        
    27.         //Backwards Speed Setup
    28.         if (Input.GetButton("Backward")){
    29.             CurrentMoveSpeed = BackwardsSpeed;
    30.            
    31.         }else{
    32.             CurrentMoveSpeed = MoveSpeed;
    33.            
    34.         }
    35.     }
    36.    
    37.     transform.Translate(MoveDirection, Rotator.transform);
    38.    
    39. }
    40. //END OF MOVING
    41.  
    42.  
    43.  
    44.  
    45.  
    46. //JUMPING
    47. function FixedUpdate () {
    48.    
    49.     //Identify CameraRotator for Direction Information
    50.     var Rotator = GameObject.Find("CameraRotator");
    51.    
    52.     //Jump
    53.     if(Input.GetButtonDown("Jump")){
    54.        
    55.         rigidbody.AddForce (Vector3.up * JumpForce);
    56.        
    57.        
    58.     }
    59. }
    60.  
    61.  
     
    Last edited: Jul 18, 2011
  7. gore23

    gore23

    Joined:
    Jan 18, 2011
    Posts:
    324
    Got that also XD

    Some reason my guy doesnt jump very accurately though, sometimes he jumps the full height, other times he doesnt. Is the force way of jumping a bad way too go, or what other way could I do it?

    My guy will still walk up 2-5 times the size of steps without me needing too jump, Why ?

    Code (csharp):
    1. //Set Speed
    2. var MoveSpeed = 3.5;
    3. var BackwardsSpeed = 2.5;
    4. var JumpForce = 250;
    5.  
    6. private var CurrentMoveSpeed = 3.5;
    7. private var moveDirection : Vector3;
    8. private var Rotator : GameObject;
    9.  
    10. function Update () {
    11.    
    12.     //Identify CameraRotator for Direction Information
    13.     Rotator = GameObject.Find("CameraRotator");
    14.    
    15.     //MOVING
    16.     //Identify Movement Buttons
    17.     var w =  Input.GetAxis("Forward");
    18.     var s =  Input.GetAxis("Backward");
    19.     var a =  Input.GetAxis("Left");
    20.     var d =  Input.GetAxis("Right");
    21.     var j = Input.GetAxis("Jump");
    22.    
    23.     //Move Player
    24.     if (Physics.Raycast (transform.position, -transform.up, 1.1)) {
    25.        
    26.         moveDirection = Vector3((d-a)*CurrentMoveSpeed*Time.deltaTime,0,(w-s)*CurrentMoveSpeed*Time.deltaTime);
    27.        
    28.         //Backwards Speed Setup
    29.         if (Input.GetButton("Backward")){
    30.             CurrentMoveSpeed = BackwardsSpeed;
    31.            
    32.         }else{
    33.             CurrentMoveSpeed = MoveSpeed;
    34.            
    35.         }
    36.     }
    37.    
    38.     transform.Translate(moveDirection, Rotator.transform);
    39.    
    40. }
    41. //END OF MOVING
    42.  
    43.  
    44. //JUMPING
    45. function FixedUpdate () {
    46.     //Identify CameraRotator for Direction Information
    47.     Rotator = GameObject.Find("CameraRotator");
    48.    
    49.     //Jump
    50.     if(Input.GetButtonDown("Jump")){
    51.         if(Physics.Raycast (transform.position, -transform.up, 1.1)){
    52.             rigidbody.AddForce (Vector3.up * JumpForce);
    53.            
    54.         }
    55.     }
    56. }
    57.  
    58.  
     
    Last edited: Jul 18, 2011
  8. priceap

    priceap

    Joined:
    Apr 18, 2009
    Posts:
    274
    You are translating the object in your update function, and adding force to it in a fixedUpdate function. Because you might be holding both a navigation key and jump key at the same moment, it is likely the two are conflicting with each other - resulting in the jump not always being what you want.

    Again, in a character controller example, the upward jumping movement is also added to the moveDirection variable, so the direction of movement is shared in one place - when your transform.Translate is occurring, it is overriding the result of the Addforce.
    Perhaps if your " if (Input.GetButtonDown("Jump")) " were integrated with the other input conditions in the Update function, you could intercept the inputs better so that the jump is done without interference of the other inputs.

    you made the point that you want to learn by making your own script, which is fine, but still looking at other examples such as the third person controller or maybe some other rigidbody controllers would help.
     
  9. gore23

    gore23

    Joined:
    Jan 18, 2011
    Posts:
    324
    I am having trouble figuring out how exactly the character motor and fps controller work together to make movement XD
     
  10. gore23

    gore23

    Joined:
    Jan 18, 2011
    Posts:
    324
    How could I make my jump integrated with the update function, I have tryed using a boolean to check but it still has the same effect. I wanted to keep the fixed update sence it applied physics to the jump but i cant get same result in normal update... I am sorta torn on what to do.

    I also was wondering if their was a way to make it so that the way I can make my self move same pace even when pressing both forward and sideways and not go twice as fast because the side and forward speeds are together *atleast that is how it feels in my head XD*

    Currently I use this for my w,s,a,d movement inputing

    moveDirection = Vector3((d-a)*CurrentMoveSpeed*Time.deltaTime,0,(w-s)*CurrentMoveSpeed*Time.deltaTime);