- Home /
Raycast Targeting Reticle.
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?
Answer by Xelareip · Nov 18, 2013 at 04:37 PM
RaycastHit hitInfo = new RaycastHit();
if (Physics.Raycast(transform.position, fwd, 5, hitInfo))
{
Vector3 impactPoint = hitInfo.point;
// Create your ring at impactPoint
}
The RaycastHit object passed in parameter should give you all the info you need, especially the position where the ray hit something. You can then move your ring at that position.
I'll implement this and see how it works, thanks man, if it works I'll thumbs up your answer definently.
The 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. $$anonymous$$aybe 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;
void Reticle ()
{
Vector3 fwd = BulletSpawn.transform.TransformDirection(Vector3.up);
RaycastHit hitInfo = new RaycastHit();
Vector3 showPos;
if (Physics.Raycast(transform.position, fwd, 5, hitInfo))
{
showPos = hitInfo.point;
}
else
{
showPos = transform.position + fwd * 5.0f;
}
Instantiate(Reticle, showPos.position);
Debug.DrawRay(BulletSpawn.transform.position, fwd, Color.green);
}
Derp, I am failing, I need sleep.
Edit: I didn't really explain, I can't figure out how to pull transform from showpos >.< I will probably be able to figure it out in the morning.
Ok now I'm running into these errors.
Assets/Scripts/Weapon/PistolController.cs(42,25): error CS1502: The best overloaded method match for `UnityEngine.Physics.Raycast(UnityEngine.Vector3, UnityEngine.Vector3, float, int)' has some invalid arguments
Assets/Scripts/Weapon/PistolController.cs(42,25): error CS1503: Argument `#4' cannot convert `UnityEngine.RaycastHit' expression to type `int'
The current script is:
void Reticle ()
{
Vector3 fwd = BulletSpawn.transform.TransformDirection(Vector3.up);
RaycastHit hitInfo = new RaycastHit();
Vector3 showPos;
if (Physics.Raycast(transform.position, fwd, 5, hitInfo))
{
showPos = hitInfo.point;
}
else
{
showPos = transform.position + fwd * 5.0f;
}
reticle.transform.position = showPos;
Debug.DrawRay(BulletSpawn.transform.position, fwd, Color.green);
}
Hmm, 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.
Your answer
Follow this Question
Related Questions
Why are my guns shooting lower when my character moves forward? 1 Answer
Gun script using Physics.Raycast not working? 2 Answers
How to send a raycast to an object to give me ammo? 0 Answers
Unity c# Raycast axis stuck on one axis 0 Answers
Raycast gun 2 Answers