Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Paramotion · Jul 04, 2018 at 03:20 PM · positionlocalrotationvector3.distance

Rotate object related to Vector3.Distance

I have a parent game object that is moved by player. This parent has a child that I want to follow the parent in position with a delay(already solved) and I need the child object to localRotate facing the movement of the parent, rotating towards X and Z only.

I made a Gif inside 3Ds Max with one axis(Hinge joint does the same in Unity, but I'm trying to make it in two Axis): alt text

So far I have this:

 void RotateAxis()
     {
         Vector3 trackDataX = new Vector3(track.transform.position.x, 0,0);
         Vector3 trackDataZ = new Vector3(0,0,track.transform.position.z);
         Vector3 childDataX = new Vector3(this.transform.position.x, 0,0);
         Vector3 childDataZ = new Vector3(0,0, this.transform.position.z);
 
         float distX = Vector3.Distance(trackDataX, childDataX);
         float distZ = Vector3.Distance(trackDataZ, childDataZ);
         this.transform.localRotation = new Quaternion(distZ, this.transform.localRotation.y, distX, this.transform.localRotation.w);
 
     }

But the child object is not rotating towards the position direction of the parent.

This other code makes the children cube to rotate smoothly but doesn't return its rotation to local 0,0,0 when the parent stops moving:

     void LookAtTarget()
     {
         Vector3 targetPosition = new Vector3(trackLook.transform.localPosition.x, this.transform.localPosition.y, trackLook.transform.localPosition.z);
         this.transform.localRotation = Quaternion.LookRotation(targetPosition, Vector3.up);
     }

How can I achieve this? Thanks in advance

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tormentoarmagedoom · Jul 04, 2018 at 05:01 PM

Hey.

I dont get exatcly what you need. Where needs to look at the child? At the parent? Or have the same rotation as the parent? Some draw will be nice to understand it!

But anyway, ypu know about LookAt() function? I think is what ypu need. Go read the api or look some tutorial to learn how to use it!

Byee!

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 Paramotion · Jul 05, 2018 at 10:37 AM 0
Share

Hi, I've investigated LookAt() before, but after some code and tweaks it isn't behaving the way I like. The idea is to have the object moving like this(I recorded a Gif in 3Ds$$anonymous$$ax animating the movement) but in both X and Z positions, forgibing the Y one. https://imgur.com/lH7voGO

Hinge joint doesn't work either.

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

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

Related Questions

Camera rotation around player while following. 6 Answers

Progress bar for player progress is backwards 1 Answer

Changing distance with Vector3.Distance 1 Answer

Camera movement 1 Answer

change properties of gameObject 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