Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 poop19999333 · May 08, 2017 at 01:13 PM · follow path

How to make character face direction of target?

I'm trying to make my enemy follow a path but also face the direction of the current target. Below is my code:

public class enemy : MonoBehaviour { public Transform[] target; public float speed; private int current;

 // Use this for initialization
 void Start () {
     
 }
 
 // Update is called once per frame
 void Update () {
     if (transform.position != target [current].position) {
         Vector3 pos = Vector3.MoveTowards(transform.position, target[current].position,speed *Time.deltaTime);
         GetComponent<Rigidbody>().MovePosition(pos);

     }
     else current = (current + 1) % target.Length;
 }

}

What can I add in this to make my enemy just face the current target it is going to?

Thanks for the help!

Comment
Add comment · Show 1
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 SohailBukhari · May 08, 2017 at 01:27 PM 0
Share

Transform.LookAt Rotates the transform so the forward vector points at /target/'s current position.

Then it rotates the transform to point its up direction vector in the direction hinted at by the worldUp vector. If you leave out the worldUp parameter, the function will use the world y axis. worldUp is only a hint vector. The up vector of the rotation will only match the worldUp vector if the forward direction is perpendicular to worldUp.https://docs.unity3d.com/ScriptReference/Transform.LookAt.html

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by leech54 · May 10, 2017 at 01:29 PM

if Y is the axis you want to rotate around you could

 float angle = Mathf.Atan2(target.x-charPos.x,target.z-charPos.x) * Mathf.Rad2Deg;
 Vector3 newVec = new Vector3(0f,0f,0f);
 newVec.y = angle;
 transform.eulerAngles = newVec;

Comment
Add comment · Show 1 · 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 leech54 · May 09, 2017 at 03:27 PM 0
Share

Here is another way

 Vector3 deltaVec = target.transform.position - transform.position;
 Quaternion rotation = Quaternion.LookRotation(deltaVec);
 transform.rotation = rotation;
avatar image
0

Answer by darchyneer · May 08, 2017 at 01:34 PM

I think you need to add a transform.lookat(). Something like Transform.lookat(the transform of whatever u what it to see). Public Transform target; then add Transform.Lookat(target) in your code.

Comment
Add comment · Show 1 · 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 poop19999333 · May 08, 2017 at 01:36 PM 0
Share

I've tried the transform.lookat(). $$anonymous$$eeps co$$anonymous$$g up as an error. Can either of you give me an example of how am I suppose to write my line? It just doesn't seem to work. Or when it does, it will only look at the first target. Not the current target.

avatar image
0

Answer by poop19999333 · May 08, 2017 at 01:40 PM

I've tried the transform.lookat(). Keeps coming up as an error. Can either of you give me an example of how am I suppose to write my line? It just doesn't seem to work. Or when it does, it will only look at the first target. Not the current target.

Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

How can I Make a path for Camera ? 1 Answer

Rigidbody trying to follow a spline with smoothing 1 Answer

Character moves along pre-defined path / curve 1 Answer

Make an NPC follow player? 1 Answer

Collider is not working... 2 Answers


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