- Home /
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 :)
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.
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.
Have mouse look script on body and trigger the rotation only when head turned 35 degrees.
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);
Thanks man :)
With a little tweak i was able to make it behave exactly how i needed :D
You're very helpful, thank you :) :) :)
You are most welcome my friend ^^. Best of luck in your project!
Your answer
Follow this Question
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