- Home /
Object rotates with camera
Hi there,
I have written a script to pick up an object using unity's FPcontroller prefab. So far I have the object being picked up and dropped when using left click. if your holding the object and hold down the right mouse button the object moves to the center of the screen and rotates along the objects Y axis. This is all working fine except if I look up or down while holding the object, it sticks to its world rotate along the Z axis. So if I look up and rotate the object I am looking at the bottom of it. I want the objects Z axis to follow the camera so no matter what direction you're looking you can always see it from the front, not the bottom or top. Here is the code:
void PickupObject(){
transform.rotation = Quaternion.identity;
rigidbody.isKinematic = true;
Collider collider = gameObject.GetComponent<Collider> ();
collider.isTrigger = true;
rigidbody.gameObject.transform.position = Camera.main.ViewportToWorldPoint(DistanceFromCamera);
this.transform.parent = Camera.main.transform;
InteractObject script = GameObject.Find (thisObject).GetComponent<InteractObject>();
script.enabled = false;
PlayerRay rayScript = GameObject.Find ("FirstPersonCharacter").GetComponent<PlayerRay>();
rayScript.EnableRay (false);
}
void DropObject(){
isHitting = false;
//Debug.Log ("dropping");
rigidbody.isKinematic = false;
Collider collider = gameObject.GetComponent<Collider> ();
collider.isTrigger = false;
rigidbody.WakeUp ();
rigidbody.AddForce (go.transform.forward * throwForce, ForceMode.Impulse);
this.transform.parent = null;
InteractObject script = GameObject.Find (thisObject).GetComponent<InteractObject>();
script.enabled = true;
PlayerRay rayScript = GameObject.Find ("FirstPersonCharacter").GetComponent<PlayerRay>();
rayScript.EnableRay (true);
bool fpsScript;
}
void RotateObject(){
if (Input.GetButton ("Fire2")) {
DistanceFromCamera = new Vector3 (0.5f, 0.5f, DistanceHeldFromCamera);
rigidbody.gameObject.transform.position = Camera.main.ViewportToWorldPoint (DistanceFromCamera);
bool fpsScript;
fpsScript = GameObject.Find ("FPSController").GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController> ().enabled = false;
transform.eulerAngles=new Vector3(0,transform.eulerAngles.y,0);
transform.Rotate (0, (Input.GetAxis ("Mouse X") * RotationSpeed * Time.deltaTime), 0, Space.World);
} else if (Input.GetButtonUp ("Fire2")) {
DistanceFromCamera = new Vector3(0.5f,-0.0f,DistanceHeldFromCamera);
rigidbody.gameObject.transform.position = Camera.main.ViewportToWorldPoint (DistanceFromCamera);
fpsScript = GameObject.Find ("FPSController").GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController> ().enabled = true;
}
}
Any help on this is much appreciated. Thank you :)
Answer by Clintmcc · May 30, 2015 at 03:52 PM
I always feel silly but satisfied when I find the answer but its a mistake I had made.
I just needed to change the rotate line from Space.World to Space.Self.
Thanks Kuba for your contribution. Lookat was the answer to the first problem and Space.self the second problem. Thank you.
Here's the final code if anyone else gets stuck.
void RotateObject(){
if (Input.GetButton ("Fire2")) {
DistanceFromCamera = new Vector3 (0.5f, 0.5f, DistanceHeldFromCamera);
rigidbody.gameObject.transform.position = Camera.main.ViewportToWorldPoint (DistanceFromCamera);
bool fpsScript;
fpsScript = GameObject.Find ("FPSController").GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController> ().enabled = false;
transform.Rotate (0, (Input.GetAxis ("Mouse X") * RotationSpeed * Time.deltaTime), 0, Space.Self);
if(!looking){
transform.eulerAngles=new Vector3(0,transform.eulerAngles.y,0);
Transform target = Camera.main.transform;
transform.LookAt(target, Vector3.up);
looking = true;
}
} else if (Input.GetButtonUp ("Fire2")) {
DistanceFromCamera = new Vector3(0.5f,-0.0f,DistanceHeldFromCamera);
rigidbody.gameObject.transform.position = Camera.main.ViewportToWorldPoint (DistanceFromCamera);
bool fpsScript;
fpsScript = GameObject.Find ("FPSController").GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController> ().enabled = true;
looking = false;
}
}
Answer by KubaPrusak · May 28, 2015 at 01:09 PM
If you want the object to face the player you can use
transform.LookAt(Transform target, Vector3 worldUp)
where target would be the player Camera. worldUp is an optional parameter but you should set it to transform.forward; if the object doesn't rotate with the correct side facing the player, you can try using different Vectors (ie. transform.up, transform.right, transform.back etc).
transform.LookAt and all of the transform.forward/up's refer to the transform of the object you are holding, not the player.
Hi, thanks for your response.
This solution solves the problem but creates a new one. I lose the ability to rotate the object because the Lookat is locking it in place looking towards the camera.
void RotateObject(){
if (Input.GetButton ("Fire2")) {
DistanceFromCamera = new Vector3 (0.5f, 0.5f, DistanceHeldFromCamera);
rigidbody.gameObject.transform.position = Camera.main.ViewportToWorldPoint (DistanceFromCamera);
bool fpsScript;
fpsScript = GameObject.Find ("FPSController").GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController> ().enabled = false;
transform.eulerAngles=new Vector3(0,transform.eulerAngles.y,0);
transform.Rotate (0, (Input.GetAxis ("$$anonymous$$ouse X") * RotationSpeed * Time.deltaTime), 0, Space.World);
Transform target = Camera.main.transform;
transform.LookAt(target, Vector3.up);
} else if (Input.GetButtonUp ("Fire2")) {
DistanceFromCamera = new Vector3(0.5f,-0.0f,DistanceHeldFromCamera);
rigidbody.gameObject.transform.position = Camera.main.ViewportToWorldPoint (DistanceFromCamera);
bool fpsScript;
fpsScript = GameObject.Find ("FPSController").GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController> ().enabled = true;
}
}
Why dont you create a parent to the object you pick up ie.
Object
-Actual model
The Object is a parent of the actual model (ie mesh etc.)
The actual model is the mesh etc and the object is the thing you pick up in your script. Then keep the script as is but make the transform.LookAt reference the transform of the actual model.
So the object rotates with your script and the mesh faces the player.
There is probably a better way to do this but thats all I've got.
I got this solution working by ensuring it doesn't run every frame with a condition. What is happening now is that if your looking forward, you can rotate the object around it's Y Axis with no problem. Looking up or down and doing it though, the Y Axis changes on the object because its being rotated. I'm not sure if I'm explaining it very well so i'll include the code and a couple of images.
void RotateObject(){
if (Input.GetButton ("Fire2")) {
DistanceFromCamera = new Vector3 (0.5f, 0.5f, DistanceHeldFromCamera);
rigidbody.gameObject.transform.position = Camera.main.ViewportToWorldPoint (DistanceFromCamera);
bool fpsScript;
fpsScript = GameObject.Find ("FPSController").GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController> ().enabled = false;
transform.Rotate (0, (Input.GetAxis ("$$anonymous$$ouse X") * RotationSpeed * Time.deltaTime), 0, Space.World);
if(!looking){
transform.eulerAngles=new Vector3(0,transform.eulerAngles.y,0);
Transform target = Camera.main.transform;
transform.LookAt(target, Vector3.up);
looking = true;
}
//transform.rotation = Quaternion.LookRotation(target, Vector3.up);
} else if (Input.GetButtonUp ("Fire2")) {
DistanceFromCamera = new Vector3(0.5f,-0.0f,DistanceHeldFromCamera);
rigidbody.gameObject.transform.position = Camera.main.ViewportToWorldPoint (DistanceFromCamera);
bool fpsScript;
fpsScript = GameObject.Find ("FPSController").GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController> ().enabled = true;
looking = false;
}
}
The parenting to an empty GO might work. I'll give this a try tomorrow and update the post. Thanks for the effort :)
Your answer
Follow this Question
Related Questions
Viewmodel/camera rotation when moving 1 Answer
Movement with camera and rotation of objects 1 Answer
How to rotate my camera? 2 Answers