- Home /
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!
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);
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!
You could make a Vector3 first, or just add 'var' in front. I edited the example code and added 'var'.
Yes! It worked! Thank you so much! I've been trying to get this working for over a week. Thanks again! :)
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
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.
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.
Thanks for the help. I was able to get it working :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613133900im_/https://answers.unity.com/themes/thub/images/avi.jpg)