- Home /
Clearing a destination...
How do I have my AI script stop moving towards its way-point while still moving forward? I'm trying to make my AI run in fear when clicked on. Right now, it runs, but he will make a large circle in order to keep going for the way-point.
I just tried replacing variable dir with "transform.forward speed Time.deltaTime;" when mouse clicks object but its not working.
The Full AI code:
var speed = 6;
var deer = 10;
var tap = 100;
var rotate;
var target : Transform;
private var waypoint : int;
//Movement Script
function Awake(){
}
function Update () {
var dir = (target.position - transform.position).normalized;
var hit : RaycastHit;
if(Physics.Raycast(transform.position, transform.forward, hit, 15)){
if(hit.transform != transform){
dir += hit.normal * 50;
}
}
var leftR = transform.position;
var rightR = transform.position;
leftR.x -= 2;
rightR.x += 2;
if(Physics.Raycast(leftR, transform.forward, hit, 8)){
if(hit.transform != transform){
dir += hit.normal * 50;
}
}
if(Physics.Raycast(rightR, transform.forward, hit, 8)){
if(hit.transform != transform){
dir += hit.normal * 50;
}
}
var rot = Quaternion.LookRotation(dir);
transform.rotation = Quaternion.Slerp(transform.rotation, rot, Time.deltaTime);
transform.position += transform.forward * speed * Time.deltaTime;
//Destroy Object at Destination
if (target) {
var dist = Vector3.Distance(target.position, transform.position);
if(dist <= 5){
Destroy(this.gameObject);
}
}
}
//Mouse Events
function OnMouseEnter() {
LevelController.Points += deer + 10;
}
function OnMouseDown () {
LevelController.Points += tap + 100;
speed = 25;
ChangeDirection();
yield WaitForSeconds (60);
Destroy(this.gameObject);
}
//Random Direction Script
function ChangeDirection() {
rand = Random.Range(2,-2);
switch(rand){
case -2:
rotate = -135;
break;
case -1:
rotate = -45;
break;
case 1:
rotate = 45;
break;
case 2:
rotate = 135;
break;
default:
rotate = 0;
break;
}
if(rotate!=0){
transform.Rotate(Vector3.up, rotate);
}
}
Answer by mirswith · Nov 01, 2011 at 09:48 PM
The script above looks like it is creating a direction to run towards 'target'. Using this script you would need to change the target of where you want the game object to move towards.
That seems correct - so to force it to go forward all you'd need to do is set
dir = transform.forward;
Then do your avoidance check..
function Update () {
var dir = (target.position - transform.position).normalized;
var hit : RaycastHit;
if(scared)
dir = transform.forward;
if(Physics.Raycast(transform.position, transform.forward, hit, 15)){
if(hit.transform != transform){
dir += hit.normal * 50;
}
}
"dir" should already be the forward vector towards "target". If you wanted to run away from target then you could add a slightly different version of your addition:
if( scared ) dir = -dir;
Oh what I understood was he just wanted it to keep running forward in it's current direction.. so the dir = transform.forward would remove any adjustment made to move towards the target and just maintain the current trajectory.
Good catch, I think your right now that I re-read it. We are missing part of the picture here since it looks like this class does not update the transform itself. If he does not want to move toward the target anymore in the higher level code he could simply disconnect this script and install a new "run away" script.. :)
I did want to have the AI move forward in the direction the model is currently facing. I tried putting "transform.forward" in the function On$$anonymous$$ouseDown but it will not stop moving towards the way-point. Ideas? I was thinking of putting a way-point as a part of the prefab that the character is made of. Then change the way-point to that one to where it just acts like a carrot on a stick.
Your answer
Follow this Question
Related Questions
Enemy Damage 2 Answers
Stay Back! 2 Answers
Shooting Damage Help 1 Answer
Store temp var / invert var 1 Answer