Ray difficulties

I know that this has been discussed tons of times but after many many searches I still can’t figure out how to accomplish this. What I need to do is have a Ray going from my guys gun, a certain range down I want to have a crosshair drawn which follows the rays x and y coordinates at that point. I understand how to get the point on the Ray, but I cannot figure out how to set the origin and direction of the Ray, or how to get the crosshairs position to be at the points position, if anyone could point me in the right direction u would be very greatful. Thanks.

I am pretty good at this stuff as I have a working third person shooter and first person shooter with raycast and a crosshair where I can shoot my bullet from anywhere I want, the gun barrel, anywhere really, and it will always land where my crosshair is pointing. If this is what you want I can explain how to do it for you but I am not sure if this is what you want so I wont bother waisting tons of time trying to explain it unless this is what you want lol.

Well see I’m making an iPhone third person shooter, and so I don’t know how to control the position of the crosshair with swipe control, but move the gun and arm of the character is easy, so I was trying to get the crosshair to follow the Ray which would originate from the gun barrel, I would love to see your code though, because that would be a great alternative.

You dont have the ray originate from the gun you have it originate from the crosshair so for example:

				var point = new Vector3(512, 384, 0); 
				var transPoint = GUI.matrix.MultiplyPoint3x4(point); 
				var ray = Camera.main.ScreenPointToRay(transPoint); 
				var fwd = Camera.main.transform.TransformDirection (Vector3.forward);
				var hit: RaycastHit;
		
				if (Physics.Raycast(target.position,fwd, hit)) {
					var hitpoint = hit.point;
					transform.LookAt(hitpoint);
					var bullet1 = Instantiate(fireball, transform.position, Quaternion.identity);
					bullet1.transform.rotation = transform.rotation;
					bullet1.rigidbody.AddForce(transform.forward * 1000);

This code is placed on my spawnpoint where I spawn the bullets from, so the spawnpoint will always look at where the crosshair is. So for instance you could also use this to turn your character in the right way but I think you would have to limit it to one axis. Of corse you have to change the 512,384 to where your crosshair is, I have it set like that because I run in 1024by 768 and that is the middle so thats where I aim it from.
Let me know how this goes. or if I need to explain anything else.

This may not be what you are looking for but what you can do is get the position of the ray from your gun and then use a screenpointtocamera or something I will loot this up and get you the correct name.

Edit: its WorldToViewPortPoint

Oh and you can get this point from the code I listed above

I’m sorry, I just started with ray’s and advanced scripting (well advanced for me) and I don’t really know how to get the ray to point from my guns position, or get the WorldToViewPortPoint and use it in the script. Thank you for being patient with me =)

Let me ask you this, are you aiming with the crosshair on your screen or do you want the crosshair to be positioned where your gun is pointing and you are actually aiming with the character?

I would like the crosshair to be pointed where I am actually aiming with my character if possible.

      var target : Transform;
      function Update(){
            var fwd = target.transform.TransformDirection (Vector3.forward); 
            var hit: RaycastHit; 
       
            if (Physics.Raycast(target.position,fwd, hit)) { 
               var hitpoint = hit.point; 
               var bullet1 = Instantiate(fireball, target.position, Quaternion.identity); 
               bullet1.rigidbody.AddForce(transform.forward * 1000);

Ok this code here you can put anywhere really, what you do is link your gun into the target in the inspector. What this will do is cast a ray strait out from your gun. So you have to make sure your gun is modeled so that is is looking strait. After that you can use hit.point with WorldToViewPortPoint
to have your crosshair on your screen where the gun is pointing. Here is the documentation for WorldToViewPortPoint - Unity - Scripting API: Camera.WorldToViewportPoint
However I do not recommend this way because if your gun points out of the screens viewport it will not show the crosshair, You would have to constrain it or something maybe. I would personally use your crosshair to aim with and have the gun shoot where your crosshair is and have the character look at that point. You can take a look at the thirdpersonshooter example I think it may give some insight.

Thank you a lot for helping me, I will try your suggestion of controlling the crosshair instead and just have the character look at the crosshair and have the gun point at it. Thanks again =)

My guess is its a free floating crosshair like the one in zelda twilight princess that you want, if this is the case I think that the locomotion (I hope thats the right word) has something that points your character in the right direction. Im not a 100 % sure on this, but I remember seeing something like that when I was looking through Unity Examples. Maybe someone else can shed some light on this subject. Anyway good luck with the project, ps dont let me discourage you, if you are set on having the character aim then by all means go that way. I just forsee it being easier with aiming with the crosshair.

@unitrix, I tried your method of controlling the crosshair and having my character look at it, but I only want the top half of his body to follow it, and the bones get really deformed when I try to use lookat, is there any alternative to lookat? otherwise I’ll have to just keep going with my current method. Thanks.

Ok, I’ve been trying to get the script you gave me to work, but I’ve run into a problem, I’ve been trying to get the crosshair to show up at the hitpoint. Well when I play my game the crosshair is no where to be seen, so I clicked on the crosshair in the hierarchy and watched the x and y coordinates as I moved my player around, they were like in the really low negatives. so I kept walking around and finally found a place on the map where the coordinates were positive, then by jumping the crosshair would pop up onto the screen. from what I know, the raycast should shoot straight out from my object and if I rotate the object the raycast will be rotated with it correct? cause it doesn’t seem to be working like that. Anyway any help would be greatly appreciated. Thanks :slight_smile:

My code is below.

 var target : Transform; 
    var Projectile : Rigidbody;
    var Crosshair : Transform;
      function Update(){ 
            var fwd = target.transform.TransformDirection (Vector3.forward); 
            var hit: RaycastHit; 
        
            if (Physics.Raycast(target.position,fwd, hit)) { 
                           Crosshair.transform.position = hit.point;
                                       if (Input.GetKey("r")){
               var hitpoint = hit.point; 
               var bullet1 = Instantiate(Projectile, target.position, Quaternion.identity); 
               bullet1.rigidbody.AddForce(transform.forward * 10);
               }
               }
               }