• 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
3
Question by Yisreal · Jul 12, 2010 at 10:07 PM · 2drotationspritesprites

how do i make an object always face the player?

I'm looking for a way to make a 2d object (or any object) always face the player in 3d. How can I go about doing so using little system resources?

Thanks a ton!

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 Cyclops · Jul 13, 2010 at 01:39 PM 0
Share

5 Replies

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

Answer by qJake · Jul 12, 2010 at 10:28 PM

Put t$$anonymous$$s on your object:

// C# using System; using UnityEngine;

public class LookAtTarget : MonoBehaviour { public Transform target;

  void Update()
  {
       if(target != null)
       {
            transform.LookAt(target);
       }
  }

}

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 Yisreal · Jul 13, 2010 at 09:57 AM 0
Share
avatar image qJake · Jul 13, 2010 at 10:14 AM 0
Share
avatar image Vitruvius · Aug 04, 2010 at 10:14 PM 1
Share
avatar image horrorgamer1337 · Nov 18, 2012 at 06:24 AM 0
Share
avatar image Akusan · Sep 03, 2014 at 01:00 AM 0
Share
Show more comments
avatar image
5

Answer by j-unity · Nov 18, 2012 at 12:07 PM

Hi! I´m new in the forum. You can try with t$$anonymous$$s...If you are in a 2D Game and your player only moves f.e. in x axys and the enemy is stopped it only will face up the face lloking the player. I add a damp variable so make the rotation softer. I hope you can use it!!

 // Script to look at camera
 
 var targetPosition :Transform; // we have to add in the Inspector our target
 var damp: int = 5; // we can change the slerp velocity here
 
 function Update ()
 {
     if ( targetPosition ) // we get sure the target is here
     {
         var rotationAngle = Quaternion.LookRotation ( targetPosition.position - transform.position); // we get the angle has to be rotated
         transform.rotation = Quaternion.Slerp ( transform.rotation, rotationAngle, Time.deltaTime * damp); // we rotate the rotationAngle 
     }
 }
 
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
avatar image
0

Answer by setn2007 · Sep 08, 2014 at 08:33 PM

I put t$$anonymous$$s on my 2d enemy char and it works great let me know what you t$$anonymous$$nk!!!

pragma strict

/java script var player : Transform; // player var self : Transform; // object you want to face the player on x axis only

function Start () {

}

function Update () { if (player.transform.position.x <= self.transform.position.x) //players spot in world space as opposed to enemy "self" spot { self.transform.rotation = Quaternion(0,180,0,0);// flips enemy around to face the player on x axis only } else if (player.transform.position.x >= self.transform.position.x)//players spot in world space as opposed to enemy "self" spot { self.transform.rotation = Quaternion(0,0,0,0);// flips enemy around to face the player on x axis only }

}

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

Answer by kevinspawner · Oct 09, 2014 at 10:26 AM

You can add t$$anonymous$$s below simple script w$$anonymous$$ch will make the object point towards camera always. Just in case if the camera rotation is messed up in your scene. Change the Vector3 .Left to other directions. Hopefully it helps!

 {
     public Camera targetCamera;
 
     
     // Update is called once per frame
     void Update() 
     {
         transform.LookAt(transform.position + targetCamera.transform.rotation * Vector3.left,
         targetCamera.transform.rotation * Vector3.up);
     }
 }
 
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
avatar image
0

Answer by proVeritas_vk · Jul 02, 2018 at 07:30 AM

T$$anonymous$$s helps me:

public Transform player;

...

Vector3 fwd = player.forward; fwd.y = 0; transform.rotation = Quaternion.LookRotation(fwd);

where transform is transform of object you want

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

2d animation import 1 Answer

Unity 4.3 Sprite Align 2 Answers

2D Sprite Rotation 0 Answers

Rotating a Sprite to face mouse / target? 2 Answers

Having Issues Rotating 2d Sprites to face another 2d Object 3 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