- Home /
Rotate player when aim
Hello, I I used this piece code from the tutorial:
private void UpdatePlayerRotationWhenAim()
{
mouseWorldPosition = Vector3.zero;
Vector2 screenCenterPoint = new Vector2(Screen.width / 2f, Screen.height / 2f);
Ray ray = Camera.main.ScreenPointToRay(screenCenterPoint);
if (Physics.Raycast(ray, out RaycastHit raycastHit, 999f, aimColliderLayerMask))
{
mouseWorldPosition = raycastHit.point;
}
if (starterAssetsInputs.aim)
{
Vector3 worldAimTarget = mouseWorldPosition;
worldAimTarget.y = transform.position.y;
Vector3 aimDirection = (worldAimTarget - transform.position).normalized;
transform.forward = Vector3.Lerp(transform.forward, aimDirection, Time.deltaTime * 20f);
}
}
Problem is, that u must create invisible objects around the map, or it doesnt work properly (When I not aim on object, rotation is set wrongly)...
Is there another way, how can I rotate player with the camera when player is aiming?
Comment
Your answer
