Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class movement : MonoBehaviour
- {
- public Vector3 targetPosition;
- public bool SideView = false;
- Animator anim;
- // Start is called before the first frame update
- void Start()
- {
- targetPosition = gameObject.transform.position;
- anim = gameObject.GetComponent<Animator>();
- }
- // Update is called once per frame
- void Update()
- {
- if (Input.GetMouseButtonDown(0))
- {
- targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
- if (SideView)
- {
- targetPosition = new Vector3(targetPosition.x, transform.position.y, targetPosition.z);
- }
- anim.StopPlayback();
- anim.Play("Walk");
- }
- if (Vector3.Distance(transform.position, targetPosition) <= 0.1f)//arrived to destination
- {
- anim.StopPlayback();
- anim.Play("Idle");
- }
- else
- {
- //transform.position = Vector3.MoveTowards(transform.position, targetPosition, Time.deltaTime * 5);
- Vector3 newPosition = Vector3.MoveTowards(transform.position, targetPosition, Time.deltaTime * 5);
- gameObject.GetComponent<Rigidbody2D>().MovePosition(newPosition);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement