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
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

@Yisreal, remember to checkmark the best answer, thanks.

5 Replies

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

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

Put this 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

Thanks works great!

avatar image qJake · Jul 13, 2010 at 10:14 AM 0
Share

Please upvote and checkmark the answer if it worked for you.

avatar image Vitruvius · Aug 04, 2010 at 10:14 PM 1
Share

Can this be constrained to only the Y-axis? so that if my player passes the enemy in 2D , the enemy rotates only around the Y-axis to face in the player's direction.

avatar image horrorgamer1337 · Nov 18, 2012 at 06:24 AM 0
Share

Could you make one of those in javascript form for me?

avatar image Akusan · Sep 03, 2014 at 01:00 AM 0
Share

Thanks qJake. This code works perfectly. After adding the script as a component to the object, I can simply drag another object to the target

@horrorgamer, http://scriptsharp.com/

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 this...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 this on my 2d enemy char and it works great let me know what you think!!!

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 this below simple script which 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

This 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.

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

Joystick 2D Sprite Rotation and Movement 0 Answers

Explain this script :) plz 1 Answer

Animation Must be marked as legacy 4 Answers

Ground checking with a frequently rotating player 2 Answers

Is there any way for a Sprite Renderer to use mipmaps? 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