- Home /
Link object rotation to its parent
I am reasonably new to programming in unity and I have been stuck with this issue for a couple of days now...
What I want is for an object that is instantiated on the press of a button then set to the child of the FPS Controller to always be facing the camera when you look around.
My current code is this (Its probably very messy):
var objectOut = false;
var prefabs : GameObject[];
var maxDistance = 5;
var minDistance = 1;
var obj;
function Update () {
Screen.lockCursor = true;
if(Input.GetKeyUp("1") || Input.GetKeyUp("2") || Input.GetKeyUp("3") || Input.GetKeyUp("4")){
if(objectOut == false){
objectOut = true;
if(Input.GetKeyUp("1")){
obj = Instantiate (prefabs[0], Vector3(0, 0, 0), Quaternion.identity);
}else if(Input.GetKeyUp("2")){
obj = Instantiate (prefabs[1], Vector3(0, 0, 0), Quaternion.identity);
}else if(Input.GetKeyUp("3")){
obj = Instantiate (prefabs[2], Vector3(0, 0, 0), Quaternion.identity);
}else if(Input.GetKeyUp("4")){
obj = Instantiate (prefabs[3], Vector3(0, 0, 0), Quaternion.identity);
}
obj.transform.collider.enabled = true;
obj.layer = 2;
obj.transform.parent = gameObject.transform;
obj.transform.rotation = gameObject.transform.rotation;
obj.renderer.material.color.a = 0.5;
var Hit : RaycastHit;
Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), Hit);
obj.transform.localPosition = Vector3(0,2,1);
}else{
Destroy(obj);
objectOut = false;
}
}
if(objectOut){
if(Input.GetAxis("Fire1")){
objectOut = false;
obj.transform.parent = null;
obj.layer = 0;
obj.transform.collider.enabled = true;
}else{
var rcHit : RaycastHit;
var theRay : Vector3 = obj.transform.TransformDirection(Vector3.down);
if (Physics.Raycast(obj.transform.position, theRay, rcHit)){
var GroundDis = rcHit.distance;
obj.transform.rotation = Quaternion.FromToRotation(Vector3.up, rcHit.normal);
obj.transform.localPosition.y = (obj.transform.localPosition.y - GroundDis) + obj.collider.bounds.size.y / 2;
}
if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), Hit, maxDistance)){
if(Hit.distance >= minDistance){
obj.transform.position.x = Hit.point.x;
obj.transform.position.z = Hit.point.z;
}else{
obj.transform.localPosition.z = minDistance;
}
}else{
obj.transform.localPosition.z = maxDistance;
}
}
}
}
When I add this:
obj.transform.localRotation.w = 0;
which I would expect to work, it results in some weird rotation of the object and even the object sinking through the ground when I move it around on slopes...
Any help would be appreciated!
You do not want to be messing around with the localRotation Quaternion unless you really know Quaternions.
Yeah, don't set rotation directly. Unity doesn't use a classic rotation method, it uses Quaternions, which avoid the issue of gimbal lock.
You could try: https://docs.unity3d.com/Documentation/ScriptReference/Quaternion.LookRotation.html
I actually haven't tried anything else because I don't know what to try... And thanks for the tip :D
Invertex, that works in the sense that it keeps my object facing me (or away from me) but it loses the rotation to fit the ground, how would I make it do both?
For the upwards direction, use transform.up ins$$anonymous$$d. Should work I believe...
Answer by Demigiant · Jan 26, 2014 at 11:31 AM
Inside LateUpdate (so you're sure it's executed before camera render but after the parent transform has changed - if he uses Update), just write:
myFacingObject.transform.LookAt(myCamera.transform, Vector3.up);
Also, since you would be using your object's and your camera's transforms at every update, I'd suggest caching them in a couple of variables.
This works but it ends up with results like this: http://i.imgur.com/aO0k$$anonymous$$qj.png where I want it to link to the terrain like this: http://i.imgur.com/L3opTBS.png
Sorry, didn't understand that that was what you meant. In that case, you'd have to calculate a vector perpendicular to the terrain, so you can pass it ins$$anonymous$$d of Vector3.up
Your answer
Follow this Question
Related Questions
using a custom rotation in "Instantiate" 4 Answers
How do you flip a card with smooth rotation? 1 Answer
Character Rotation Now Matching GameObject's Local Rotation 0 Answers
Trying to rotate and move a gameobject through JavaScript, help? 1 Answer
Recording hand gestures and playing it back on same hand model 1 Answer