Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 bishop87 · Aug 08, 2015 at 10:23 AM · lookat

LookAt only on Z Axis

Hey guys, I'm trying to get an object to look at a player. The object moves with the player, however the problem is that it moves on the X and Y axis when I want it to just look at the Z axis.

public class FollowAim : MonoBehaviour {

 private GameObject targetOBJ;
 private Transform targetTRA;

 
 // Update is called once per frame
 void Update () {

     targetOBJ = GameObject.FindGameObjectWithTag("Player");
     targetTRA = targetOBJ.transform;
     this.transform.LookAt(targetTRA);
 }

}

Is there any way to get the object to JUST rotate on the Z axis?

Comment
Add comment · Show 3
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 Bonfire-Boy · Aug 08, 2015 at 10:46 AM 0
Share

I'm not sure I follow the question, they way you're talking about "moving on" and "looking at" axes seems a bit confused. Note that an axis is a line, with infinite extent, so saying that you want something to look at an axis doesn't actual define what you're trying to do.

I take it that you're saying that you don't actually want the object to look at the player? So what point do you want it to look at?

avatar image bishop87 · Aug 09, 2015 at 12:28 AM 0
Share

Sorry, What I meant was that it is rotating on the X and Y when I want it to JUST rotate on Z. I want the object to look at the player, and in order for it to do this, it must only rotate on Z

avatar image bishop87 · Aug 09, 2015 at 03:52 AM 0
Share

Sorry it appears that I wasn't clear on what it was I wanted to get done so I'll try to refine it a bit.

I'm doing a top down game. The objects move along X(L and R) and Y(U and D) and rotate on Z. So if I wanted an enemy to face the player, I would want the enemy to rotate on Z. Rotating on X or Y results in the enemy shooting bullets off into space not on screen or shooting the sprite on its side, neither of which I want.

$$anonymous$$y problem was that the original script was causing the enemy to Rotate on both X and Y at the same time, and not on Z. I wanted to know if/how to prevent the enemy from rotating on X and Y, and to only rotate on Z.

Hopefully that is more clear, sorry for the confusion.

2 Replies

· Add your reply
  • Sort: 
avatar image
18

Answer by jimbobulus2 · Aug 28, 2015 at 06:14 PM

Hi @bishop87, If you want to just rotate on the Z axis, don't use the LookAt() method, it uses all axes by default.

in your Update(), replace the line with LookAt to this:

 Vector3 difference = targetTRA.position - transform.position;
 float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
 transform.rotation = Quaternion.Euler(0.0f, 0.0f, rotationZ);

If you need more info, I made a tutorial with an example project here: http://blog.iss.io/?p=26

Comment
Add comment · Show 5 · 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 Bonfire-Boy · Sep 01, 2015 at 10:42 AM 0
Share

This is the right approach I$$anonymous$$O. If you only want to rotate about one axis then only rotate about one axis... LookAt is not the best way to do that.

avatar image Cypras · Feb 27, 2016 at 08:29 PM 0
Share

@jimbobulus2 How would I do the same thing for the x axis? It works great for the z axis.

avatar image jimbobulus2 Cypras · Feb 29, 2016 at 10:05 AM 1
Share

@Cypras You would swap the axes, same code though: float rotX = $$anonymous$$athf.Atan2(difference.z, difference.y) * $$anonymous$$athf.Rad2Deg; Quaternion newRotation = Quaternion.Euler(new Vector3(rotX + adjustmentAngle, 0.0f, 0.0f));

avatar image ihsan07 · Feb 10, 2018 at 08:38 AM 0
Share

I love how you approach this problem and I need such a solution but X and Y angles always get stuck at 0 and if I write down original X and Y angle ins$$anonymous$$d of 0, it doesn't look at target object anymore. Rotation goes wierd. Can you help me with my question? It is posted here.

avatar image mkusan · Jul 06, 2018 at 12:11 PM 0
Share

Thank you so much! This helped me sooo much

avatar image
3

Answer by captainmalk · Mar 07, 2020 at 06:49 PM

If you want to use "transform.lookat" you can always make a new vector and keep the object's transform in one or more of the axises, this is what i often do:

 public Transform target;    
         
 void Update() {

      transform.LookAt (new Vector3 (target.position.x, transform.position.y, target.position.z));
 }

You should be able to change the "transform.position" on any axis you want to keep on the original position.

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 DarshGupta · Dec 25, 2020 at 04:32 PM 0
Share

Thank you! This helped a lot in my racing game. I'm making a Car AI without using actual AI, and this helped so much!

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

12 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

Related Questions

Enemy circling around the player when using LookAt 1 Answer

Look At with SmoothDamp 1 Answer

Respawn looking 1 Answer

Aim flashlight at cursor in third person 1 Answer

how do i make somthing rotate with mouse 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