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 /
  • Help Room /
avatar image
1
Question by Doctor_Uair · Sep 04, 2015 at 01:09 AM · movement scriptrigidbody.addforce

Can you move a rigid body object forward without using relative force? If so, how?

I'm trying to make an object that moves along it's local Z axis but when I use AddRelativeForce it ends up sliding all over the place when I turn the object. I want a character to stop the second the walk key is released and to turn sharply, similar to games like Wizard 101. I'm using C#, here's my code so far:

using UnityEngine; using System.Collections;

public class character_controller_script : MonoBehaviour { public Rigidbody playerRB; public float playerWalkSpeed; public CapsuleCollider playerCapsuleColider; public float playerJumpPower;

 void Start(){
     playerRB = GetComponent<Rigidbody> ();
     playerCapsuleColider = GetComponent<CapsuleCollider> ();
     playerWalkSpeed = 8.0f;
 }
 void Update(){
     float velocity = Input.GetAxis ("Vertical");
     float rotation = Input.GetAxis ("Horizontal");
     Vector3 movement = new Vector3 (0.0f, 0.0f, velocity);
     playerRB.AddRelativeForce (playerWalkSpeed * movement); //This is what I need to get rid of, but how?
     playerRB.transform.Rotate (Vector3.up * rotation);
 }

}

I have the camera, the player object, and everything else inside the same game object so I don't need to worry about anything else right now, I just need to stop having the character drift.

!!!EDIT 9/4/2015!!!

I also need it to move forward and back on it's local axis, not it's global axis. The rotation is there to let the player turn, along with the camera, so don't worry about the character moving left and right, that isn't happening, just turning.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by CaptainKirby · Sep 04, 2015 at 11:38 AM

Give Rigidbody velocity a go. It is a direct manipulation, so you dont get stuff like acceleratio given to you like with addforce etc. though. If you just want it to be straight up instant then it is a good choice.

 inputDir.x = player.GetAxis("Move Horizontal");
 inputDir.z = player.GetAxis("Move Vertical");
 
 GetComponent<Rigidbody>().velocity = inputDir * speed; 
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 Doctor_Uair · Sep 05, 2015 at 12:58 AM 0
Share

I was just about to try this but whenever I type inputDir it shows up as an error. Do I need a new component? Or would a float work?

avatar image Doctor_Uair · Sep 05, 2015 at 01:06 AM 0
Share

I forgot to mention that it needs to go on it's local axis, not the global axis, so velocity won't work. Thanks for the quick reply though.

avatar image CaptainKirby · Sep 05, 2015 at 08:47 AM 0
Share

in what axis it moves(local or glabal) makes no difference, it is simply a means of movement, like addforce and translate.

the inputDir is just a Vector3 variable.

However, for ease for you: Change this:

 playerRB.AddRelativeForce (playerWalkSpeed * movement);

to:

 playerRB.AddRelativeForce (playerWalkSpeed * movement, Forcemode.VelocityChange);

this will change the speed instantly. For more about forcemodes check out http://docs.unity3d.com/ScriptReference/Force$$anonymous$$ode.html

Also this thread has a more in depth explanation of the difference between addforce and velocity http://forum.unity3d.com/threads/addforce-vs-addrelativeforce-vs-rigidbody-velocity.32808/

avatar image Scribe · Sep 05, 2015 at 10:57 AM 0
Share

You could use the axes of the object to direct it!

 Vector3 inputDir = Vector3.zero;
 
 inputDir.x += transform.right*player.GetAxis("$$anonymous$$ove Horizontal");
 inputDir.z += transform.forward*player.GetAxis("$$anonymous$$ove Vertical");    
 playerRB.velocity = inputDir * speed; 



avatar image
0

Answer by zabhinav · Apr 27, 2016 at 04:30 AM

rigidbody.AddForce(vector3.forward);

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

29 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

Related Questions

Need help for applying force to rigidbody with .AddForce 0 Answers

Rotate y-axis of child object following a rigidbody sphere parent 0 Answers

Cricket Ball Physics? 1 Answer

Speed limit to AddForce (without limiting velocity of rigidbody) 0 Answers

Object Movment problem (3D) 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