- Home /
Shoot nearest enemy with raycast
I'm making a game where the player is driving a car with a mounted gun and is being chased by other cars with guns. (Sounds dumb i know) Anyway, you won't be able to aim the gun very well so I want it to work in a way that allows you to shoot the nearest enemy by pressing the shoot button and for you to never miss. So essentially homing bullets. This will be the only way to make the game playable. This script is what I have so far but I'm really new to coding and I've gotten stuck. Thanks.
public class Gun1 : MonoBehaviour {
public float lookRadius = 10.0f;
public Transform target;
public GameObject gun;
public ParticleSystem muzzleFlash;
public GameObject projectile;
// Update is called once per frame
void Update()
{
float distance = Vector3.Distance(target.position, transform.position);
if (Input.GetButtonDown("Jump"))
{
Shoot();
}
if (Input.GetButtonDown("Jump"))
{
Instantiate(projectile, gun.transform.position, Quaternion.identity);
}
if (distance <= lookRadius)
{
Vector3 direction = (target.position - transform.position).normalized;
Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f);
}
}
void Shoot()
{
Instantiate(muzzleFlash, gun.transform.position, gun.transform.rotation);
Destroy(this.muzzleFlash, 0);
RaycastHit hit;
if (Physics.Raycast(gun.transform.position, gun.transform.forward, out hit))
{
Debug.Log(hit.transform.name);
}
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.blue;
Gizmos.DrawWireSphere(transform.position, lookRadius);
}
}
Answer by rhapen · Oct 27, 2020 at 11:57 PM
Few things, Raycast should happen in FixedUpdate and input in the Update. the way you are running it the Raycast has big chance to miss. FixedUpdate is running the physics and can be slower than Update therefore they might not happen in sametime http://docs.unity3d.com/Manual/ExecutionOrder.html
so you can do something like
bool shoot =false;
void Update(){
if (Input.GetButtonDown("Jump"))
{
shoot = true;
}
}
void FixedUpdate(){
Ray ray = Camera.main.ScreenPointToRay(lookPosition); //i am using the new input system here, you can use probably use the gun.transform.position, gun.transform.forward
RaycastHit hit;
Debug.DrawRay(ray.origin,ray.direction, Color.red);
if (Physics.Raycast(ray, out hit, 15f))
{
if(shoot) do_something;
}
}
@rhapen Thanks so much for responding (You've given many helpful answers to my questions), however, I am extremely new to Unity and C# so I'm going to have to ask for you to break this down a little for me. I see that you replaced the Shoot function with a boolean. Why is that? Could I just insert my current Shoot function into the fixed update. Also:
if (Physics.Raycast(ray, out hit, 15f)) { if (shoot) do_something; }
could you explain this bit please. Is this just where I tell the raycast to do something when/if it hits something? Thanks a lot!
@jamesorion44 the ray is what you want to look at out hit is what you hit and th 15 is maximum lenght of the ray. Change it to whatever you like.
Then you need ofcource change do_something to function or commands you want to execute for example dosomething(hit. collider);
And create the function void dosomething(Collider coll) { if(coll.tag == "EnemyHead"){
Debug.Log("HeadHit!!");
}
}
Your answer
Follow this Question
Related Questions
raycast in direction of movement key down 2 Answers
I need help with dragging objects 1 Answer
How to trigger scripts with line sight. (3D & First-Person) 1 Answer
2D blade to 3D 0 Answers