How to get the rotation value?

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.

Anyone?

I’m having a difficult time understanding what the end result is that you’re trying to do. Let me know if this is right:

You have a character (sphere) in the middle of your screen. You have a camera that is looking at the character from some distance away. When you move your mouse left and right, you want the character to turn, but you always want the character’s back to the camera.

Is that close?

Close, but not exactly.

The “character” is a sphere which rolls. And i want it to roll away from the camera. And i want to be able to turn the camera around the character. so the character doesnt really have a back. But it has to move the way the camera faces, when u press the up button, for example.

So i dont want the character to turn i want the camera to turn and the charater rolling in the direction the camera is aimed at

Hope its more clear now?

I think it’s not so much a code problem as a logic problem. You don’t want to figure out the angle of the camera to the sphere (doesn’t make much sense anyway). What you want to do is apply force to the sphere in the direction the camera is facing (its forward vector).

Ok thanks your right, I give that a try! :slight_smile:

transform.rotation is a quaternion, it’s not euler angles. A quaternion is a 4D representation and the x/y/z/w elements range from -1.0 to 1.0.

–Eric

@Eric

Thanks I did not know that, I knew what the Quaternion is good for, but i didnt know how it works and stuff like that.

I think there was a ball rolling script in the asset store. You may want to check it out.