Skip to content

Instantly share code, notes, and snippets.

@TheCuttlefish
Created November 12, 2020 16:29
Show Gist options
  • Save TheCuttlefish/8639e3170bb056b11208ed9041bb2a29 to your computer and use it in GitHub Desktop.
Save TheCuttlefish/8639e3170bb056b11208ed9041bb2a29 to your computer and use it in GitHub Desktop.
Follow cursor with physics
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowCursor : MonoBehaviour
{
Vector3 offset = new Vector3(0, 0, 10);
Rigidbody2D rb;
Vector2 cursorPos;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
cursorPos = Camera.main.ScreenToWorldPoint(Input.mousePosition) + offset;
//transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition) + offset;
rb.AddForce((cursorPos - new Vector2(transform.position.x, transform.position.y) ) * 10);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment