Rotate player to look at something

I would like my player to change his y rotation so that he is facing a certain transform.
I tried this:

		if(Input.GetMouseButton(1) || Input.GetMouseButton(0))
		{
			SoldierController.soldierTransform.localRotation.eulerAngles.y = -transform.localRotation.eulerAngles.y;
		}

But it doesn’t look look at the target directly and it glitches out alot.

I know there is a way to transform a vector3 ( target position ) to a eulerangles direction or something.

Thanks,
Dusk

http://unity3d.com/support/documentation/ScriptReference/Transform.LookAt.html

Please read the post.

“I would like my player to change his y rotation so that he is facing a certain transform.”

this just changes all the rotations.

Use LookAt but have a line afterward that set’s the x and z eulerAngles back to 0.

I would use LookAt on a temporary Transform variable whose position/rotation is set to the same as your soldier transform. Then: “SoldierController.soldierTransform.localRotation.eulerAngles.y = myTempTransform.localEulerAngles.y”

I think this would work better than setting x and z to zero, because I don’t know that they were set to 0 in the first place.

I can’t test this right now, but it should be quick to try.

heh rotations…

I hate writing rotation stuff off the top of my head but I think it should go like this… If you want it to snap rather rotate over time, get rid of the RotateTowards stuff.

Vector3 relativePos = CurrentTarget.position - transform.position;
Quarternion rot = Quarternion.LookRotation(relativePos);

transform.rotation = Quaternion.RotateTowards( transform.rotation, Quarternion.Euler(0, rot.y, 0) , 1);
var lookAtPos = target.position;
lookAtPos.y = transform.position.y; //set y pos to the same as mine, so I don't look up/down
transform.LookAt(lookAtPos);

If you only need to rotate around Y, and no other axis NEVER, this other solution i posted in my thread also works :stuck_out_tongue:

// get Around Y rotation
		float x = target.position.x - thisTransform.position.x;
		float y = target.position.z - thisTransform.position.z;
		//float hip = Vector3.Distance(target.position, thisTransform.position);
		float newAngleY = Mathf.Atan2(x, y);
		newAngleY *= Mathf.Rad2Deg;
		thisTransform.Rotate(0,newAngleY,0);

you may want to check atan2():

http://forum.unity3d.com/threads/120308-implementing-a-manual-LookAt()

well, I’m reverse engineering the bootcamp demo, and adding to the soldiercontroller, but it doesnt seem to like the above suggestions.

		var currentAngle = soldierTransform.localRotation.eulerAngles.y;
		var delta = Mathf.Repeat ((targetYRotation - currentAngle), 360);
		if (delta > 180)
			delta -= 360;
		soldierTransform.localRotation.eulerAngles.y = Mathf.MoveTowards(currentAngle, currentAngle +  delta, Time.deltaTime  *  maxRotationSpeed);
	}
	
	function GetUserInputs()
	{		
		if(Input.GetMouseButton(1) || Input.GetMouseButton(0))
		{		
		soldierTransform.LookAt(target);
		soldierTransform.localRotation.eulerAngles.x = 0;
		soldierTransform.localRotation.eulerAngles.z = 0;
		}

The get user inputs part with LookAt works decent, but if I uncomment the

soldierTransform.localRotation.eulerAngles.y = Mathf.MoveTowards(currentAngle, currentAngle +  delta, Time.deltaTime  *  maxRotationSpeed);

Then it starts to glitch out. Its like in one part the script is telling it to rotate here but in another part its saying to stay at its current rotation.

Also, I would like to smooth the rotation instead of just snapping it to look at the target. But I want to fix this current issue first.

I seem to have gotten it to work. But to an extent.

		var relativePos = target.position - transform.position;
		
		var rotationToTarget = Quaternion.LookRotation(relativePos);
		
		if(Input.GetMouseButton(1) || Input.GetMouseButton(0))
		{
			transform.rotation = rotationToTarget;
			print(rotationToTarget);
		}
		
		else
		{		
			var currentAngle = soldierTransform.localRotation.eulerAngles.y;
			var delta = Mathf.Repeat ((targetYRotation - currentAngle), 360);
			if (delta > 180)
				delta -= 360;
			soldierTransform.localRotation.eulerAngles.y = Mathf.MoveTowards(currentAngle, currentAngle + delta, Time.deltaTime * maxRotationSpeed);
		}

When you left or right click, the player will snap and look at the target. But, when you let go the player will snap back to the position it is originally in. I know that the bottom half of the code is doing this, but I’m not sure how to prevent it. Removing the bottom half isn’t an option.

Thanks,
Dusk

bump?