- Home /
Anyone know how to code it so the target will respawn instead of carrying moving
I am making an aim trainer and i am making a moving targets task, the targets are hit because the score goes up but the target does not stop moving and will only respawn while being hit when the target is not moving. This is the code using System.Collections; using System.Collections.Generic; using UnityEngine;
public class targetshooter : MonoBehaviour {
public float clicks = 0;
public float shots_hit = 0;
public Camera fpsCam;
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
void Shoot()
{
clicks = clicks + 1;
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit))
{
Debug.Log(hit.transform.name);
target target = hit.collider.gameObject.GetComponent<target>();
if (target != null)
{
target.Hit();
shots_hit = shots_hit + 1;
}
}
}
} this is for the gun using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class target : MonoBehaviour { static float scores = 0;
[SerializeField] Text scoreText;
public void Hit()
{
transform.position = targetbounds.Instance.GetRandomPosition();
scores = scores + 1;
}
void Update()
{
scoreText.text = scores.ToString();
}
} this is for the target when hit using System.Collections; using System.Collections.Generic; using UnityEngine;
public class targetbounds : MonoBehaviour { public static targetbounds Instance;
void Awake()
{
Instance = this;
}
[SerializeField] BoxCollider col;
public Vector3 GetRandomPosition()
{
Vector3 center = col.center + transform.position;
float minX = center.x - col.size.x / 2f;
float maxX = center.x + col.size.x / 2f;
float minY = center.y - col.size.y / 2f;
float maxY = center.y + col.size.y / 2f;
float minZ = center.z - col.size.z / 2f;
float maxZ = center.z + col.size.z / 2f;
float randomX = Random.Range(minX, maxX);
float randomY = Random.Range(minY, maxY);
float randomZ = Random.Range(minZ, maxZ);
Vector3 randomPosition = new Vector3(randomX, randomY, randomZ);
return randomPosition;
}
} this get a random spawn for the target using System.Collections; using System.Collections.Generic; using UnityEngine;
public class moverandom : MonoBehaviour { private float movementDuration = 2.0f; private float waitBeforeMoving = 0.2f; private bool hasarrived = false; public bool Active = true;
void Update()
{
if (!hasarrived)
{
hasarrived = true;
float randX = Random.Range(-5.0f, 5.0f);
float randY = Random.Range(4.0f, 8.0f);
float randZ = Random.Range(-5.0f, 5.0f);
StartCoroutine(movetopoint(new Vector3(randX, randY, randZ)));
}
}
private IEnumerator movetopoint(Vector3 targetpos)
{
float timer = 0.0f;
Vector3 startpos = transform.position;
while (timer < movementDuration)
{
timer += Time.deltaTime;
float t = timer / movementDuration;
t = t * t * t * (t * (6f * t - 15f) + 10f);
transform.position = Vector3.Lerp(startpos, targetpos, t);
yield return null;
}
yield return new WaitForSeconds(waitBeforeMoving);
hasarrived = false;
}
} this is the code to move the target by getting a random location to move to. anyone know how to respawn the target instead of it moving. The movement script and hit script are a component on the object target.