• 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 Makiavel · Sep 11, 2014 at 04:45 PM · rotatedirectionaxis

How to make the npc face the player.

Question may seems silly, but I am failing to rotate the npc using only single (vertical) axis. How do I freeze two axis's while rotating the object to face a Vector3 point? I tried to rotate it around the y axis by the angle between transform.forward and direction to the Vector3 point, but the angle is always a positive value, so npc constantly rotates like a bloody carousel)

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
2
Best Answer

Answer by VesuvianPrime · Sep 11, 2014 at 05:02 PM

You should take a look at the documentation for Quaternions.

Quaternions look difficult at first, but when you learn how to use them and WHY to use them, they will make your life a million times easier.

In your specific case, you can get the rotation for your NPC by doing:

 Vector3 playerPos;
 Vector3 npcPos;
 Vector3 delta = new Vector3(playerPos.x - npcPos.x, 0.0f, playerPos.z - npcPos.z);
 
 Quaternion rotation = Quaternion.LookRotation(delta);




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 Makiavel · Sep 11, 2014 at 08:21 PM 0
Share

Thanks you very much! Quaternions always confused me.

avatar image JJDeveloper · Jul 06, 2015 at 07:08 PM 0
Share

I tried this script just to test it. I put it in the Update(), but it won't run because there's an error. Could you please share the rest of the relevant code?

avatar image matrise · Apr 05 at 08:39 AM 0
Share

This is a good answer, for anyone who wants to know how to further update the rotation of the NPC after getting the target value, here is what I did below. Of course, playerPos and npcPos must be given the transform.position of their respective GameObjects.

float smooth = 5.0f; //Sets the speed at which the NPC rotates towards you to keep contact Vector3 playerPos; Vector3 npcPos; Vector3 delta = new Vector3(playerPos.x - npcPos.x, 0.0f, playerPos.z - npcPos.z); Quaternion target = Quaternion.LookRotation(delta); transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);

avatar image
0

Answer by PokerBob · Oct 21, 2019 at 09:10 AM

I was just looking for a simple way how to do this and found your answer, which works great, thanks. for anyone else who is having trouble understanding how to implement it, what i did was add a script to each NPC gameobject I wish to keep looking at us (as below). BTW 'ThePlayer' is a static reference to the FPS controller

 using UnityEngine;
 
 public class FacePlayer : MonoBehaviour
 {
    void Update()
    {
       Vector3 playerPos = GameManager.ThePlayer.transform.position;
       Vector3 npcPos = gameObject.transform.position;
       Vector3 delta = new Vector3(playerPos.x - npcPos.x, 0.0f, playerPos.z - npcPos.z);
       Quaternion rotation = Quaternion.LookRotation(delta);
       gameObject.transform.rotation = rotation;
    }
 }
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

26 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Face to direction, in a Mario Galaxy style game? 1 Answer

Rotating character based on joystick angle, at an angle 0 Answers

Objects rotate but remain on the original axis... 0 Answers

use world axis when using transform.rotate 3 Answers

Add force at position Question 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