- Home /
Locking onto a enemy until its dead
I made a script which looks at an enemy and shoots at it as long as i hold the mouse button onto it, but i cant figure out how to make it lock onto the enemy only by clicking once and then let it go when the enemy dies.
Player code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class PlayerController : MonoBehaviour
{
public Camera cam;
public NavMeshAgent agent;
public float damage = 10f;
public float range = 100f;
public float rotspeed = 10f;
public GameObject Gun;
public Quaternion targetRotation;
public float impactForce = 10f;
public float firerate = 0f;
private float nextTimeToFire = 0f;
public ParticleSystem flash;
public GameObject impact;
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0) )//&& Time.time >= nextTimeToFire)
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.gameObject.layer == LayerMask.NameToLayer("Default"))
{
agent.SetDestination(hit.point);
}
else if (hit.transform.gameObject.layer == LayerMask.NameToLayer("Enemy"))
{
targetRotation = Quaternion.LookRotation(hit.point - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotspeed * Time.deltaTime);
Shoot();
}
}
}
}
void Shoot()
{
if (Time.time >= nextTimeToFire)
{
flash.Play();
nextTimeToFire = Time.time + 1f / firerate;
RaycastHit hit;
if (Physics.Raycast(Gun.transform.position, Gun.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent<Target>();
if (target != null)
{
target.TakeDamage(damage);
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * impactForce);
}
GameObject impactGO = Instantiate(impact, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactGO, 1f);
}
}
}
}
Enemy code:
public class Target : MonoBehaviour
{
public float hp = 50f;
public void TakeDamage (float amount)
{
hp -= amount;
if (hp <= 0f)
{
Die();
}
}
void Die()
{
Destroy(gameObject);
}
Comment
Best Answer
Answer by The_Three_Vs · Apr 01, 2020 at 09:24 PM
You can add a trigger boolean and store values for the enemy:
bool targeting = false;
GameObject targetEnemy;
Transform targetPoint;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0) )//&& Time.time >= nextTimeToFire)
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.gameObject.layer == LayerMask.NameToLayer("Default"))
{
agent.SetDestination(hit.point);
}
else if (hit.transform.gameObject.layer == LayerMask.NameToLayer("Enemy"))
{
targeting = true;
targetEnemy = hit.transform.gameObject;
targetPoint = hit.point;
targetRotation = Quaternion.LookRotation(hit.point - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotspeed * Time.deltaTime);
Shoot();
}
}
}
else if(targeting)
{
if(targetEnemy.GetComponent<Target>().hp < 0f)
{
targeting = false;
targetEnemy = null;
targetpoint = null;
}
else
{
targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotspeed * Time.deltaTime);
Shoot();
}
}
}
Hope this helps!
Thanks, that helped!But you should keep in $$anonymous$$d the the targetPoint is a vector and cant be null.