Third Person Controller - How to Click to Move?
Hi everyone,
This is driving me crazy, sifting through numerous articles on the best way to accomplish this.
Simply put, I am using the Third Person Controller prefab (Ethan) from the Standard Asset package. Instead of using the arrow keys to move him, I'd like to have him move to where the player clicks.
The code I have so far just barely works, and is not usable, but hopefully it will give you an idea for what I am trying to accomplish. I was able to get it working better than this, but the Ethan Animation Controller broke, and none of his animations walk/run would happen on his way to the final destination, so I tried to tie in Unity's script to accomplish that.
Any guidance would make me rest peacefully tonight! Thanks for your help in advance.
My Script (attached to the Player Object)
using UnityEngine;
using System.Collections;
using UnityStandardAssets.Characters.ThirdPerson;
public class clickToMove : MonoBehaviour
{
private ThirdPersonCharacter thirdPersonScript;
private Transform playerTransform;
private bool completed;
void Start()
{
//Link Scripts
thirdPersonScript = gameObject.GetComponent<ThirdPersonCharacter>();
//Link Objects
playerTransform = gameObject.GetComponent<Transform> ();
}
void Update()
{
if (Input.GetMouseButtonDown (0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit));
{
Debug.Log ("I hit something!");
StartCoroutine(movePlayer(hit.point));
}
}
}
IEnumerator movePlayer (Vector3 finalDestination)
{
completed = false;
var i = 0.0f;
float journeyLength = Vector3.Distance(playerTransform.position, finalDestination);
while (journeyLength > 1.0f)
{
journeyLength = Vector3.Distance(playerTransform.position, finalDestination);
thirdPersonScript.Move (Vector3.Lerp(playerTransform.position, finalDestination, 10F), false, false);
Debug.Log (journeyLength);
yield return null;
}
Debug.Log ("Done!");
completed = true;
}
}
Unity's ThirdPersonCharacter.cs for reference
public void Move(Vector3 move, bool crouch, bool jump)
{
// convert the world relative moveInput vector into a local-relative
// turn amount and forward amount required to head in the desired
// direction.
if (move.magnitude > 1f) move.Normalize();
move = transform.InverseTransformDirection(move);
CheckGroundStatus();
move = Vector3.ProjectOnPlane(move, m_GroundNormal);
m_TurnAmount = Mathf.Atan2(move.x, move.z);
m_ForwardAmount = move.z;
ApplyExtraTurnRotation();
// control and velocity handling is different when grounded and airborne:
if (m_IsGrounded)
{
HandleGroundedMovement(crouch, jump);
}
else
{
HandleAirborneMovement();
}
ScaleCapsuleForCrouching(crouch);
PreventStandingInLowHeadroom();
// send input and other state parameters to the animator
UpdateAnimator(move);
}
Answer by EssyTech · Sep 12, 2015 at 07:06 AM
The animations for ethan depend on two vectors. m_TurnAmount and m_ForwardAmount. Make sure those are referenced in the code that runs the animations. or recreate them based on the move vector you are passing through to the UpdateAnimator() function.
Your answer
Follow this Question
Related Questions
Character controller and determinism issue 0 Answers
Rope & Character controller ? 0 Answers
Raycast never gets called 1 Answer
Proper use of Character Controller 2 Answers
Raycast doesn't stay in one place 0 Answers