douras
1
Hello,
I would like to move a sprite where the player has clicked.
It would be a mouvement like in Spore but with 2D sprites (cell) Spore
I usaly code in Javascript, do you have an idea to do that?
I have tested many scripts but they don’t work with the new sprite system of unity 4.3.
Thank you
P.S: Sorry for my english, it’s not my native langage.
Here is a bit of code that moves and rotates. It assumes that the camera is axes aligne and that it is facing towards positive ‘z’:
#pragma strict
public var speed = 1.5;
public var rotationSpeed = 90.0;
private var pos : Vector3;
private var qTo : Quaternion;
function Start () {
pos = transform.position;
qTo = transform.rotation;
}
function Update () {
if (Input.GetMouseButtonDown(0) || Input.GetMouseButton(0)) {
pos = Input.mousePosition;
pos.z = transform.position.z - Camera.main.transform.position.z;
pos = Camera.main.ScreenToWorldPoint(pos);
}
var dir = pos - transform.position;
if (dir != Vector3.zero) {
qTo = Quaternion.FromToRotation(transform.right, dir) * transform.rotation;
transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, Time.deltaTime * rotationSpeed);
}
transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);
}