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 Den D. · Jul 26, 2014 at 06:29 AM · rigidbodyvelocity

AddRelativeForce not working correctly

I am stuck on the code for couple of days but couldn't figure it out. Therefore I turned out to look the answer here but couldn't find any that match the requirement Hope some One might help

here is the code

     /*  Part1 
      *Step Horizontal Movement
      * Use to get left and right movement 
      */
     if(Input.GetKey ("right")||Input.GetKey ("left")){
         moveDir.x = Input.GetAxis("Horizontal"); // get result of AD keys in X
     }
     rigidbody.velocity = moveDir * speed;

     /*Part2
      *Continuse Forward Movement
      */
     if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
     {
         moveDir.z = Input.GetAxis("Vertical");
         rigidbody.AddRelativeForce(moveDir*50);
     }

I have a Cube with added rigidBody Component. When pressed left or right key the cube should move in that direction and when key is released it should stop Part one when run commenting part2 code runs correctly

the other movement that I am trying to achieve is the cube should move continuously.But the thing is when pressed up key it should increase the magnitude and when released it should maintain the magnitude and move constantly.This is also achieved successfull with the part2 code by commenting part1 code But when but run without commenting any of them part1 results are correct but part2 does not provide correct result which was obtained by commenting part1 code

Is there any other way to obtain the same reult

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 robertbu · Jul 26, 2014 at 01:55 PM 0
Share

Your problem is easily solved if the cube remain axes aligned. It is more work if the object rotates. From your code I cannot tell. Part 1 suggest it is aligned since you are directly setting the Rigidbody with a world direction. Part 2 suggests that it is not since you use AddRelativeForce() ins$$anonymous$$d of AddForce(). So does the object remain aligned? Can the object collide with other objects and if so, have you frozen the rotation in the Rigidbody constraints?

avatar image Den D. · Jul 26, 2014 at 02:28 PM 0
Share

Yes the object remain aligned and it collide with the other object and rotational component of the object is frozen

1 Reply

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

Answer by robertbu · Jul 26, 2014 at 04:30 PM

You don't give me enough code to tell if you are using Javascript or C#. Here is a C# solution (untested):

     Vector3 vel = rigidbody.velocity;
     vel.x = Input.GetAxisRaw("Horizontal") * speed;
     rigidbody.velocity = vel;

     rigidbody.AddForce (Vector3.forward * Input.GetAxis("Vertical") * 50.0f);

First the Input.GetKey() tests are unnecessary. If you look at how the Horizontal and Vertical Axes are defined, you will see they have the keys you are using defined. Also there is a GetAxisRaw() that gets the the raw data. GetAxis() is buffered a bit. You may like the way your game feels with GetAxis() rather than GetAxisRaw(), but your question indicated an immediate stop.

Note the way this problem is solves is to have part 1 only manipulate the 'x' axis, and part 2 only manipulate the 'z' axis of the object.

If you are using Javascript/Unityscript, it can be simplified to:

     rigidbody.velocity.x = Input.GetAxisRaw("Horizontal") * speed;

     rigidbody.AddForce (Vector3.forward * Input.GetAxis("Vertical") * 50.0f);
Comment
Add comment · Show 4 · 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 Den D. · Jul 27, 2014 at 04:13 AM 0
Share

Thanks a lot this is all what I wanted...:D But there is a new issue that had arrived this code is working in excellent way when gravity is turn off and turning off the Gravity the cube doesn't jump.And keeping the Gravity on forces the cube to halt after traveling certain distance

avatar image robertbu · Jul 27, 2014 at 05:01 AM 0
Share

Start by setting a Physics material with little or no friction for colliders on both the object and the surface it rides on. If that is not enough, then set velocity ins$$anonymous$$d of doing AddForce(). You can do:

 vel = rigidbody.velocity;
 vel.z += Input.GetAxis("Vertical") * factor;

...where 'factor' is a value you tune to get things the way you want (assu$$anonymous$$g a linear progression works for you).

avatar image Den D. · Jul 27, 2014 at 05:41 AM 0
Share

This is the script that I am Using

 void Start(){
       // get the distance to ground
       distToGround = collider.bounds.extents.y;
     }
      
    bool IsGrounded() {
       return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1);
     }
      
     void Update () {
       if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space) && IsGrounded()){
         rigidbody.velocity = new Vector3 (0,jumpSpeed, 0);
         print("I am on");
       }
     }

This script is not working wothout gravity.

avatar image robertbu · Jul 28, 2014 at 12:39 AM 0
Share

I'm not sure what you want to do. The code I posted in my last comment will overcome the slown-down issue, even with gravity turned on. You can use an alternate definition for grounded that will work without gravity, but then you'd also have to include alternate to gravity to move your object back down after a jump.

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

23 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

Related Questions

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

Sum of multiple velocities 1 Answer

Rigidbody Character 1 Answer

rigidbody.velocity.normalized application 1 Answer

Why is velocity checking intensive? 0 Answers


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