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 Tea_Doogun · Oct 08, 2012 at 06:42 PM · rotationparentchild

Parent object to face childs rotation...

HI :) Please tell me if i have explained this badly and i will re-write!

  • 3rd person perspective game

  • I've got 2 objects, a head and a body, the head is a child of the body

  • When i move the mouse to look around, the head moves, and is always looking toward the center of the screen

  • when the head moves 35 degrees from the direction of the body, i want the body to turn in the same direction by 35 degrees, so that it lines back up with the head

  • The code i wrote doesn't seem to work this way, instead, it will work once and then the body will continually follow the head until the head centralises, at which point it works again

  • can you smart cats take a look and advise my dumb ass?

    var body : GameObject;

    var turnSpeed : float = 1.0;

    var turnTrigger : float = 35;

    var lookSensitivity : float = 5.0;

    var yRotation : float;

    var xRotation : float;

    var currentYRotation : float;

    var currentXRotation : float;

    var yRotationV : float;

    var xRotationV : float;

    var lookSmoothDamp : float = 0.1;

    function Update () {

    var bodyRotation = body.transform.localRotation.y;

       yRotation += Input.GetAxis("Mouse X") * lookSensitivity;
         xRotation -= Input.GetAxis("Mouse Y") * lookSensitivity;
         
         xRotation = Mathf.Clamp(xRotation, -90, 45);
         
         //HERE'S THE CODE I'M HAVING TROUBLE WITH:
         
         if (yRotation > bodyRotation + turnTrigger){
         body.transform.eulerAngles.y = yRotation;
     
         Debug.Log("turn right!");}
         
         if (yRotation < bodyRotation - turnTrigger){
         body.transform.eulerAngles.y = yRotation;
     
         Debug.Log("turn left!");}
         
         //THAT'S IT!
         
     
         currentXRotation = Mathf.SmoothDamp(currentXRotation, xRotation, xRotationV, lookSmoothDamp);
         currentYRotation = Mathf.SmoothDamp(currentYRotation, yRotation, xRotationV, lookSmoothDamp);
     
     
         transform.rotation = Quaternion.Euler(currentXRotation, currentYRotation, 0);
     
     
     
     }
    
    

The Code is on the 'head' object.

I only wrote the code which turns the body, the 'mouse look' script is from ETeeski Tutorials.

Thanks, Tom :)

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 Sundar · Oct 08, 2012 at 07:14 PM 0
Share

Is there any reason that you can't make body as heads child? this way - when you turn head the body follows.

or you can attach "mouse look" script to body, when you turn, both body and head turn in the same direction and degrees of turn.

avatar image Tea_Doogun · Oct 08, 2012 at 07:21 PM 0
Share

Thanks for your input :)

I'd like the body to remain still while the head looks around, until the head faces away from the body's forward vector by 35 degrees. Just to simulate a person's head looking around while standing still.

I tried adding the movement to the head, hilarity ensued! It just didn't move right.

Another mouse look script on the body could work well, but it would still have to only move around when the head turns to meet a certain angle.

avatar image Sundar · Oct 08, 2012 at 09:35 PM 0
Share

Have mouse look script on body and trigger the rotation only when head turned 35 degrees.

1 Reply

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

Answer by Memige · Oct 08, 2012 at 09:50 PM

The problem (I believe) is that you are trying to set an individual axis through the eulerAngles property. This is a no-no. Individual axes can be -read- with the eulerAngles.y value, but should only be set with a full new vector (transform.eulerAngles)

try:

 body.transform.eulerAngles = Vector3(body.transform.eulerAngles.x, yRotation, body.transform.eulerAngles.z);


Comment
Add comment · Show 2 · 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 Tea_Doogun · Oct 15, 2012 at 11:42 AM 0
Share

Thanks man :)

With a little tweak i was able to make it behave exactly how i needed :D

You're very helpful, thank you :) :) :)

avatar image Memige · Oct 16, 2012 at 12:29 AM 0
Share

You are most welcome my friend ^^. Best of luck in your project!

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

11 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

Related Questions

Make a simple tree 1 Answer

Why is a child object not rotating properly with parent? 1 Answer

Child world location 3 Answers

First Person Camera not being child of the character/ Set child's rotation to global rotation 2 Answers

How do I have a Child tell a Parent to move without the Child itself moving 0 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