Basically I have some guns, you can switch between them, and shoot them, and I have them creating a raycast down the barrel so that when its done it will render a reticle at 5 meters out or on the object in front of the gun, (It’s a third person shooter, thus a reticle in the middle of the screen simply will not do.) All in all it’s going quite well, but I hit a snag, I have a model (just a simple ring) and I want to have it be spawned wherever the ray ends, be that an object or just floating in the air at the end of the ray.
the script for this part of the guns pretty simple:
void Reticle ()
{
Vector3 fwd = BulletSpawn.transform.TransformDirection(Vector3.up);
if (Physics.Raycast(transform.position, fwd, 5))
{
print("There is something in front of the object!");
}
Debug.DrawRay(BulletSpawn.transform.position, fwd, Color.green);
}
If you want the hole script I can pull that from my files. Any thoughts guys?
I'll implement this and see how it works, thanks man, if it works I'll thumbs up your answer definently.
– BonkaheThe above code will give you the position of the hit, but if it does not hit something, you need to add code to calculate the position. Maybe something like: RaycastHit hitInfo = new RaycastHit(); Vector3 showPos; if (Physics.Raycast(transform.position, fwd, 5, hitInfo)) { showPos = hitInfo.point; } else { showPos = transform.position + fwd * 5.0f; } // Place your ring at showPos;
– robertbuHmm, that's my bad, the order of the parameters is wrong : Physics.Raycast(transform.position, fwd, hitInfo, 5.0f) Also, since the distance parameter is a float, 5.0f is better than 5. -> http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html
– XelareipYou're welcome :) Don't forget to accept the answer and close the subject.
– XelareipRoger Roger, I accepted it, but I'm not sure how to close it.
– Bonkahe