Hello,
I’m not very familiar with Unity or JavaScript yet. And I just wrote a code to make my camera turn around my “character”.
I used an invisible cube which has the position of the character, which is the parent of the camera which functions as the “target”. So if the target rotates, the camera will rotate around the target. This responds to the position from the mouse.
This makes my camera turn around my character, depends on which way i move my mouse.
I made this up myself, I had problems with internet yesterday so i couldn’t find another way (Maybe its a weird way) but i needed a solution fast for a prototype.
My character is a sphere, which now moves by adding force when the arrow keys are pressed.
But now i want my sphere to move in the direction the camera is facing.
So i figured i first need the rotation of the target/camera to be able to calculate how much force has to be added to the character in which direction, depending on which way the camera faces.
Now the thing that is giving me a hard time is what i thought would be the easiest part.
I need to have the y rotation of the target. But I cant understand all the different rotation possibility’s in Unity.
How do i do that? Here is my code.
The part in Debug.Log is where i want to have the y rotation.
The problem is I get an y rotation here, but this one is different from the y rotation in the Inspector.
I get a rotation from -1 to 1. Which is separated over 2 full 360 turns. So 1 turn goes from 0-1 and then the next from -1 - 0. While the Inspector shows y rotation from 0-360. Which I want to use to calculate the force for the character.
var player : Transform;
var cameraRotationSpeed = 1.0;
static var mousePosition : Vector3;
function Update(){
playerPos = player.position;
transform.position = playerPos;
cameraRotation = (((Input.mousePosition.x) - (Screen.width/2))/4)*cameraRotationSpeed;
rotateCamera = cameraRotation;
transform.Rotate(Vector3(0.0,rotateCamera,0.0) * Time.deltaTime);
Debug.Log(transform.rotation.y);
}
Edit: While im at it, does anyone have a tip how most games have their camera move around the character. I found out this is kind of annoying, cause now when i move my mouse just slightly it already turns. How does it work for example with a shooter? Do they calculate the position in 3D space where the mouse is pointed at and then move the middle of the camera to that position?
Cause in shooters it is always easy to point at a place with your mouse and then your aim will be on that place. But now when i move my mouse it will keep moving until i put my mouse back in the exact middle.