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 bartleycollin · Feb 18, 2014 at 03:17 AM · rotationforceangle

How do I add force to an object based on my Camera's cureent Y rotation (C#)

The question explains it for the most part.

I'm currently storing my camera's Y rotation angle in a float.

What I want to do is to take the float, and add force to a different object, at that angle.

I have a feeling I need to use Quaternions, but I haven't the slightest on where to start with those.

Any help will be appreciated!

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
Best Answer

Answer by robertbu · Feb 18, 2014 at 03:47 PM

Here are two ways to approach this problem. You use the Camera's forward instead of the angle you saved. You would do something like:

 var dir = Camera.main.transform.forward;
 dir.y = 0.0;
 rigidbody.AddForce(dir.normalized * amount);

If you want to use your angle you can do it this way:

 var dir = Quaternion.AngleAxis(angle, Vector3.up) * Vector3.forward;
 rigidbody.AddForce(dir * amount);


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 bartleycollin · Feb 18, 2014 at 04:41 PM 0
Share

Thanks! I'll try this and see what happens!

avatar image bartleycollin · Feb 18, 2014 at 04:45 PM 0
Share

I decided to go with the second one, because I have to use the angle I stored. I tried it and it said "The name dir does not exist in this current context" Do I need to make a Vector3 called dir first? Thanks!

avatar image robertbu · Feb 18, 2014 at 04:56 PM 0
Share

You could make a Vector3 first, or just add 'var' in front. I edited the example code and added 'var'.

avatar image bartleycollin · Feb 18, 2014 at 06:06 PM 0
Share

Yes! It worked! Thank you so much! I've been trying to get this working for over a week. Thanks again! :)

avatar image
0

Answer by JeffreyD · Feb 18, 2014 at 03:24 AM

Here is a link to the script reference for AddForce. That may give you what you need. If not let me know. http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForce.html

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 bartleycollin · Feb 18, 2014 at 02:06 PM 0
Share

Thanks for the answer, but this doesn't really help. I know how to add force, but I don't know how to add force at a specific angle.

avatar image JeffreyD · Feb 18, 2014 at 03:42 PM 0
Share

This might help. http://answers.unity3d.com/questions/17012/how-to-get-local-angularvelocity.html

It talks about getting angular velocity but it also talks about AddForceAtPosition which may be what you need. http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForceAtPosition.html This looks to me as the way to go.

IF you want to stay with using the angle that you know then from what I understand about forces so far is that they are applied in terms of Vector3. So there are functions available to convert angles to transforms. If I understand them correctly you may able to use one of those. http://docs.unity3d.com/Documentation/ScriptReference/Transform-eulerAngles.html

A final thought is to use constantForce. http://docs.unity3d.com/Documentation/ScriptReference/ConstantForce-relativeForce.html

So given the above, I think you could take the angle you know. Convert it to a Vector3 then apply that to the different object via constatForce.RalativeForce.

I'll assu$$anonymous$$g that you do not want to rotate the different object to the known angle but simply apply the force at the angle. Yes?

I know this is all conceptual. Sorry I don't have any code to share. Hope it helps.

avatar image bartleycollin · Feb 18, 2014 at 06:06 PM 0
Share

Thanks for the help. I was able to get it working :)

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

20 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

Related Questions

Velocity bug with hinge joints forces in C# 0 Answers

Rotating sprite through touch (storing current rotation) 0 Answers

How can I multiply the rotation of a mirrored object? 1 Answer

Place object on radius pointing to a other object 1 Answer

How to add rotation force with playmaker? 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