- Home /
Rotating an GameObject in a 2D plane based on clicking on a game object.
Hi,
I have been trying to solve a problem for a while and I feel like I am going in circles, so I thought I would try here.
Firstly, I apologise for the crudeness of the image.
I have a Circle (Blue) with a Game Object in the center of it (Black Circle). The Camera (Green) is a child of the Black Circle.
I am trying to rotate the camera, to the randomly instantiated Game Objects (Red) When they are selected, by rotating the Black Circle, however, I am running into difficulties when the Black Circles rotation value goes into negative values.
I haven't had great success with this so any help in pointing me in the right direction would be much appreciated.
Because you mention angles I assume you use Euler angles. You should know that angles are just a representation of actual rotation defined by Quaternions. So most operations like rotating the objects and making them look at some directions are best done with methods provided by quaternions.
So how do you rotate them? Can you post your code please?
Hi,
Thanks for your response. Currently I am using Quaternion.FromToRotation, although this code is considering 3D space. With this snippit, my camera rotation group always ends up at 0,0,0.
Camera myCamera = Camera.main;
Transform cameraRotator = myCamera.transform.parent.transform;
Vector3 currentPos = cameraRotator.position;
Vector3 targetDir = transform.position;
cameraRotator.rotation = Quaternion.FromToRotation(currentPos, targetDir);
Answer by PitVital · Jun 15, 2018 at 01:31 PM
Hi, so I managed to get something working very close to what I wanted, I think I can tune it in from here.
But I wanted to share my solution so here it is:
if (Input.GetMouseButtonDown(0))
{
// Getting Camera.
Camera myCamera = Camera.main;
// Get parent Transform from camera
Transform cameraRotator = myCamera.transform.parent.transform;
// Get positions of the building in question and the cameraRotator.
Vector3 currentPos = cameraRotator.position;
Vector3 targetDir = transform.position;
// Subtracting the Vector3's from each other.
Vector3 relativePos = targetDir - currentPos;
// Create the Quaternion we need to rotate by.
Quaternion rotationDelta = Quaternion.FromToRotation(cameraRotator.transform.up, relativePos);
// Nuke the Y and Z Axis back to zero on the delta
var angles = rotationDelta.eulerAngles;
angles.y = angles.z = 0f;
rotationDelta = Quaternion.Euler(angles);
// Multiply the values together.
Quaternion targetRotation = cameraRotator.rotation * rotationDelta;
// starting the routine that will allow us to lerp this aforementioned value.
StartCoroutine(Transition(cameraRotator, targetRotation));
}
IEnumerator Transition(Transform cameraRotator, Quaternion targetRotation)
{
float step = 0.0f;
while (step < 1.0f)
{
// The step size is equal to the speed times frame time.
step += Time.deltaTime * (Time.timeScale / transitionDuration);
cameraRotator.rotation = Quaternion.Lerp(cameraRotator.rotation, targetRotation, step);
yield return 0;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612164804im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Align camera and turret aim 0 Answers
Rotate an Object by a spezific value 2 Answers
Set Velocity of Object According to Rotation 2 Answers
How to rotate my object correctly? 1 Answer