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
1
Question by Paul36 · Sep 05, 2012 at 08:34 PM · transformvector3forward

How to anticipate the Vector3.forward of the enemy?

Hi,

i would like my character to anticipate the direction of the enemy, in order to catch him. But my character looks too slow, and rotate just after the enemy passes him. So i would like the character to anticipate the next step of the enemy, with Vector.forward * 5 (in this example), but he does not seem to understand what i am trying to do, the z axis does not rotate fast enough so that the character can catch the enemy. Would you know why?

Here is the code :

 //rotate the character every frame
 if(enemyFound == true){
         //enemy to follow
         var newTransform : Vector3 = enemyToFollow.transform.position;
         newTransform += newTransform.forward*5*Time.deltaTime; //anticipate a bit, does not work
         directionToClosestEnemy = newTransform - transform.position;
         tempRot = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(directionToClosestEnemy),2*Time.deltaTime);
     }
     
 
 //make him move
 if(enemyFound == true){
         targetVelocity = enemyToFollow.transform.position - transform.position;
         targetVelocity = targetVelocity.normalized; // Is now speed 1, going correct way
         targetVelocity = targetVelocity * MOVE_SPEED;
         var velocityChange = (targetVelocity - velocity);
         velocityChange.y = 0;
         rigidbody.AddForce(velocityChange, ForceMode.Force);
     }

Thanks

Edit: do you mean like this ?

 var newTransform : Vector3 = enemyToFollow.transform.position;
         newTransform += enemyToFollow.transform.forward * 100 * Time.deltaTime;
         directionToClosestEnemy = newTransform - transform.position;
         tempRot = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(directionToClosestEnemy),2*Time.deltaTime);
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

1 Reply

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

Answer by Piflik · Sep 05, 2012 at 08:50 PM

The reason why it doesn't work is that you don't add the enemy's forward vector, but the Z-component of its position. You need to replace 'newTransform.forward' with 'enemyToFollow.transform.forward'.

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 Paul36 · Sep 05, 2012 at 10:08 PM 0
Share

@Philipp Jutzi thanks, it does not seem to respond to my setting, i edited my previous post, would you know why it does this? The character still rotates after the enemy, ins$$anonymous$$d of anticipating the direction of the enemy.

avatar image Piflik · Sep 06, 2012 at 08:58 AM 0
Share

It doesn't work, because you don't even use the stuff you are calculating. You are just applying a force that is calculated from the current position of the enemy.

Try this:

 private var multiplier : int = 100; // how much do you want the character to lead the enemy

 if(enemyFound) {
     var enemyPos : Vector3 = enemyToFollow.transform.position + enemyToFollow.transform.forward * multiplier; //no Time.deltaTime...this is a fixed position in front of the enemy, independent of the framerate
     transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation((enemyPos - transform.position).normalized));
     velocityChange = ((enemyPos - transform.position).normalized * $$anonymous$$OVE_SPEED) - velocity;
     velocityChange.y = 0;
     rigidbody.AddForce(velocityChange);
 }
avatar image Paul36 · Sep 06, 2012 at 10:23 AM 0
Share

@Piflik thanks Piflik! you 're right, i needed to use enemyPos for the position too, not only the rotation. Thanks a lot, it works great! (edit: don't know how to do a +1 for your answer, but thanks a lot)

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

8 People are following this question.

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

Related Questions

Move an object forward and not up? 1 Answer

Turn and Move Foward 1 Unit relative to Objects local direction 1 Answer

Rigidbody to follow player around 1 Answer

transform.forward makes my GameObject Disappear 0 Answers

Problems with Vector3.angle and angle to target 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