I need help making a script that would make my object flee from the player. What I mean by this is when the player gets close enough to the object, the object will start moving away slowly from the player. I also need to make sure the the object doesn’t float in the air. I tried to fix this by adding a rigidbody and turned on gravity. For some reason it made my object move incredibly fast and fly off my terrain and before it did not do this.
So what I need is:
-
Object flee from player
-
Not float in the air/use gravity
-
Face opposite rotation of player (objects back is towards the player)
My script that I have been using to test movement with my object is this, which follows the object instead of fleeing because it follows rotation with the player.
var Distance;
var Target : Transform;
var lookAtDistance = 25.0;
var attackRange = 15.0;
var moveSpeed = 5.0;
var Damping = 6.0;
function Update ()
{
Distance = Vector3.Distance(Target.position, transform.position);
if (Distance < lookAtDistance)
{
GetComponent.<Renderer>().material.color = Color.yellow;
lookAt();
}
if (Distance > lookAtDistance)
{
GetComponent.<Renderer>().material.color = Color.green;
}
if (Distance < attackRange)
{
GetComponent.<Renderer>().material.color = Color.red;
attack ();
}
}
function lookAt ()
{
var rotation = Quaternion.LookRotation(Target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}
function attack () //Flees
{
transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
}