- Home /
Problem with OnTriggerEnter
What I'm trying to do is have the object this script is attached to move toward it's target when the player enters it's trigger. It actually STARTS to work, but the object stops moving instantly after it starts. Meaning it just moves about an inch then stops. This is a script from the forums that I've slightly modified for what I need. Please note that I'm still a beginner at scripting, and after hours of frustration, I still can't get this object to move towards the target when the player enters it's trigger. Can someone please tell me what I'm doing wrong here?
var player : GameObject;
var speed : float = 1;
function Start () { player = GameObject.FindGameObjectWithTag("CarrieTarget1");
if (!player)
Debug.Log ("ERROR could not find Player!");
}
function OnTriggerEnter (other : Collider) {
if (!player)
return;
var distance = Vector3.Distance( player.transform.position, transform.position);
if ( distance < 100 )
{
Debug.Log ("player is close");
if ( distance < 1 )
Destroy (gameObject);
var delta = player.transform.position - transform.position;
delta.Normalize();
var moveSpeed = speed * Time.deltaTime;
transform.position = transform.position + (delta * moveSpeed);
}
else
{
Debug.Log("not close yet " + distance);
}
}
Answer by fafase · Apr 25, 2012 at 04:07 PM
var player : GameObject;
var target: Transform;
var controller: CharacterController;
var speed: int = 5;
var rotSpeed: float = 5;
var going:boolean;
function Start(){
controller = GetComponent(CharacterController);
player = GameObject.Find("Player");
going = false;
target.position = Vector3(0,0,0); // here put the position of where you want your guy to go
}
function Update(){
// The player is within at less than 10m but more than 5m
if(Vector3.Distance(transform.position, player.transform.position) < 10)
{
going = true;
}
// Carrie goes.
if(going){
var forward : Vector3 = transform.TransformDirection(Vector3.forward);
transform.rotation=Quaternion.Slerp(transform.rotation,Quaternion.LookRotation
(target.transform.position-transform.position),rotSpeed * Time.deltaTime);
controller.SimpleMove(forward * speed);
}
}
@script RequireComponent(CharacterController)
and tadaaa. Now your guy will only move if the player/object is within 10m and will follow him until the object is at a distance of more than 10m.
It won't move the rest of the time. and actually var forward could be inside the if...You also need a character controller on your NPC.
But I don't need the object to follow the player, I just need it to move to the empty game object. The moving object is actually a character named Carrie. All I want is for Carrie to move to her target when they player gets close. (The player is NOT the target. The only role I need the player to have in this, is to trigger Carrie to move to her target when they get close.) I don't need for Carrie to follow the player at all, I only need her to move to her target when the player approaches her. It seems like this would be a pretty simple script, but even after 2 days of trying, I still can't get one to work properly.
Ok now Carrie is waiting for the guy and when he's done and passing less than 10m, Carrie is going to (0,0,0), here you need to pass the right position. Is that it?
Yes, exactly. I just need to make sure carrie actually smoothly moves to the position, rather than just teleporting.
What you can do is remove the target.position=Vector3(0,0,0); and ins$$anonymous$$d in the inspector you drag the object you want Carrie to go to. If you have an animation you can play it as well. She won't be teleporting, she will be looking at the obejct and move towards it. You coul add a Time.deltaTime to make it machine independant.
Answer by fafase · Apr 25, 2012 at 05:20 AM
Your problem is simple you use a collision function to check distance.
OnTriggerEnter only checks when you enter the zone, hence your guy is moving an inch and then stops, try to take the function off and put the whole thing in the Update from here:
if (!player)
return;
until the end. But remove one } at the end
If I do that I get 3 errors.
(17,33) "expecting ), found ':'." (17,34) "';' expected. Insert a semicolon at the end." (17,43) "Unexpected token: )."
All of them are regarding the line "OnTriggerEnter" is on. I've gotten these before, and it seemed that no matter what I did, they wouldn't go away, and I just got more errors. I'm sorry if I'm missing something obvious here, because as I said, I'm still kind of a beginner at scripting.
Well do what it says. OnTriggerEnter is not right in here. You need in the update to check for the distance between the player and the NPC. If the player is in then transform.LookAt(player); and move forward. OnTriggerEnter is used to trigger an action or a reaction when something enter another one but only when it enters. At he worst,you could use OnTriggerStay, but the main idea is they are callbacks routine and it is wrong to call them for what you want to achieve.
Ok, I managed to get rid of the errors and have the OnTriggerEnter command after the "if (!player) return;", but now I'm back to my initial problem. The object moves forward slightly then stops.
Your answer
Follow this Question
Related Questions
OnTriggerEnter 1 Answer
Can't figure out how to use multiple triggers in single scene 1 Answer
Box collider doesn't work when same size as mesh. 1 Answer
BoxCollider Overlap OnTriggerEnter Problem 1 Answer
Trigger Spawning? 1 Answer