SHARE
TWEET

Untitled

a guest Feb 21st, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class movement : MonoBehaviour
  6. {
  7.     public Vector3 targetPosition;
  8.     public bool SideView = false;
  9.     Animator anim;
  10.     // Start is called before the first frame update
  11.     void Start()
  12.     {
  13.         targetPosition = gameObject.transform.position;
  14.         anim = gameObject.GetComponent<Animator>();
  15.     }
  16.  
  17.     // Update is called once per frame
  18.     void Update()
  19.     {
  20.         if (Input.GetMouseButtonDown(0))
  21.         {
  22.             targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  23.             if (SideView)
  24.             {
  25.                 targetPosition = new Vector3(targetPosition.x, transform.position.y, targetPosition.z);
  26.             }
  27.             anim.StopPlayback();
  28.             anim.Play("Walk");
  29.         }
  30.         if (Vector3.Distance(transform.position, targetPosition) <= 0.1f)//arrived to destination
  31.         {
  32.             anim.StopPlayback();
  33.             anim.Play("Idle");
  34.         }
  35.         else
  36.         {
  37.             //transform.position = Vector3.MoveTowards(transform.position, targetPosition, Time.deltaTime * 5);
  38.             Vector3 newPosition = Vector3.MoveTowards(transform.position, targetPosition, Time.deltaTime * 5);
  39.             gameObject.GetComponent<Rigidbody2D>().MovePosition(newPosition);
  40.         }
  41.     }
  42. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top