- Home /
Return to origional position.
I am working on an AI script and when the item ths spaceship is chasing leaves its collider I want the AI to return back to its original position. Also I am trying to find out if there is a way to make it not chase after it goes so far from original position.
Here is my script so far.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class EnemyAi : MonoBehaviour
{
//public Transform target;//Targets.
public int moveSpeed;//Speed enemy can move.
public int rotationSpeed;//Speed enemy can rotate.
public List<Transform> targets = new List<Transform>();//List of targets.
public int maxDistance;//Max distance it should get to us.
public bool movingEnemy;
public GameObject ThrusterLeft;
public GameObject ThrusterRight;
private Transform myTransform;//Save for the transform.
void Awake()
{
myTransform = transform;//This saves our transform so we dont have to look it up all the time.
}
void Update ()
{
if(targets.Count < 1)
{
if(movingEnemy)
{
ThrusterLeft.SetActive(false);//If there is no more enemys turn off thruster.
ThrusterRight.SetActive(false);//If there is no more enemys turn off thruster.
}
return;//If there is no targets sets targets to 0.
}
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(targets[0].position - myTransform.position), rotationSpeed * Time.deltaTime);//Makes it look at us using the speed we set over time.
if(movingEnemy)
{
if(Vector3.Distance(targets[0].position, myTransform.position) > maxDistance)//Prevents the enemy from siting on top of us.
{
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;//Move the enemy towards us.
ThrusterLeft.SetActive(true);//Turn on thrusters for chase.
ThrusterRight.SetActive(true);//Turn on thrusters for chase.
}
else
{
ThrusterLeft.SetActive(false);//turn off thrusters we are not chasing anything.
ThrusterRight.SetActive(false);//turn off thrusters we are not chasing anything.
}
}
}
void OnTriggerEnter(Collider other) //When somthing enters the trigger.
{
if(other.tag == "Player")//Check if it was tagged player.
{
targets.Add(other.transform);//If so add it to the targets list.
}
}
void OnTriggerExit(Collider other) //When somthing exits the trigger.
{
if(other.tag == "Player")//Check if it was tagged player.
{
targets.Remove(other.transform);//If so remove it from the targets list.
}
}
}
I am trying to do it be telling it to go to an empty game object but it does it instantly ins$$anonymous$$d of over time.
Answer by flaviusxvii · Aug 21, 2013 at 06:28 PM
Look at the example code here: http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Lerp.html
I'm not saying you have to use Lerp, but look at how they make it move over time.
For some reason it is still just jumping back instantly.
myTransform.position = Vector3.Lerp (myTransform.position, oldPosition.transform.position, moveSpeed * Time.deltaTime);
Read the docs again...
The 3rd argument takes a value between 0 and 1. 0 for the start position. 1 for the end position. speed * deltaTime is not even CLOSE to what you want...
Answer by Alpha_Guac · Oct 19, 2015 at 10:36 AM
have you tried making a transform that you set as the initial transform and set the enemy transform as the initial when you want it to return. as for not letting the AI move to far, have you tried using an if statement that compares the enemies transform to the initial transform and if it's too far it goes back to the initial position
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How can I more optimize My AI Script 0 Answers
How can I get a character to patrol and follow terrain? 1 Answer
Basic 2d AI follow? 1 Answer