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
0
Question by RPMonkey · Nov 17, 2015 at 02:27 PM · transformvector3velocitydirection

is it possible for velocity vectors to be added on the players axis?

Hi, I have been using this line of code to add force to my rigidbody player.

 GetComponent.<Rigidbody>().velocity += Vector3.right * sideSpeed;

the only problem is that it only works in one particular direction. So, say if i was facing north, and i press a key that executes this function. And the player jumps right. If i was to face East, the command would shoot me backwards. Is there a way so that in any direction the player faces they will always jump to their right? Thanks.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by _dns_ · Nov 17, 2015 at 03:36 PM

Hi, use transform.right or transform.forward : those are vectors that are expressed in your object local space = always forward or right relative to your object, not absolute values in world space like Vector3.right.

And a reminder: always modify an object physics properties during FixedUpdate :)

Comment
Add comment · Show 7 · 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 _Game_Dev_Dude_ · Nov 17, 2015 at 05:23 PM 1
Share

Sure and Unity makes it very simple. Ins$$anonymous$$d of Vector3.right use Transform.right.

avatar image _dns_ _Game_Dev_Dude_ · Nov 17, 2015 at 05:39 PM 1
Share

(converted to a comment)

Well, that's not some Unity complexity here, that's how a hierarchy of 3D object works. Vector3.forward is a constant from the Vector3 class and it's expressed in world space. if you want that some force pushes an object in one direction regardless of it's own rotation, add Vector3.forward to it's velocity (like some wind force).

If you want the object to go "in front" of itself, you need to take in account it's rotation in the world, and the rotation of it's parents (if any). That's why you have to use the transform component of this object to compute his local forward vector. Note that's is "transform.forward", so the object's transform component's "forward" and not "Transform.forward" that would be a constant from the Transform class like Vector3.forward is.

avatar image RPMonkey · Nov 17, 2015 at 11:03 PM 0
Share

Thanks dude, this work brilliantly! I eventually figured out to go left would be to use -transform.right. Is it possible to do this for the command rigid body .AddForce? Like addforce to the right / left? Thanks guys

avatar image _dns_ RPMonkey · Nov 17, 2015 at 11:13 PM 1
Share

yes, using Rigidbody.AddForce is a more abstract way than modifying velocity. You just don't need to multiply forces by deltaTime (ho, just realize that you may not do that yet, so better use AddForce to be framerate-proof). Check the different Force$$anonymous$$ode you can use in the documentation to get the result you want.

avatar image RPMonkey _dns_ · Nov 17, 2015 at 11:55 PM 0
Share

Would something like this work?

 gameObject.rigidbody.AddForce(transform.right * force)
Show more comments
avatar image RPMonkey · Nov 18, 2015 at 06:41 AM 0
Share

thanks dns, you have been such a help for me. The way you answer questions comes across in a very polite manner. Thanks for putting up with me!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Increase velocity of gameObject every time it spawns? 1 Answer

Applying direction into transform position 0 Answers

Rigidbody Velocity in negative direction 0 Answers

how to make the player move in only 2 directions? 0 Answers

Vehicle throttle cut with negative forward velocity 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