- Home /
How to drag a sprite in a limit space after it finished playing animation
I am new in Unity and I tried to figure this out for a long time . I need to drag a sprite in a limit space(2D) with the direction as the mouse after the sprite had been clicked and finished playing the animation. English is not my native language so there must be many grammer mistake but hope you can know what I mean. Can anyone help me? I will very very appreciate it. Thank you! Now the situation is the sprite can drag at the start and it can click to play animation, but it can't drag in a limit space after playing the animation. drag either and also when I just drag it the mouse drag up the sprite turn left???
Below is the code I had done so far
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Hu_clicktoDrag : MonoBehaviour { bool dragable; Vector3 oldPos; // Use this for initialization
void Start () {
dragable = false;
}
// Update is called once per frame
Vector3 offset;//<-- x, y, z
private void OnMouseDown()
{
gameObject.GetComponent<Animator>().Play("dog");
}
private void OnMouseUp()
{
dragable = true;
}
void OnMouseDrag()
{
Vector3 newPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 50.0f);
{
Vector3 movD = gameObject.transform.position - oldPos;
if (movD != Vector3.zero)
{
float angle = Mathf.Atan2(movD.y, movD.x) * Mathf.Rad2Deg - 90f;
gameObject.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
}
oldPos = gameObject.transform.position;
Vector3 gamePos = gameObject.transform.position;
Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10.0f);
Vector3 screenPos = Camera.main.ScreenToWorldPoint(mousePos);
gameObject.transform.position = screenPos;
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
GUI text not showing in scene or game view 0 Answers
Scale fishing rod in one direction 2 Answers