- Home /
How to get a Vector3 from a RaycastHit variable?
I'm making an FPS and right now I'm working on making the gun aim in the middle of the screen. I'm trying to achieve this by casting a ray from my camera and making my bullets look at that target. I know how to code everything except how to get the point at which my raycast is hitting any gameobject. Here's my code so far, I didn't attempt to get a Vector3 from the ray, I just tried to see if I could get a value from the rayPoint variable? Thanks!
//casts a ray to find a point in the middle of the screen for the bullet to aim at, so aiming is easier
//the length of the ray var distance : int;
function Update () {
//makes a ray and gives back the point it is hitting
//this is the point in space the bullet will look at
var rayPoint : RaycastHit;
//a direction
var fwd = transform.TransformDirection(Vector3.forward);
//casts the ray
if(Physics.Raycast(transform.position, fwd, rayPoint, distance)) {
if(rayPoint.GameObject) {
Debug.Log("works");
}
}
}
P.S. I'm working on making good comments throughout scripts, if you can, can you tell me if I did a good job commenting the script?
Answer by rabbitfang · Feb 18, 2012 at 01:05 AM
The Vector3 of the point that the raycast hit will be rayPoint.point
.
Read this for further information.
@Posly, $$anonymous$$ake sure to mark the question as answered then....
Your answer
Follow this Question
Related Questions
How to get information from raycast? 2 Answers
Raycast returns null for no apparent reason 0 Answers
Raycasthit.point - same place every time! (Unity 3.5) 1 Answer
[SOLVED] Raycast for custom button JS 1 Answer
Need Help with RayCast. No Vector? 1 Answer