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 FrowningPigeon · Jul 03, 2013 at 12:16 PM · collisionfollow

Trouble with getting cube to follow character

Hi all,

at the moment I have two scripts (Hills_Cube_Collide.js, Follow.js) The first script sets the target of the cube to 'Player' when the character hits the cube, but only if that cube has the tag 'Cube2'. The second script is where the code for the cube to follow the character goes. Now here is my problem. I have two cubes on the screen both with the tag 'Cube2', so what I want to happen is when i intersect one cube, that follows me, and then when i hit the next cube that follows me also. But what is happening is no matter which cube i hit, they both start to follow me at the same time, they don't wait until they have been hit. For example, if i hit the cube on the left; the one on the right will also follow. Whereas what I want to happen is that the cube follows me when i hit it, not when i hit a cube with the same tag as it...any suggestions would be appreciated!

Hills_Cube_Collide:

 function OnTriggerEnter (other : Collider) { 
 if (other.gameObject.tag == "Cube1" ){
     //Destroy(other.gameObject);
 //    other.Parent = Player;
    Score.score += 1;
    Follow.target = GameObject.FindWithTag("Player").transform;
    }
 }

Follow.js

     static var target : Transform; //the enemy's target
     var moveSpeed = 3; //move speed
     var rotationSpeed = 3; //speed of turning
    
      
     var myTransform : Transform; //current transform data of this enemy
      
     function Awake()
     {
     //myTransform = transform; //cache transform data for easy access/preformance
     }
      
     function Start()
     {
     //target = GameObject.FindWithTag("Player1").transform; //target the player
      
     }
      
     function Update () {
     
     if (target == GameObject.FindWithTag("Player").transform)
     {
     //rotate to look at the player
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
      
     //move towards the player
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
     }
      
      
     }
     
     
  
 
Comment
Add comment · Show 2
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 Ben-Stoneman ♦♦ · Jul 03, 2013 at 01:09 PM 0
Share

Do you have a pro licence? or are you using Unity free?

avatar image FrowningPigeon · Jul 03, 2013 at 01:10 PM 0
Share

I am using a trial version of the pro

1 Reply

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

Answer by Seregon · Jul 03, 2013 at 01:35 PM

You have the 'target' variable set as static, which means it is shared by all instances of that class, and when you set it for one cube, you set it for all cubes. To fix this you need to first change the decleration of 'target' to non static:

 var target : Transform; //the enemy's target

and then change how you set that value on collision:

     function OnTriggerEnter (other : Collider) {
        if (other.gameObject.tag == "Cube1" ){
           Score.score += 1;
           var followScript: Follow = other.gameObject.GetComponent("Follow");
           followScript.target = GameObject.FindWithTag("Player").transform;
        }
     }

Lastly, in your question you say the cubes are tagged as "Cube2", and in the script they're tagged as "Cube1", so you may want to check that. Let me know if this works or not, I use C# so there may be some minor syntax errors.

Comment
Add comment · Show 2 · 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 FrowningPigeon · Jul 03, 2013 at 01:40 PM 0
Share

Yes thanks! that has worked for me....I am still learning the basics as I am new to coding in unity, I have dont some before...but using c# like your self. Just one more question...how would i do it so that the cube follows me 5 steps behind the character at all times? +1

avatar image Seregon · Jul 03, 2013 at 01:49 PM 0
Share

That's a little more involved, but I would start with something like

 function Update () {
  
    if (target == GameObject.FindWithTag("Player").transform)
    {
       //rotate to look at the player
       myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
       Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
  
       var distanceToPlayer : float = Vector3.Distance(target.transform.position, myTransform.position);
       if(distanceToPlayer > 5){
          //move towards the player
          myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
       }
    }
 }

This way, if the cube is closer than 5 units, it stops, otherwise it follows the player. If you want something smoother, you might want to try getting the players speed, and using that as the cubes speed, and making it faster or slower if it gets too far away/too close. Thats too much detail to go into in a comment though.

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Advice for Intersecting Colliders 0 Answers

Destroy On Collision 4 Answers

Combine common surfaces 1 Answer

Weirdest Collision EVER 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