Rotate an object around its Y axis

hey,

i want to rotate an object around its y axis so that it points to my mouse position. i tested so many different cases but i cant find a simple solution.

my best bet was this code:

Vector3 MouseWorldPosition = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, transform.position.z));
		transform.LookAt (MouseWorldPosition);
		transform.rotation = Quaternion.Euler (new Vector3 (0, transform.rotation.eulerAngles.y, 0));

but its just completly wrong. i have big problems in understanding vector math :confused:

any ideas ?

sitting here since 3 hours now :/,
still not getting the correct rotation. and it seems so simple :X

well atleast iam able to draw a ray from the object(player) to my mouse

Vector3 mPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,transform.position.z));
	Debug.DrawRay(transform.position, mPos-transform.position,Color.red);

any idea how i can tell my player object to use this rotation ?

This should work, but only rotates 1 direction because I’m starting to learn how to write scripts myself. But when you attach this code to any object, and you move your mouse from left to right or from right to left, the object rotates around its Y-axis. Make the speedDamp value like 100 and you’ll see it moves.

var rotateMultiplier = 4;
var speedDamp : float = 1.0;

function Update() {



    if (Input.GetAxis ("Mouse X"))

    	transform.Rotate(0, Time.deltaTime/rotateMultiplier*speedDamp, 0);



}

hey,
thank you for the reply but i dont see how this code should work.

delta time is always the same if your fps doesn’t change so that means if your fps is constant the Y value wont change at all.

iam looking for a way that i can adjust the rotation from an object based on a direction.

This is what the script reference says about Time.deltaTime:

  • Use this function to make your game frame rate independent.

But I’m not sure what you’re trying to achieve, you’re trying to make an object rotate on its Y-axis based on the position of the player?

think of an RTS hack and slash game.

you can see your “Hero Player Unit” and it should always face in the direction of your mouse.

(You control the Hero rotation by your mouse postion)

never played diablo but i think it should be the same.

Ok I think I get it. But I don’t see why you can’t do it with transform.Rotate. Takes a bit more research I guess but it shouldn’t be too hard I think.

In order to get a object to look at the curser, you can cast a raycast from the camera onto the terrian. And have it look at the point where it hit.

The name of the object looking at the point is called ‘cube’ in my case but it can be changed to fit whatever the name is of the character. You can also change how far the raycast reaches by changing its distance (the distance is what 300 is). You can attach this script to your camera.

function Update () {

var ray : Ray; 
ray = Camera.main.ScreenPointToRay(Input.mousePosition);

var hit : RaycastHit;


if(Physics.Raycast(ray,hit,300)){

var cube = GameObject.Find("Cube");

cube.transform.LookAt(hit.point);

}

yeah that was my first try too.

there is one minor problem that the “cube” doesnt only change its Y rotation, it changes all axes.

but one huge problem is that if my mouse is above the cube, the cube begins to teleport around, and i dont know how to fix that.

ok then zero out the x rotation and z rotation after the transform.LookAt command.

function Update () {

 
var ray : Ray; 

ray = Camera.main.ScreenPointToRay(Input.mousePosition);

var hit : RaycastHit;

if(Physics.Raycast(ray,hit,300)){

var cube = GameObject.Find("Cube");

cube.transform.LookAt(hit.point);

cube.transform.rotation.z = 0;
cube.transform.rotation.x = 0;

}
}

You may want something more smoother than transform.LookAt. Then you would use another method to make the object turn smoothly around.

var turnspeed = 3;

function Update () {

var ray : Ray; 
ray = Camera.main.ScreenPointToRay(Input.mousePosition);

var hit : RaycastHit;


if(Physics.Raycast(ray,hit,300)){

var cube = GameObject.Find("Cube");

//this sets the direction of where we want to look. Subtracting the objects position of what we want to look 
//at with the object we want facing it creates a direction. See lookRotation in scripting reference for more info.
var playerRotatedirection = Quaternion.LookRotation(hit.point - cube.transform.position);

//this tells the cube to actually move from its transform.rotation and face the direction we want it to face.		 
cube.transform.rotation = cube.transform.rotation = Quaternion.Lerp(cube.transform.rotation,playerRotatedirection,turnspeed*Time.deltaTime); 

//zeros out the x and z rotations so it only turns on y
cube.transform.rotation.x = 0;
cube.transform.rotation.z = 0;
}
}

thanks it works like a charm now.

however you have to make a dummy var, dunno why.

if (Physics.Raycast (ray, out hit, 300)) {
			transform.LookAt(hit.point);
			Quaternion dummy = transform.rotation;
			dummy.x = 0;
			dummy.z= 0;
			transform.rotation = dummy;
			
		}

not sure what you mean by dummy var. But if it works then great :).

also look for this solution: http://unifycommunity.com/wiki/index.php?title=Click_To_Move