Forum rules - please read before posting.

Cursor Orientation pointing North(fwd) /South(bkwd)/East (Left)/West (Right)

Is it possible to have the cursor orient to the direction that it is on the side of.

I have made this script but unsure if it will work within AC or where it should be attached to thanks.

using UnityEngine;

public class CursorDirection : MonoBehaviour
{
// Previous position for direction calculation.
private Vector3 previousPosition;

// Speed of the rotation interpolation.
public float lerpSpeed = 5.0f;

void Start()
{
    // Initialize previous position at the start.
    previousPosition = transform.position;
}

void Update()
{
    // Calculate direction of movement.
    Vector3 direction = transform.position - previousPosition;

    // Check for significant movement.
    if (direction.magnitude > 0.1f)
    {
        // Calculate the angle in degrees.
        float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;

        // Target rotation based on the movement direction.
        Quaternion targetRotation = Quaternion.Euler(0, 0, angle);

        // Smoothly interpolate the rotation.
        transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, lerpSpeed * Time.deltaTime);

        // Update previous position.
        previousPosition = transform.position;
    }
}

}

Comments

  • The script above relates to a Transform, i.e. a physical object in 3D space. The cursor, by default, is a separate element drawn atop the scene in 2D.

    You can have the cursor act in 3D space by making use of the included "World Space Cursor Example" component - see the Manual chapter by the same name for details - which the above could also be attached to, but I'm not sure of the behaviour you're ultimately looking to achieve. Can you elaborate with screenshots / mockups?

  • edited December 2023

    This should help in the explanation. As the player moves the mouse it orients to the direction the cursor is going. So in the image, the player is moving the cursor from East to South. Hopefully this makes sense. Thx Chris
    https://imgur.com/a/pHXo4VS

  • edited December 2023

    So the cursor changes to a South graphic when at the bottom of the scene, North when at the top etc?

    Your script doesn't account for any position on screen - only the amount it's moved by between frames, in world-space.

    If you use Unity UI for your cursor rendering, you can attach an Animator that controls which cursor graphic gets shown in its Raw Image component - see the Manual's "Unity UI cursor rendering" chapter for details.

    What you can do is create a Blend Tree to show up/down/left/right graphics based on "ScreenX" and "ScreenY" float parameters, and then use this script to set those parameter values based on the mouse's position on-screen (ranging from -1 to +1):

    using AC;
    using UnityEngine;
    
    public class CursorDirection : MonoBehaviour
    {
    
        public Animator _animator;
    
        void Update ()
        {
            Vector2 mousePosition = KickStarter.playerInput.GetMousePosition ();
            Vector2 screenPosition = new Vector2 (mousePosition.x / Screen.width, mousePosition.y / Screen.height);
            Vector2 centeredScreenPosition = new Vector2 (screenPosition.x * 2f - 1f, screenPosition.y * 2f - 1f);
            _animator.SetFloat ("ScreenX", centeredScreenPosition.x);
            _animator.SetFloat ("ScreenY", centeredScreenPosition.y);
        }
    
    }
    
  • Thank you very much @ChrisIceBox I will be working on this over the holidays.

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.