- Home /
 
smooth movement
Well I finished scripting my core movement script but the problem is the movement is not smooth. How can i make it more smoother? Player can jump for infinite times, I tried to disable it with yield but it still doesnt work. Player can jump rapidly if the player press space button rapidly. Its like infinite jump. How can i disable that too?
 #pragma strict
 var speed : float = 2;
 var jumpForce : float = 20;
 
 function Update () 
 {
     if (Input.GetKeyDown("w"))
     {
     this.transform.position.x += this.speed;
     }
     
         if (Input.GetKeyDown("a"))
         {
         this.transform.position.z += this.speed;
         }
         
             if (Input.GetKeyDown("s"))
             {
             this.transform.position.x -= this.speed;
             }
             
                 if (Input.GetKeyDown("d"))
                 {
                 this.transform.position.z -= this.speed;
                 }
                     
                     if ( Input.GetKeyDown("space") ) 
                     {
                     Jump();
                     }
 
 }
 
                         function Jump()
                         {
                         yield WaitForSeconds(2);
                         rigidbody.AddForce (Vector3.up *jumpForce);
                         }
 
              Try 'this.speed * Time.deltaTime; '
Also, not sure weather you have some other purpose for doing so, but you don't need to define 'this' for just using variables and transforms.
transform.position.z -= speed * Time.deltaTime;
should work just fine.
With your jump problem, your might be better off using a raycast - basically a line from your object that will collide with things, and you can set it's length.
http://docs.unity3d.com/ScriptReference/Physics.Raycast.html
So if the line isn't touching the floor, don't jump, else if it is touching the floor, jump.
e.g. Untested code, there may be spelling mistakes
 RaycastHit hit;  
 
 if(Physics.Raycast(transform.position, Vector3.down, out hit, 1.5f)){
 
    Jump();
 
 } 
 
                  If raycasts are a new concept, they can be confusing to understand (personally anyway)you can do this: To visibly see the ray you'll be casting.
 Debug.drawLine(transform.position, Vector3.down, color.black,  1.5f);
 
                 Answer by clunk47 · Jun 21, 2014 at 04:48 PM
If you're using a rigidbody for your character, place any physics code inside FixedUpdate(). I'd also use forces / velocity rather than position iteration. You would also want to use rigidbody.position or rigidbody.movePosition if you prefer using positions rather than forces, again, in FixedUpdate. Have a look at RigidbodyFPSWalker on the wiki.
Answer by babaji1234 · Jun 21, 2014 at 03:27 PM
try-
 #pragma strict
 var speed : float = 2;
 var jumpForce : float = 20;
  
 function Update () 
 {
     if (Input.GetKeyDown("w"))
     {
     this.transform.position.x += this.speed*Time.deltaTime;
     }
  
         if (Input.GetKeyDown("a"))
         {
         this.transform.position.z += this.speed*Time.deltaTime;
         }
  
             if (Input.GetKeyDown("s"))
             {
             this.transform.position.x -= this.speed*Time.deltaTime;
             }
  
                 if (Input.GetKeyDown("d"))
                 {
                 this.transform.position.z -= this.speed*Time.deltaTime;
                 }
  
                     if ( Input.GetKeyDown("space") ) 
                     {
                     Jump();
                     }
  
 }
  
                         function Jump()
                         {
                         yield WaitForSeconds(2);
                         rigidbody.AddForce (Vector3.up *jumpForce*Time.deltaTime);
                         }
 
              Your answer
 
             Follow this Question
Related Questions
How to make smooth jump? 1 Answer
Move Object Smoothly 1 Answer
Networking Synchronize Problem. 0 Answers
Rotate character to the moving direction problems? 2 Answers
Smooth Camera/Object Movement 1 Answer