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.
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.
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);
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.