Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 xKriostar · Sep 22, 2017 at 06:16 PM · rigidbodycharacter controllerthird person controller

How to make rigidbody addforce locally

I know that rigidbody.addrelativeforce is supposed to be what I'm looking for, but when I use it, it still does not work, but instead, sends my character going flying.

I am making an 8 direction character controller that rotates the character in the direction that it is walking. It rotates the character based on the keyboard inputs and camera rotation that works perfectly but my character walks in the same directions.

Here is a gif of what is happening

Here is the section that I use to move my character: Vector3 velocity = rigidBody.velocity;

         Vector3 movementForce = new Vector3(Input.GetAxisRaw("Horizontal"), 0.0f, Input.GetAxisRaw("Vertical"));
         rigidBody.AddForce((movementForce * speed) - velocity, ForceMode.VelocityChange);
 
         // Rotate character only if they are moving
         if (rigidBody.velocity != Vector3.zero)
         {
             // Rotate character to the direction that it is moving + the rotation of the camera
             float angle = Mathf.Atan2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")) * Mathf.Rad2Deg;
             angle += mainCamera.transform.localEulerAngles.y;
             this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.Euler(0, angle, 0), 0.15f);
         }


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 hexagonius · Sep 22, 2017 at 07:49 PM 0
Share

From the looks of the gif I would say using Forces is not the right approach here. Just altering the velocity will give you a much better result in terms of character control. At least if you want to be able to immediately change direction from and to any direction.
Second, in the code you're actually using AddForce, not AddRelativeForce,, is that the correction already?.
Third, subtracting the current velocity results in incorrect behaviour because the velocity will accumulate on the rigidbody and therefore reach a point where its greater than the force you're trying to pass in, meaning you're always navigating in an opposite kind of way.
And finally, but I'm not sure about this one, rotating the rigidbody (teleport) you just applied forces to might also rotate the force in the new direction, but I'm not sure if that's so AND if that's intended.

avatar image unit_nick · Sep 23, 2017 at 08:33 AM 0
Share

I think you are forgetting momentum. You are adding the force as you intend. What you are not doing is adding an opposing force to stop drift. Essentially Pikachu is driftin' bra. $$anonymous$$ad skilz.

So if you want to stop that behavior while using physics add an equal & opposite force to the drift.

 float velocity = Vector3.Dot(rigidbody.velocity, transform.right);
 float force = rigidbody.mass * velocity / Time.fixedDeltaTime; // Time.fixedDeltaTime assumes using FixedUpdate
 rigidbody.AddForce(transform.right * -1f * force);

@x$$anonymous$$riostar Yet another edit :D

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by IgorAherne · Sep 23, 2017 at 09:25 AM

you can express the forward vector (or any other direction local to the character) of a character like this:

 Vector3 forceRelativeToWorld = myCharacter.transform.localToWorldMatrix.MultiplyVector(Vector3.forward);

You can then use this vector as a directon for your rigidbody force

Comment
Add comment · Show 1 · 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 xKriostar · Sep 25, 2017 at 06:31 PM 0
Share

That doesn't seem to help me solve my issue. At least, I don't know how to implement it to solve my issue. $$anonymous$$y character rotates in the direction that it moves, the problem I am having with my code above is that the character moves in the same direction no matter which way I am rotated

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

93 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 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 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 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 keep rigidbody from falling through terrain? 2 Answers

Character Controller pushing other rigidbodies 0 Answers

Colliders Not Working 1 Answer

Navmeshagents, Character Controllers and Colliders 1 Answer

Slope Limit on Rigidbody First Person Controller 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