- Home /
Rotating an object based on the side the player is looking at
I would like to script this behavior: The player should be able to rotate a cube 90 degrees to the left or up (depending on his input), based on the side the player is looking at. In other words in the players point of view the cube should rotate to the left or up.
So the problem with this is, that based on the rotation the cube already has and the side the player approaches the cube, the axis around which the cube has to rotate changes. How can I identify the axes around which the script should rotate the cube?
Answer by stardust · Nov 20, 2013 at 11:14 AM
After I got Vector3.Cross and RaycastHit.normal explained to me by my prof I could figure out the solution by myself. So with this script the player can rotate an object to the left and up, based on how the player is looking at the object. From the players point of view the rotation is always to the left and up. This script is placed on the player.
using UnityEngine;
using System.Collections;
public class RotateTarget : MonoBehaviour {
public string rotateLeftButton;
public string rotateUpButton;
//Rotate the given object to the left
//For this I use the gravity's vector, because the player is always affected by gravity in my game
void RotateCubeLeft(Transform rayHit) {
rayHit.Rotate(Physics.gravity.normalized*(-90), Space.World);
}
//Rotate the given object up
//With Vector3.Cross we get the third axis from the gravity and the normal the player is pointing at
void RotateCubeUp(Transform rayHit, Vector3 hitNormal) {
Vector3 rotationVec= Vector3.Cross(Physics.gravity, hitNormal).normalized;
rayHit.Rotate(rotationVec*90, Space.World);
}
// Update is called once per frame
void Update () {
RaycastHit hit;
// Raycast detects if the player is close enough to the object to be rotated
if(Physics.Raycast(transform.position,transform.forward, out hit, 5)) {
Debug.DrawLine(transform.position, hit.point);
//Rotate the hit object to the left, based on the side(normal) the player is pointing at
//I only want this to work with objects tagged with "Cube"
if(Input.GetButtonDown(rotateLeftButton)) {
if(hit.transform.tag=="Cube") {
RotateCubeLeft(hit.transform);
}
}
//Rotate the hit object up, based on the side(normal) the player is pointing at
//I only want this to work with objects tagged with "Cube"
if(Input.GetButtonDown (rotateUpButton)) {
if(hit.transform.tag=="Cube") {
RotateCubeUp(hit.transform, hit.normal);
}
}
}
}
}
Answer by robertbu · Nov 16, 2013 at 04:11 PM
You can rotate in the global rather than the local space. Using Transform.Rotate(), you can add the 'Space.World' parameter for an instant rotation:
transform.Rotate(Vector3.up -90.0, Space.World);
Over time, it is easier to get an accurate rotation using Quaternions. At the start you calculate the rotation:
var dest = Quaternion.AngleAxis(Vector3.up, -90.0) * transform.rotation;
Then in Update():
transform.rotation = Quaternion.RotateTowards(transform.rotation, dest, speed * Time.deltaTime);
I added a fancy picture to illustrate the problem. I already used Space.World, and as you can see this solves the problem of the previously rotated cube (the global axes in every row are the same).
But there is still the problem, that if the player looks at the cube from another side, the local and the global axes around which the cube should be rotated change. Correct me if I'm wrong...
I also thought about comparing the cubes and the players position, but i think that wouldn't work either...
I'll give the problem more of a look later when I have more time, but I'm puzzled about the goal here. Is the Left or Up rotation always be relative to how the character is viewing the cube? If so, then the axis of rotation will be either be the character's up or the character's right depending on which way you are doing the rotation. Ins$$anonymous$$d of 'Vector3.up' in the 'dest' calculation, you would use something like 'character.up'.
Yes exactly, it should always be relative to how the character is viewing the cube, because it may be very confusing for the player in a first person view if it wouldn't always behave the same from his point of view.
You can't use character.left because the player could stand somewhere and still point at the object while his angle would indicate something else (for example if he is standing very close to the object and looking along its side).
Your answer
Follow this Question
Related Questions
Smooth rotation about global axis instead of local axis. 1 Answer
rotate object around another object 1:1 1 Answer
2D Rotate object so y axis faces other object 1 Answer
Rolling a capsule lengthways, sideways and twisting with AddTorque 1 Answer
Rotate object based on another rotation above a certain threshhold 1 Answer