Created
November 12, 2020 16:29
-
-
Save TheCuttlefish/8639e3170bb056b11208ed9041bb2a29 to your computer and use it in GitHub Desktop.
Follow cursor with physics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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