Need help with line that extends to the cursor and rotates towards it

Hi!

I’m an absolute newbie to unity and I thought I’d ask for help for a problem I’ve been having trouble with.

I’ve been trying to create a line that creates new segments and extends and rotates towards the cursor, but I’m having a little bit of trouble.

I managed to get it working horizontally the way I want it to, but I’m having a bit of trouble to add vertical movement too.

I figured I’d ask for help here because I couldn’t find many answers for other places, and this is where 90% of the answers online came from.

Thanks!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AddNewGameObjects : MonoBehaviour
{
    public List<GameObject> newGameObjects = new List<GameObject>();

    public Vector2 mousePos = new Vector2();

    public float xPos;
    public float yPos;
    public GameObject newGameObject;

    SpriteRenderer spriteRenderer;
    public Sprite sprite;

    public bool createNewSprite = false;

    public int newGameObjectCount;

    public bool mouseMoved;

    public GameObject parentGameObject;



    // Start is called before the first frame update
    void Start()
    {
        for (int i = 0; i < 2; i++)
        {

            CreateNewGameObject();

            if (i == 0)
            {
                newGameObjects[i].transform.position = new Vector3(-8, 0, 0);
            }
            else if (i == 1)
            {
                newGameObjects[i].transform.position = new Vector3(-3, 0, 0);
            }
        }

    }
    // Transform of the object we want to stretch

    // Update is called once per frame

    void Update()
    {
        if (Input.GetAxis("Mouse X") != 0)
        {
            mouseMoved = true;
        }

        xPos = Camera.main.ScreenToWorldPoint(Input.mousePosition).x;

        if (newGameObjects[newGameObjects.Count - 1].transform.position.x - newGameObjects[0].transform.position.x > 1)
        {
            CreateNewGameObject();
        }

        for (int i = 1; i < newGameObjects.Count; i++)
        {
            newGameObjects[i].transform.position = (new Vector3(xPos - i - 1,  0, 0));
        }


        if (newGameObjects[newGameObjects.Count - 1].transform.position.x <= -8 && newGameObjects.Count > 2 && mouseMoved == true)
        {
            GameObject gameObjectToDestroy = newGameObjects[newGameObjects.Count - 1];
            newGameObjects.RemoveAt(newGameObjects.Count - 1);
            Destroy(gameObjectToDestroy);
        }

        mouseMoved = false;
    }


    public void CreateNewGameObject()
    {
        newGameObject = new GameObject();
        newGameObject.AddComponent<SpriteRenderer>();

        spriteRenderer = newGameObject.GetComponent<SpriteRenderer>();
        spriteRenderer.sprite = sprite;

        newGameObject.transform.localScale = new Vector2(10, 10);

        newGameObject.transform.parent = parentGameObject.transform;

        newGameObjects.Add(newGameObject);




    }
}

Getting this working on the y axis shouldn’t take any extra code. Rather than managing x and y positions separately, track them via a combined Vector2 struct. All calculations can be done on the combined struct, rather than each x and y value separately.

Though if you want something to track the mouse with a delay/offset, this is generally done by having a collection of a certain length. You put the current position into one end, and take the last position out the other end. This is where a Queue<T> collection is probably the most suitable, limiting it to a certain length.

Then you effectively have a trail of point that lead towards the cursor, without any need to do any real calculations aside from converting it to a world position.

Thank you!

Can I have some example code if that’s possible? doesn’t have to be specifically this program or anything, just how to make a combined vector2 struct if that’s fine so i have an idea to get started. If not then that’s totally fine too.

I would probably go the collection method.

I put together a simple package to explain the idea:
ObjectCursorFollow.unitypackage (3.4 KB)

More work would need to be put in to get a more smooth and consistent mouse follow.

This is definitely enough to get me started! Thank you!

1 Like