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
1
Question by designico · Oct 30, 2013 at 10:54 AM · lookatlocalspace

LookAt locked at Y-axis but in local space

Hi!

I used the Faux Gravity Script to work in planetary Setup... However I have a gameobject that is always looking at the worlds center. I put another gameobject into it. This child should look in a special direction but must be locked on the local Y axis. Using

transform.LookAt(new Vector3(forwardObject.position.x, transform.localPosition.y, forwardObject.position.z)

makes the child looking at the target and it is looked a the Y axis, but the X and Z axis are looked at the world space. I need them to be looked at the localspace, so that the child is always aligning at its parent. Hope this is understandable.

Do someone have a solution for that?

Comment
Add comment · Show 5
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 robertbu · Oct 30, 2013 at 02:15 PM 0
Share

What I visualize is a head on a parent that can look up or down but cannot turn side to side...or a gun on a turret that can look up or down but it is the turret that rotates. Is this correct? And is the child trying to get the elevation of the world target? And is the parent 'y' axis aligned or can it have arbitrary rotation?

avatar image designico · Oct 30, 2013 at 02:39 PM 0
Share

Not really. The parents down vector always points to the center of the planet(its Y-Rotation doesn't matter). The child should inherit the rotation but can freely look left/right (Y).

The problem is if I use transform.LookAt(new Vector3(forwardObject.position.x, transform.localPosition.y, forwardObject.position.z) the X and Z rotation of the child are not inherits the parents rotation - it is just locked at the world space.

So the childs down vector should always point to the planets center, but turns at a target object but only uses the Y axis.

Sorry - it's kind of hard to explain for me.

avatar image robertbu · Oct 30, 2013 at 02:59 PM 0
Share

So just to be clear if the parent is a body walking around on a planet. The head can look left and right with respect to the body AND, given the head restrictions, the head tries to track some target?

avatar image designico · Oct 30, 2013 at 03:07 PM 0
Share

yepp: head.eulerAngle.x = 0 in local space (body) head.eulerAngle.y = tries to look at the target. head.eulerAngle.z = 0 in local space (body)

avatar image N427 · Aug 06, 2014 at 08:52 PM 0
Share

I really really really needed this and after a few days of searching I found this answer. Thank you. So. $$anonymous$$uch.

I love you. (>^-^)>

1 Reply

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

Answer by robertbu · Oct 30, 2013 at 03:24 PM

I'm going to use head and body here as the transform for the head and the body, where the body is standing on the planet and the head can look left and right with respect to the body. The easiest way to make this work is to project the target point onto a plane that has a normal of the body's up.

 var distanceToPlane = Vector3.Dot(body.up, target.position - head.position);
 var planePoint = target.position - body.up * distanceToPlane;
 head.LookAt(planePoint, body.up);
Comment
Add comment · Show 7 · 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 designico · Oct 30, 2013 at 03:49 PM 0
Share

Thank you!! To be honest I have not idea how Vector3.Dot works... but it works! I'll try to understand it later :-)

avatar image designico designico · Oct 30, 2013 at 04:02 PM 0
Share

$$anonymous$$aybe interesting for others: In my case I needed to put that code into a LateUpdate function. Otherwise it was jittering.

avatar image robertbu · Oct 30, 2013 at 04:04 PM 0
Share

While understanding Vector3.Dot() works is nice to know, the top two lines of code just project a point onto a plane. As a way of getting some of the functionality before having a deep understanding of Vector math, take a look at the Wiki $$anonymous$$ath3d script. It gives you lots of functions for 3D geometry that you can use including ProjectPointOnPlane().

avatar image RoryScanlan · Mar 01, 2014 at 06:34 PM 0
Share

just thaught id post a C# version of the above code as i found it very usefull.

 float DistanceToPlane = Vector3.Dot(body.up, target - head.position);
 Vector3 plantPoint = target - head.up * DistanceToPlane;
 
 firePoint.transform.LookAt(plantPoint, body.up);
avatar image andyRogerKats · Dec 14, 2017 at 05:42 PM 0
Share

I am trying to get similar results, but my lookAt is looking at a raycast hit.point. I can get the "head " to turn around on it's local axis but once the raycast hit.point moves the opposite side of the original hit, the "head" jitters and jumps around. What I want is to have the head look at the hit.point and swivel on it's local y axis. I am not sure why it is jumping around when the hit.point moves... can anyone explain this to me?

avatar image andyRogerKats andyRogerKats · Dec 14, 2017 at 05:42 PM 0
Share

And bonus points if it can do the Quaternion.Slerp

avatar image mauriciofelippe · May 01, 2020 at 04:49 AM 0
Share

Best answer, Thanks!

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

19 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

Related Questions

Look at around one axis in local space 1 Answer

i want to know what object is the closer to the camera to see 1 Answer

[RePost for No Answer] How to make a character look at the direction it's going towards 1 Answer

replace raycast with transform direction 1 Answer

Indicator rotating around player 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