Question by
altrev · Aug 11, 2017 at 06:50 AM ·
canvastransform.positionmousepositiontranslatemouse-drag
HOW TO MOVE ANIMATED OBJECT ON CANVAS
Please I have an animated coin spinning at the top corner of my canvas. I need help in moving this coin with the mouse on right click. They don't necessarily need to be in the same position (the mouse being over the coin) as I just need the transform position of the coin to translate with that of the mouse. Many scripts I found moved the coin in 3d and the coin gets left behind when I move my character forward but I need it to remain planar (moving only in x and y like on the canvas). By the way, my canvas uses a secondary camera. The last script I tried took the coin out of sight
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 = Vector3.Lerp( new Vector3 (transform.position.x , transform.position.y, -5), mousePosition, moveSpeed);
}
}
A solution would be very much appreciated. Thank you.
Comment