Hello all. I’m fairly new to Unity and I’m stepping through an old legacy tutorial and the code doesn’t work for Unity 5, so i’m having to make some adjustments and coming up short. It’s an isometric gaming tutorial, so the goal is to rotate my object and move it towards the position i clicked on.
I’ve imported assets outside of the assets described in the tutorial to more closely follow the new Mechanim process, but I’m having issues getting the character to move forward and stop with proper run/idle animations.
Right now what happens is that the object doesn’t move and the move animation constantly loops. I’m assuming this is because my SimpleMove function isn’t working and i’m never making to the destination point to initiate the idle animation. I’m not sure why this isn’t working.
The Asset package is ‘Female Warrior Princess’
Here is my code sample from my ClickToMove script -
using UnityEngine;
using System.Collections;
public class ClickToMove : MonoBehaviour {
public float speed;
public CharacterController controllers;
private Vector3 position;
public Animator anim;
// Use this for initialization
void Start () {
position = transform.position;
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButton(0))
{
//Locate where the player clicked on the terrain
locatePosition();
}
//Move the player to the position
moveToPosition();
}
void locatePosition()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast(ray, out hit, 1000))
{
position = new Vector3(hit.point.x, hit.point.y, hit.point.z);
Debug.Log(position);
}
}
void moveToPosition()
{
//Game Object is moving
if (Vector3.Distance(transform.position, position) > 1)
{
Quaternion newRotation = Quaternion.LookRotation(position - transform.position);
newRotation.x = 0f;
newRotation.z = 0f;
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 10);
controllers.SimpleMove(transform.forward * speed);
anim.Play("UNCombatMove", -1);
}
//Game Object is idle
else
{
anim.Play("UNCombatIdle", -1);
}
}
}