- Home /
How to rotate an object according to the camera's view?
I HAVE ATTACHED A DIAGRAM DISPLAYING WHAT I MEAN
Basically, my goal is this: I want a script I can attach to an object that rotates it according to the camera's view. As in, if a cube with the script attached is in front of the camera, and I press W, it rotates upward so that according to the camera, the cube face that was facing the camera is now facing north, the cube face that was facing south is now facing the camera, etc. I've been able to almost do this using quaternion.angleAxis and then lerping, but it rotates the object according to its own orientation, not the cameras, and that leaves the rotation feeling random as it does not act like I described above. I would very much appreciate any help! Thanks.
Answer by coinnip_unity · Aug 20, 2018 at 09:14 AM
Here Simple code for rotate your object considering camera view
if the camera transform is rotated, this makes your cube rotate correct direction for view
if (Input.GetKeyDown("a"))
{
cubeTransform.Rotate(cameraTransform.up, 90, Space.World);
}
if (Input.GetKeyDown("d"))
{
cubeTransform.Rotate(cameraTransform.up, -90, Space.World);
}
if (Input.GetKeyDown("w"))
{
cubeTransform.Rotate(cameraTransform.right, 90, Space.World);
}
if (Input.GetKeyDown("s"))
{
cubeTransform.Rotate(cameraTransform.right, -90, Space.World);
}
"cubeTransform" for your object to rotate,
"cameraTransform" for your camera object's transform
code tested on 2017.3.1p4
Thanks so much! You nailed it! The only thing is I'm trying to get it rotate over time as well, just so it's smoother, you know? I'll post my completed code as soon as I do it, but thanks so much.
This is how I got the code above working exactly like it normally does, but rotating over time. I'm sure there are way better ways of doing this, but this is what works for me and its late lol.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class rotating : $$anonymous$$onoBehaviour {
public bool ableToRotate = true;
public Camera camera;
private bool A$$anonymous$$eyRotate;
private bool W$$anonymous$$eyRotate;
private bool S$$anonymous$$eyRotate;
private bool D$$anonymous$$eyRotate;
private int workingInt;
private int workingIntLimit = 6;
void Start(){
camera = Camera.main;
}
void Update ()
{
if (ableToRotate)
{
if (Input.Get$$anonymous$$eyDown("a"))
{
A$$anonymous$$eyRotate=true;
}
if (Input.Get$$anonymous$$eyDown("d"))
{
D$$anonymous$$eyRotate = true;
}
if (Input.Get$$anonymous$$eyDown("w"))
{
W$$anonymous$$eyRotate = true;
}
if (Input.Get$$anonymous$$eyDown("s"))
{
S$$anonymous$$eyRotate = true;
}
}
if (A$$anonymous$$eyRotate) {
ableToRotate = false;
gameObject.transform.Rotate(camera.transform.up, 15, Space.World);
workingInt++;
if (workingInt >= workingIntLimit) {
A$$anonymous$$eyRotate = false;
workingInt = 0;
ableToRotate = true;
}
}
else if (D$$anonymous$$eyRotate) {
ableToRotate = false;
gameObject.transform.Rotate(camera.transform.up, -15, Space.World);
workingInt++;
if (workingInt >= workingIntLimit) {
D$$anonymous$$eyRotate = false;
workingInt = 0;
ableToRotate = true;
}
}
else if (W$$anonymous$$eyRotate) {
ableToRotate = false;
gameObject.transform.Rotate(camera.transform.right, 15, Space.World);
workingInt++;
if (workingInt >= workingIntLimit) {
W$$anonymous$$eyRotate = false;
workingInt = 0;
ableToRotate = true;
}
}
else if (S$$anonymous$$eyRotate) {
ableToRotate = false;
gameObject.transform.Rotate(camera.transform.right, -15, Space.World);
workingInt++;
if (workingInt >= workingIntLimit) {
S$$anonymous$$eyRotate = false;
workingInt = 0;
ableToRotate = true;
}
}
}
}