I’d like to make a top down game that the sprite follows the mouse in a very nicely animated manner, i’m just wondering if there is a way to do so. At the moment i’m thinking about systematically making the only directions being to move horizontally and vertically, it will be a topdown shooter that you move with your right click of the mouse, but im mostly worried about making the sprite follow the mouses position when you right click. Please help me, is there any way of doing this/ doing this easily?
Here’s some code I wrote for you. Just attach it to any object you want to follow the mouse when right-click is down. I hope it helps!
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);
}
}
}
or use moving to mouse with normalized moving vector
void Update () {
if (Input.GetMouseButton(1)) {
mousePosition=Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 tmpDir = mousePosition - transform.position;
//this line should disable moving near the end or the object will "jitter"
//disable if-line to see what I mean
if (tmpDir.sqrMagnitude > 0.1f)
transform.Translate (tmpDir.normalized * moveSpeed * Time.deltaTime);
}
}
maybe the moveSpeed was zero, check the value in inspector (and the script is for left-click, change Input.GetMouseButtonDown(0) to 1, if right-click is needed ).
public float moveSpeed = 10;
Vector3 mousePosition;
void Start()
{
//to prevent that object will move to zero at beginn.
mousePosition = transform.position;
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePosition.z = transform.position.z;
}
transform.position = Vector3.MoveTowards(transform.position, mousePosition, moveSpeed * Time.deltaTime);
}
Pyrotech850, could you post your solution?
On line 14 inside point.cs, you are trying to use an object which is null. This is usually caused by either trying to GetComponent which is not available, or using a member variable which is not set in the editor.
Hey there I just started programming and am wondering how i might be able to do it without needing to press left mousebutton. so i simply hover the screen and my object follows it… any ideas?
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 () {
mousePosition = Input.mousePosition;
mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
transform.position = Vector2.Lerp(transform.position, mousePosition, moveSpeed);
}
}