- Home /
Making the 'transform.rotation' of an object the 'transform.rotation' of another object
Now, the situation I'm trying to solve is a bit more complicated than what the title says;
In my scene, I have a Sphere (which is supposed to be your 'companion', it aims where you aim and moves where you move), the problem here is that the First Person Controller only rotates on the Y axis, while the Main Camera of the First Person Controller rotates only on the X axis when you aim with the Mouse. In my scene there's a button too, which 'activates' the sphere, which starts to follow you and aim where you do.
To make this work, I put the Sphere inside an EmptyGameObject that copies the position of the Player, and I put the Sphere a bit far from it, so that when it follows the Player the Sphere is not inside it, I wrote a Script for it, and it works, infact the position is not a problem, now the problem is the rotation of the Sphere itself, infact I don't want the EmptyGameObject to rotate, i want the Sphere itself to rotate when the Player does (I have two Cubes attached to the Sphere so that I can see if the Sphere is actually rotating);
I wrote this code:
using UnityEngine;
using System.Collections;
public class SphereGO : MonoBehaviour {
public bool isSphereGameObject;
public bool isSphereEmptyGameObject;
public GameObject yRotationObject;
public GameObject xRotationObject;
public static bool sphereFollowPlayer;
public GameObject followObject;
// Use this for initialization
void Start () {
sphereGoFollow = false;
}
// Update is called once per frame
void Update () {
if(isSphereEmptyGameObject){
if(sphereFollowPlayer == true){
transform.position = Vector3.Lerp (this.transform.position, followObject.transform.position, 0.1f);
}
}
if(isSphereGameObject){
if(sphereFollowPlayer == true){
transform.localPosition = Vector3.Lerp (this.transform.localPosition, new Vector3(1.169f,1.582f,1.12f), 0.1f);
transform.rotation = Quaternion.Euler(xRotationObject.transform.rotation.x, yRotationObject.transform.rotation.y, transform.rotation.z);
}
}
}
}
All the code that is written inside the 'isSphereEmptyGameObject' works for the EmptyGameObject, and all the code written inside the 'isSphereGameObject' works for the Sphere itself, so I didn't need to create two separate scripts for the EmptyGameObject and the Sphere.
Now the main problem, the Sphere doesn't rotate, or better perhaps, the Sphere rotates a TINY bit when I rotate my view. What's the problem?
Answer by KellyThomas · Dec 14, 2013 at 12:13 PM
Transform.rotation is a Quaternion, but you are taking it's properties and treating them as Euler angles
Yeah, I knew there was something wrong with Quaternion.Euler, but how can I solve my problem? What should I write ins$$anonymous$$d?
Quaternion.eulerAngles
is the euler angle representation... so you can use Transform.rotation.eulerAngles
to access x,y,z euler angles from a tranform.
If I put:
Transform.rotation.eulerAngles = new Vector3(xRotationObject.transform.rotation.x, yRotationObject.transform.rotation.y, transform.rotation.z);
I get an error saying that an object reference is required to access non-static member 'UnityEngine.Transform.rotation'; I already put static on the GameObjects but it's still giving me the error.
transform.rotation = Quaternion.Euler(
xRotationObject.transform.rotation.eulerAngles.x,
yRotationObject.transform.rotation.eulerAngles.y,
transform.rotation.eulerAngles.z);
Your answer
Follow this Question
Related Questions
Mouse won't lock 1 Answer
Door script not working when door is turned 90 degrees 1 Answer
Scroll Bar 1 Answer
FindGameObjectsWithTag not working 1 Answer
Animation not working 1 Answer