How can I make the game object follow the mouse?
I want the newly instantiated game object (the name of the object is DragObjectCard) to follow the mouse, please help. I have tried multiple different methods but none worked. Is there any way to do this. Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CreateObjectDrag : MonoBehaviour
{
public GameObject DragObjectCard;
public GameObject ObjectCard;
public GameObject target;
public Vector2 position;
public void OnMouseDown()
{
if (Input.GetButtonDown("Fire1"))
{
GameObject newDrag = Instantiate(DragObjectCard);
newDrag.transform.position = new Vector2(ObjectCard.transform.position.x, ObjectCard.transform.position.y);
}
}
}
Answer by rage_co · Jul 07, 2021 at 07:27 AM
Have you tried using Input.mousePosition to get the mouse's position? also using GetButtonDown would snap the object directly so use GetButton() and Vector3.Lerp for help.... 1. Input.mousePosition 2. Input.GetButton 3. Vector3.Lerp
Im not sure exactly how to implement it so you can either figure it out yourself or wait for an more educated answer
Your answer
Follow this Question
Related Questions
Set One GameObject Inactive If Another GameObject Is Active? 1 Answer
Find active Child gameobject out of multiple gameobjects 2 Answers
After Rotating a Prefab, Transform.Position of children is inaccurate 1 Answer
Player follows mouse, but I need only Y 1 Answer
Z position moving in 2D space 1 Answer