How can i make my sphere continuously following the mouse but always with the same speed?
Also if the mouse moves faster than the speed of the sphere.
this is just the follow script without a specific speed for the sphere.
#pragma strict
var depth = 10.0;
function Start ()
{
Screen.showCursor = false;
}
function Update ()
{
var mousePos = Input.mousePosition;
var wantedPos = Camera.main.ScreenToWorldPoint (Vector3 (mousePos.x, mousePos.y, depth));
transform.position = wantedPos;
}
Thanks for your help!!
Add a speed variable and change line 14 to get the following script:
#pragma strict
var depth = 10.0;
var speed = 1.5;
function Start ()
{
Screen.showCursor = false;
}
function Update ()
{
var mousePos = Input.mousePosition;
var wantedPos = Camera.main.ScreenToWorldPoint (Vector3 (mousePos.x, mousePos.y, depth));
transform.position = Vector3.MoveTowards(transform.position, wantedPos, speed * Time.deltaTime);
}
I was brought to this forum for my Unity 2D project and am posting my solution for those other poor souls.
//Get the mouse
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePos = new Vector3(mousePos.x, mousePos.y, 0);
//Rotate the sprite to the mouse point
Vector3 diff = mousePos - transform.position;
diff.Normalize();
float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 90);
//Move the sprite towards the mouse
transform.position += transform.up * speed * Time.deltaTime;