- Home /
I'm trying to shoot a raycast then if there is collision detection shoot a bullet in that direction. I have no Idea where to start
void Firing() { RaycastHit hit; if(Physics.Raycast(fpscam.transform.position, fpscam.transform.forward, out hit)) { Debug.Log("good"); } }
Good start, now you just need to spawn a bullet (you can set the bullet's rotation equal to your fps camera's rotation). Is your raycast working like you want it to? Do you need help spawning a bullet? Do you need help creating a bullet that can be shot?
Answer by theportalmasters · May 29, 2020 at 04:41 PM
You seem to have most of the work done already for a hitscan weapon. Hit has a property collider that will return the object hit by the ray cast. If all you want to do is damage the thing the gun is pointing at when the trigger is pulled, take that collider.GetComponent().Damage or equivalent. If you really want a bullet for some reason, your best bet to start with is Instantiating a prefab Gameobject, then setting its rotation to the direction of the ray cast, and giving it a large forward velocity, assuming the prefab has a forward velocity.
if(Physics.Raycast(fpscam.transform.position,fpscam.trasform.forwad, out hit))
{
GameObject bullet Instantiate(myBullet, fpscam.transform);
bullet.transform.rotation.SetLookRotation(fpscam.transform.forward);
bullet.GetComponent<Rigidbody>().AddForce(100, 0, 0);
}
Hopefully, that's enough to get you started. good luck!
Your answer
Follow this Question
Related Questions
Jarring "snap" on 2d Camera raycast collider 0 Answers
Raycast passes Collider 0 Answers
AI bug issue 0 Answers
Rigidbody 3D Sticking issue 2 Answers
Raycast goes through collider 2 Answers