Answers for "unity 2d object follow mouse"

C#
0

unity 2d object follow mouse

using UnityEngine;
using System.Collections;
 
public class MouseMove2D : MonoBehaviour {
 
    private Vector3 mousePosition;
    public float moveSpeed = 0.1f;
 
    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
        if (Input.GetMouseButton(1)) {
            mousePosition = Input.mousePosition;
            mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
            transform.position = Vector2.Lerp(transform.position, mousePosition, moveSpeed);
        }
 
    }
}
Posted by: Guest on August-22-2021

C# Answers by Framework

Browse Popular Code Answers by Language