Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Darwin Mecharov · Apr 07, 2014 at 05:39 PM · movementrigidbodyvelocityaddforcestuck

Rigidbody movement conflict?

Hello guys! I've got a problem. When I hold AWSD then hold spacebar, it just ends up with the character getting up a little from the ground then going back to normal, like as if nothing happened! Is there some way to fix this? Here is my script:

 function OnTriggerStay ()
 {
     Jumped = false;
 }
 function FixedUpdate ()
 {
     //Player movement
     if(Input.GetKey(KeyCode.Space) && !Jumped)
         {
             Jumped = true;
             rigidbody.AddForce(Vector3.up * JumpHeight);
         }
     if(Input.GetKey(KeyCode.W))
         rigidbody.velocity = transform.forward * Forward * Time.deltaTime;
     if(Input.GetKey(KeyCode.S))
         rigidbody.velocity = transform.forward * -MoveSpeed * Time.deltaTime;
     if(Input.GetKey(KeyCode.D))
         rigidbody.velocity = transform.right * MoveSpeed * Time.deltaTime;
     if(Input.GetKey(KeyCode.A))
         rigidbody.velocity = transform.right * -MoveSpeed * Time.deltaTime;
     //Player run movement
     if(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
     {
         if(Input.GetKey(KeyCode.W))
             rigidbody.velocity = transform.forward * ForwardRun * Time.deltaTime;
         if(Input.GetKey(KeyCode.S))
             rigidbody.velocity = transform.forward * -RunSpeed * Time.deltaTime;
         if(Input.GetKey(KeyCode.D))
             rigidbody.velocity = transform.right * RunSpeed * Time.deltaTime;
         if(Input.GetKey(KeyCode.A))
             rigidbody.velocity = transform.right * -RunSpeed * Time.deltaTime;
     }
 }

I'm guessing it's because I'm using rigidbody movement for both commands? Sorry, only started Unity about a week ago.

Comment
Add comment · Show 2
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 andrew_196 · Apr 08, 2014 at 12:08 AM 0
Share

you might need to check the strength of your gravity level to see how high it is.

avatar image Darwin Mecharov · Apr 08, 2014 at 12:54 AM 0
Share

I didn't change the gravity, just activated it on my rigidbody component

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by AlucardJay · Apr 08, 2014 at 06:04 AM

It is because immediately after adding force you are hard setting the velocity of the rigidbody, overriding the force added. You need to maintain the Y-velocity.

Edit : Sorry for the late reply. I didn't have time to give an example, but tbkn has given an example of what I was describing. And it seems like my comment was converted to answer, so I have stripped down one of my player code projects to give an example for future readers. Even though stripped down it is quite long, the main part is in the function ApplyMovement() where (again as tbkn demonstrated) the rigidbody velocity Y is maintained in the calculated velocity before applying to the rigidbody.

 calcVel.y += myRigidbody.velocity.y;

Hopefully you'll see some interesting stuff in my below code :

 //------------------------------//
 //  RigidbodyMovement.js        //
 //  Written by Alucard Jay      //
 //  4/8/2014                    //
 //------------------------------//
 
 #pragma strict
 
 
 public var moveSpeed : float = 7.0;
 public var jumpVelocity : float = 120.0;
 public var airMovementModifier : float = 0.1;
 public var velocityLerpTime : float = 0.05;
 public var maxSlopeAngle : float = 60.0;
 public var inputDeadZone : float = 0.05;
 
 public var isAlive : boolean = false;
 
 private var myTransform : Transform;
 private var myRigidbody : Rigidbody;
 
 private var desiredVelocity : Vector3 = Vector3.zero;
 private var inputVector : Vector3 = Vector3.zero;
 private var isGrounded : boolean = false;
 private var isJumping : boolean = false;
 
 
 
 //  Persistant Functions
 //  ------------------------------------------------------------------------------------------------
 
 
 function Awake() 
 {
     Initialize();
 }
 
 
 function Start() 
 {
     myTransform = transform;
     myRigidbody = transform.rigidbody;
 }
 
 
 function Update() 
 {
     if ( !isAlive )
     {
         return;
     }
     
     // -- MOVEMENT -- 
     GetPlayerInput();
     ApplyMovement();
 }
 
 
 function FixedUpdate() 
 {
     if ( isAlive )
     {
         myRigidbody.velocity = desiredVelocity;
     }
 }
 
 
 //  Other Functions
 //  ------------------------------------------------------------------------------------------------
 
 
 function Initialize() 
 {
     isAlive = true;
 }
 
 
 // PLAYER INPUT
 //  ------------------------------------------------------------------------------------------------
 
 function GetPlayerInput() 
 {
     // reset inputVector
     inputVector = Vector3.zero;
     
     // get movement inputs
     var horz : float = Input.GetAxis("Horizontal");
     var vert : float = Input.GetAxis("Vertical");
     
     // get player Vertical input
     if ( vert < -inputDeadZone || vert > inputDeadZone )
     {
         inputVector += Vector3( 0, 0, vert );
     }
     
     
     // get player Horizontal input
     if ( horz < -inputDeadZone || horz > inputDeadZone )
     {
         inputVector += Vector3( horz, 0, 0 );
     }
     
     // get player Jump input
     if ( Input.GetKeyDown(KeyCode.Space) )
     {
         isJumping = true;
     }
 }
 
 
 // APPLY MOVEMENT
 //  ------------------------------------------------------------------------------------------------
 
 function ApplyMovement() 
 {
     // calculate the direction for the force to be applied
     var calcVel : Vector3 = myTransform.TransformDirection( inputVector.normalized ) * moveSpeed;
     
     // check if jumping, add Jump velocity, 
     // else maintain current Y velocity
     if ( isJumping )
     {
         calcVel.y = jumpVelocity;
         
         isJumping = false;
     }
     else
     {
         // maintain current Y velocity
         //calcVel.y = myRigidbody.velocity.y;
         // add velocity.y instead of hard setting it
         calcVel.y += myRigidbody.velocity.y;
     }
     
     // calculate desired velocity
     if ( !isGrounded )
     {
         // calculate desired velocity
         calcVel *= airMovementModifier; // restrict movement while airborne
         calcVel += myRigidbody.velocity * ( 1.0 - airMovementModifier ); // maintain velocity while in the air
         desiredVelocity = Vector3.Lerp( myRigidbody.velocity, calcVel, velocityLerpTime );
     }
     else
     {
         // calculate desired velocity
         desiredVelocity = Vector3.Lerp( myRigidbody.velocity, calcVel, velocityLerpTime );
     }
 }
 
 
 //  Is Grounded Collision Events
 //  ------------------------------------------------------------------------------------------------
 
 
 var currSlopeAngle : float = 0.0; // Debugging slope angle
 
 
 function OnCollisionEnter( collision : Collision )
 {
     for ( var contact : ContactPoint in collision.contacts )
     {
         if ( Vector3.Angle( contact.normal, Vector3.up ) < maxSlopeAngle )
         {
             isGrounded = true;
         }
         
         
         // ----
         
         currSlopeAngle = Vector3.Angle( contact.normal, Vector3.up );
         
         // ----
     }
 }
 
 
 function OnCollisionStay( collision : Collision )
 {
     for ( var contact : ContactPoint in collision.contacts )
     {
         if ( Vector3.Angle( contact.normal, Vector3.up ) < maxSlopeAngle )
         {
             isGrounded = true;
         }
         
         
         // ----
         
         // Debugging slope angle
         // character is climbing slopes
         
         currSlopeAngle = Vector3.Angle( contact.normal, Vector3.up );
         
         // ----
     }
 }
 
 
 function OnCollisionExit()
 {
     isGrounded = false;
 }


Comment
Add comment · Show 3 · 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 Tomer-Barkan · Apr 08, 2014 at 06:23 AM 2
Share

Try the following commands to keep the original y-velocity when manually setting the rigidbody velocity:

 Vector3 tempVelocity;

 if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.W))
     tempVelocity = transform.forward * Forward * Time.deltaTime; // change this in all you ifs, ins$$anonymous$$d of rigidbody.velocity set tempVelocity
 ...

 // now set the velocity, using the original y
 tempVelocity.y = rigidbody.velocity.y;
 rigidbody.velocity = tempVelocity;

  
avatar image Darwin Mecharov · Apr 08, 2014 at 06:27 AM 0
Share

It works! It also solves my problem about character falling down slowly. Thanks!

avatar image AlucardJay · Apr 08, 2014 at 09:15 AM 0
Share

I updated the comment (which somehow became an answer!) with a stripped down version of one of my controllers. @tbkn has given a great example of what I was trying to say.

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 move Character with AddForce 1 Answer

Question regarding Ridigbody2D.velocity.magnitude 1 Answer

Ball moving in a tube 0 Answers

Why is velocity checking intensive? 0 Answers

rigidbody velocity on x axis and addforce on y axis ?? 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