Sign in to follow this  

Unity UnityScript how to I get vector of pointing at object or camera aim

Recommended Posts

PointInSpace    102

I am using




This should point camera of First person Object to look at the other object.  How do I get the vector representing this so that I can send a raycast in same direction?   Also in general how to I get the camera of the FPO ?  How do I get the vector pointing at another object?






Share this post

Link to post
Share on other sites
kburkhart84    3182

Well, assuming the call to lookAt() works fine, then right afterwards, you should be able to use transform.forward, which is supposed to give you a world vector that is the current forward vector of that gameObject's transform.


Also, what is FPO?


And the last question, the difference(subtraction) will give you a vector that points from one object to the other, with so obj1's position - obj2's position would give a vector that points from obj2 to obj1.  But then you may want to normalize that vector as well unless you want the actual distance to matter, and if you want the distance for something, you just get the length of that vector after the subtraction.


I recommend you look up a bit of vector math.  I'm sure there is an article somewhere here on gamedev, and you could also use this section of the Unity docs to familiarize yourself as well.




I just noticed that FPO is "first person object..."


If the code is in a script on the same gameObject as the camera, then you can use the dot operator with the "camera" variable to access that.  If it is on another object, you'll need to first access that other gameObject.  You may have to set up a public variable in that script, and then drag&drop the camera gameobject into that variable.  Or, you could have a private variable in that class, and then in the start or awake function, use some variation of the findObject***() function to get that other object, likely via the tag.

Edited by kburkhart84

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this