Forum rules - please read before posting.

Moving the player to face the mouse pointer

Is there a way to have the player turn to always face the mouse pointer? As you move the pointer, the player turns.

This would need to be disableable as required.

I thought I saw a setting, but can't find it now.

Olly

Comments

  • The closest setting to this is to have the Player turn their head to face the active Hotspot. To have them face the pointer would involve scripting, as you'd need to convert the cursor in screen-space to a 3D position using raycasting and detecting colliders in the scene.

    For example:

    using UnityEngine;
    
    namespace AC
    {
    
        public class PlayerFaceCursor : MonoBehaviour
        {
    
            public LayerMask collisionLayer;
            public bool playerFaces;
    
            private Ray ray;
            private RaycastHit hit;
    
            public void AllowFacing ()
            {
                playerFaces = true;
            }
    
            public void PreventFacing ()
            {
                playerFaces = false;
            }
    
            private void Update ()
            {
                ray = KickStarter.CameraMain.ScreenPointToRay (KickStarter.playerInput.GetMousePosition ());
                if (Physics.Raycast (ray, out hit, KickStarter.CameraMain.farClipPlane, collisionLayer))
                {
                    SetPosition (hit.point);
                }
    
                if (playerFaces)
                {
                    KickStarter.player.SetLookDirection (transform.position - KickStarter.player.transform.position, false);
                }
            }
    
            private void SetPosition (Vector3 targetPosition)
            {
                float distanceFromCamera = (targetPosition - KickStarter.CameraMainTransform.position).magnitude;
                distanceFromCamera = Mathf.Clamp (distanceFromCamera, KickStarter.CameraMain.nearClipPlane, KickStarter.CameraMain.farClipPlane);
    
                transform.position = KickStarter.CameraMainTransform.position + (ray.direction.normalized * distanceFromCamera);
            }
    
        }
    
    }
    
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.