I have made a Object that Rotates smoothly and follows you.
Now i want it to Shoot at you when you are in range.
ANy ideas?, I am new, so please any examples would be appreciated
I have made a Object that Rotates smoothly and follows you.
Now i want it to Shoot at you when you are in range.
ANy ideas?, I am new, so please any examples would be appreciated
adding Trigger to the Object(the “YOU” must had rigidbody component)?
tick the OnTriggerBox then scale the Triggerbox size to where you wan the range to be!
then search unity3d scripting for all the reference on OnTriggerEnter(),OnTriggerExit() and shoot at you will be using Instantiate
if you don know where is the scripting reference,it is at help ->scripting reference
There may be more than one way of doing it so my idea may not be the best ideal solution.
i’ll try it, thanks
I have a problem, When i get in range of the turret it fires too many balls, Probably 1 per frame due to it being in a update funtion?
Can someone give me a example on how to make it only shoot say… Twice a second?, I’m still new to this so the code below took me a while to come up with!
var target : Transform;
var prefabBullet : Transform;
var shootForce;
var range = 10;
function Update () {
if(Vector3.Distance(transform.position, target.position) < range)
{
Instantiate(prefabBullet, transform.position, Quaternion.identity);
prefabBullet.rigidbody.AddForce(transform.forward * shootForce);
}
}
There is a script in this thread that implements a simple timer class that you can use in the Update function to keep track of the time since the last shot occurred. Another option is to start a coroutine and use the WaitForSeconds class to introduce the delay. Yet another option is to use the InvokeRepeating function to call a script function regularly (this function would have essentially the same body as your Update function in the code you posted).