Deepika C P
Deepika C P

Reputation: 1303

How to move an 2D character(with animation) to mouse click position in C#?

i am going to develop a simple game called waste collection.In which if i click on a waste the character needs to walk to certain waste and collect it.Startingly the character is in the middle position of the screen.when we click on the left side of the character,animation left walk needs to play.On the other hand,when we click on the right side of the character the animation right walk needs to play.How the clicked position is taken in this application and how the character animates to that clicked position

public class mousepos : MonoBehaviour {

private Animator anim;
 public float speed = 1.5f;
 private Vector3 target;

 void Start () {
 anim = GetComponent<Animator>();
     target = transform.position;
 }

 void Update () {
    if (Input.GetMouseButtonDown (0)) {
                    if (**Input.mousePosition.x <= 500**) { //move skeleton on X axis to the left
                            target = Camera.main.ScreenToWorldPoint (Input.mousePosition);
                            target.z = transform.position.z;
                            transform.localScale = new Vector3(1, 1, 1); 
                            anim.SetInteger ("Direction", 1);

                    } else if (Input.mousePosition.x >= 500) {

                            target = Camera.main.ScreenToWorldPoint (Input.mousePosition);
                            target.z = transform.position.z;
                            transform.localScale = new Vector3(-1, 1, 1); 
                            anim.SetInteger ("Direction", 1);
                    }

            }
     transform.position = Vector3.MoveTowards(transform.position,              target, speed * Time.deltaTime);
 }    
 }

Upvotes: 0

Views: 959

Answers (2)

Deepika C P
Deepika C P

Reputation: 1303

I got it ..:)

public class mousepos : MonoBehaviour {

    private Animator anim;
    public float speed = 1.5f;
    private Vector3 target;
    private Vector3 MousePosition;
    private Vector3 PlayerPosition;
    void Start () {
    anim = GetComponent<Animator>();


    target = transform.position;

    }
    void Update () {

            if (Input.GetMouseButtonDown (0)) {

            target = Camera.main.ScreenToWorldPoint (Input.mousePosition);
           target.z = transform.position.x;

           Debug.Log ("traaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + target.z);
           if (target.x <= target.z) {


          transform.localScale = new Vector3 (1, 1, 1); 
         anim.SetInteger ("Direction", 1);

    }
   else if (target.x >= target.z) {

                            transform.localScale = new Vector3 (-1, 1, 1); 
                            anim.SetInteger ("Direction", 1);
                    }



            }

      transform.position = Vector3.MoveTowards (transform.position, target, speed * Time.deltaTime);

    }
    }

Upvotes: 0

The_Guy
The_Guy

Reputation: 108

Not sure exactly what you're asking for but would something like this work?

if (Input.GetMouseButtonDown (0)) {
    target = Camera.main.ScreenToWorldPoint (Input.mousePosition);
    target.z = transform.position.z;
    if(target.x > transform.position.x) transform.localScale = new Vector3(1, 1, 1); 
    else if(target.x < transform.position.x) transform.localScale = new Vector3(-1, 1, 1);
    anim.SetInteger ("Direction", 1);
 } 
 transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);

Upvotes: 1

Related Questions