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
0
Question by DNP · Aug 26, 2012 at 05:00 AM · rotationaxislookat

Making a specific axis look at an object

How can i get something to look at something but just on one axis, such as the y axis, so the object is always looking at an object but never looking up or down, just left and right, always following the object. I tried this code and it didnt work:

 var PlayerObj : Transform;
 
 function Update () {
 
 transform.rotation.y.LookAt(PlayerObj);
 
 }

Please help!

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

Answer by flokkienathur · Aug 26, 2012 at 06:04 AM

You could try to make 2 vector3's with the same x and z position and than use the LookAt function on Quaternion. Than it will rotate on only the y axis. Or you could make a new quaternion, set it equal to the current rotation, then rotate the object and reset everything in the wuaternion exept for the y rotation. Only problem is, quaterions are realy difficult to understand so if you use the second method, and you run into a problem, try the first :)
Hope this helps!

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 DNP · Aug 26, 2012 at 06:07 AM 0
Share

Yeah, im kind of a noob. I understand many things, but i never seemed to understand Quaternions. $$anonymous$$ind giving me an example based on my script thats in the post?

avatar image aldonaletto · Aug 27, 2012 at 02:08 AM 1
Share

He means something like this:

function Update(){
  var pos = target.position; // get the target position...
  pos.y = transform.y; // but at this object's height
  transform.LookAt(pos); // then look at it
}
avatar image DNP · Aug 27, 2012 at 02:48 AM 0
Share

Thank you all so much! It works!

avatar image
2

Answer by ScroodgeM · Aug 26, 2012 at 08:57 PM

C#

using UnityEngine;
using System.Collections;
public class Yrotater : MonoBehaviour
{
	public Transform lookAt;
	void Update()
	{
		Vector3 worldLookDirection = lookAt.position - transform.position;
		Vector3 localLookDirection = transform.InverseTransformDirection(worldLookDirection);
		localLookDirection.y = 0;
		transform.forward = transform.rotation * localLookDirection;
	}
}
Comment
Add comment · Show 4 · 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 DNP · Aug 27, 2012 at 01:34 AM 0
Share

Well, i will see what i can do with this. I dont code in C# so i dont know what the first 3 lines mean, but i believe i understand the rest. I will approve your answer if it ends up being correct.

avatar image aldonaletto · Aug 27, 2012 at 01:52 AM 2
Share

In UnityScript:

function Update(){
    var worldLookDirection = lookAt.position - transform.position;
    var localLookDirection = transform.InverseTransformDirection(worldLookDirection);
    localLookDirection.y = 0;
    transform.forward = transform.rotation * localLookDirection;
}
This is a more complete case, where the object will rotate strictly around its local Y axis (even if its inclined relative to the world Y axis). $$anonymous$$ore often, the object must simply rotate around the world Y axis, which simplifies things:

function Update(){
    var lookDirection = lookAt.position - transform.position;
    lookDirection.y = 0;
    transform.forward = transform.rotation * lookDirection;
}
avatar image DNP · Aug 27, 2012 at 01:58 AM 0
Share

Its saying lookAt is an $$anonymous$$ Identifier

avatar image ScroodgeM · Aug 27, 2012 at 07:34 AM 0
Share

you should attach some transform to lookAt variable.

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

10 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

Related Questions

LookAt on one axis 2 Answers

How can I rotate towards/look at a specific axis of a collided object? 2 Answers

Object facing the wrong way? 1 Answer

LookAt() with X axis 1 Answer

[Please!] Noob need's help with planet gravity movement. 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