Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TheGoliath · Apr 25, 2012 at 05:17 AM · colliderontriggerentertarget

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); 
 } 

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Apr 25, 2012 at 04:09 PM 0
Share

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.

avatar image TheGoliath · Apr 25, 2012 at 04:18 PM 0
Share

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.

avatar image fafase · Apr 25, 2012 at 04:32 PM 0
Share

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?

avatar image TheGoliath · Apr 25, 2012 at 04:44 PM 0
Share

Yes, exactly. I just need to make sure carrie actually smoothly moves to the position, rather than just teleporting.

avatar image fafase · Apr 25, 2012 at 06:28 PM 0
Share

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.

Show more comments
avatar image
1

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheGoliath · Apr 25, 2012 at 01:24 PM 0
Share

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.

avatar image fafase · Apr 25, 2012 at 02:13 PM 0
Share

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.

avatar image TheGoliath · Apr 25, 2012 at 02:16 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges