x


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?

more ▼

asked Feb 18 '12 at 12:59 AM

Posly gravatar image

Posly
391 35 42 44

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

The Vector3 of the point that the raycast hit will be rayPoint.point.

Read this for further information.

more ▼

answered Feb 18 '12 at 01:05 AM

rabbitfang gravatar image

rabbitfang
932 2 4 11

Thanks a bunch!

Feb 18 '12 at 01:24 AM Posly

@Posly, Make sure to mark the question as answered then....

Feb 18 '12 at 01:25 AM rabbitfang
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1578
x166
x155
x69
x6

asked: Feb 18 '12 at 12:59 AM

Seen: 855 times

Last Updated: Feb 18 '12 at 01:25 AM