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

Movement help needed (Javascript)

Discussion in 'Scripting' started by tjweh, Feb 9, 2014.

  1. tjweh

    tjweh

    Joined:
    Feb 9, 2014
    Posts:
    4
    Alright so, my code works like this, I have two scripts that run, a camera and a movement one. It's supposed to work like World of Warcraft (Zooming in and out of FPS and TPS. Left and right click camera/character movement, etc.)

    Everything works fine, except, when I jump while moving I stay in the air until I stop moving, this is also a problem for when I go up one of my hills I'll stay that height from the ground when it goes down.

    tl;dr? When the character jumps while moving, it stays in the air.

    Help me out please. Here's the code:
    Code (csharp):
    1. private var jumpSpeed:float = 18.0;
    2. private var gravity:float = 32.0;
    3. private var runSpeed:float = 15.0;
    4. private var walkSpeed:float = 45.0;
    5. private var rotateSpeed:float = 150.0;
    6.  
    7. private var grounded:boolean = false;
    8. private var moveDirection:Vector3 = Vector3.zero;
    9. private var isWalking:boolean = false;
    10. private var moveStatus:String = "idle";
    11.  
    12. static var dead : boolean = false;
    13.  
    14. function Update ()
    15. {
    16.  
    17.         if(dead == false) {
    18.        
    19.        
    20.        
    21.  
    22.  
    23.         // Only allow movement and jumps while grounded
    24.         if(grounded) {
    25.                 moveDirection = new Vector3((Input.GetMouseButton(1) ? Input.GetAxis("Horizontal") : 0),0,Input.GetAxis("Vertical"));
    26.                
    27.                 // if moving forward and to the side at the same time, compensate for distance
    28.                 // TODO: may be better way to do this?
    29.                 if(Input.GetMouseButton(1)  Input.GetAxis("Horizontal")  Input.GetAxis("Vertical")) {
    30.                         moveDirection *= .7;
    31.                        
    32.                 }
    33.                
    34.                 moveDirection = transform.TransformDirection(moveDirection);
    35.                 moveDirection *= isWalking ? walkSpeed : runSpeed;
    36.                
    37.                 moveStatus = "idle";
    38.                 if(moveDirection != Vector3.zero)
    39.                         moveStatus = isWalking ? "walking" : "running";
    40.                
    41.                 // Jump!
    42.                 //if(Input.GetButton("Jump"))
    43.                
    44.                 if (Input.GetKeyDown(KeyCode.Space))
    45.                
    46.                         moveDirection.y = jumpSpeed;
    47.         }
    48.        
    49.         // Allow turning at anytime. Keep the character facing in the same direction as the Camera if the right mouse button is down.
    50.         if(Input.GetMouseButton(1)) {
    51.                 transform.rotation = Quaternion.Euler(0,Camera.main.transform.eulerAngles.y,0);
    52.         } else {
    53.                 transform.Rotate(0,Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0);
    54.                
    55.         }
    56.        
    57.        
    58.                
    59.                
    60.                
    61.        
    62.         // Toggle walking/running with the T key
    63.         //if(Input.GetKeyDown("t"))
    64.                 //isWalking = !isWalking;
    65.  
    66.  
    67.        
    68.        
    69.         //Apply gravity
    70.         moveDirection.y -= gravity * Time.deltaTime;
    71.  
    72.        
    73.         //Move controller
    74.         var controller:CharacterController = GetComponent(CharacterController);
    75.         var flags = controller.Move(moveDirection * Time.deltaTime);
    76.         grounded = (flags  CollisionFlags.Below) != 0;
    77.        
    78.  
    79.         }
    80.        
    81.        
    82.         if(Input.GetMouseButton(1) || Input.GetMouseButton(0)) {
    83.                 //Screen.lockCursor = true;
    84.                
    85.                 Screen.showCursor = false;
    86.                
    87.                
    88.                 //var mouse1 = Input.mousePosition.y;
    89.                 //var mouse2 = Input.mousePosition.x;
    90.                
    91.                 }
    92.                
    93.                 //Vector3 mousePos = Input.mousePosition;
    94.         else  {
    95.                 //Screen.lockCursor = false;
    96.                 Screen.showCursor = false;
    97.                
    98.                 //Input.mousePosition.y = mouse1;
    99.                 //Input.mousePosition.x = mouse2;
    100.                
    101.                 //Input.mousePosition = mousePos;
    102.                
    103.                 }
    104.        
    105.        
    106. }
    107.  
    108. @script RequireComponent(CharacterController)
     
  2. tjweh

    tjweh

    Joined:
    Feb 9, 2014
    Posts:
    4
    Can anyone help?
     
  3. Cinema

    Cinema

    Joined:
    Sep 19, 2013
    Posts:
    18
    Maybe you could use a rigidbody for your motion and use the addforce function.
     
  4. tjweh

    tjweh

    Joined:
    Feb 9, 2014
    Posts:
    4
    bump