Question by
hozthemage · Sep 09, 2015 at 06:48 PM ·
movementmousecursormovetowards
Using MoveTowards to move GameObject from starting point to mouse cursor
So I'm trying to get a GameObject to move to the mouse cursor upon release using MoveTowards. The coordinates seem to be alright and the current point and ending point are defined. But when it runs, the MoveTowards just seems to have the GameObject tweak out. It's kind of like it starts to move to where the mouse cursor was released, but it keeps resetting back to the starting point on every frame. Any ideas as to why this may be?
using UnityEngine;
using System.Collections;
public class Spin : MonoBehaviour {
public Vector3 currentPoint;
public Vector3 mousePoint;
public float speed;
public float totalDistanceToDestination;
// Use this for initialization
void Start () {
speed = 20.0f;
currentPoint = transform.position;
mousePoint = transform.position;
Debug.Log("The shuriken is starting at" + currentPoint);
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButton(0))
{
transform.Rotate(Vector3.back, Time.deltaTime*2000);
Debug.Log("Mouse is at " + mousePoint);
}
if (Input.GetMouseButtonUp(0))
{
mousePoint = Input.mousePosition;
mousePoint = Camera.main.ScreenToWorldPoint(mousePoint);
mousePoint.z = 0;
Debug.Log("The shuriken is now at" + mousePoint);
}
transform.position = Vector3.MoveTowards(currentPoint, mousePoint, speed * Time.deltaTime);
}
}
Comment