Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by wegaz · Jun 21, 2014 at 01:33 PM · javascriptmovementsmoothapi

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);
                         }

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RyanPaterson · Jun 21, 2014 at 03:14 PM 0
Share

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);

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);
                         }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges