Question by
Unidesky · Jul 25, 2017 at 07:39 PM ·
animationanimatorclick to move
Walking animation on a click to move script
Hi, i am having serious problems with this script i am trying to assemble from other scripts for a click to move character that walks while moving and stays still after:
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Animations;
public class ClickToMove : MonoBehaviour { public Text countText; public Text winText; public Button WinButton; public RawImage WinImage;
Animator anim;
private float speed = 20f;
private int count;
private Vector3 targetPosition;
private bool isMoving;
const int LEFT_MOUSE_BUTTON = 0;
// Use this for initialization
void Start()
{
count = 0;
SetCountText();
winText.text = "";
WinButton.gameObject.SetActive(false);
WinImage.gameObject.SetActive(false);
anim = GetComponent<Animator>();
targetPosition = transform.position;
isMoving = false;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(LEFT_MOUSE_BUTTON))
SetTargetPosition();
if (isMoving)
MovePlayer();
}
void SetTargetPosition()
{
Plane plane = new Plane(Vector3.up, transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float point = 0f;
if (plane.Raycast(ray, out point))
targetPosition = ray.GetPoint(point);
isMoving = true;
anim.Play("Correr");
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive(false);
count = count + 1;
SetCountText();
}
}
void MovePlayer()
{
transform.LookAt(targetPosition);
transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
if (transform.position == targetPosition)
isMoving = false;
anim.Play("Quieto");
Debug.DrawLine(transform.position, targetPosition, Color.red);
}
void SetCountText()
{
countText.text = " " + count.ToString();
if (count >= 8)
{
winText.text = " ";
WinButton.gameObject.SetActive(true);
WinImage.gameObject.SetActive(true);
}
}
}
Any ideas or fixes? Thanks
Comment
Answer by unity_pq4f9m5BG-5Inw · Feb 24, 2020 at 06:45 AM
Follow this video https://www.youtube.com/embed/DPBdqbP8MFI