- Home /
Move a Third Person Character via move-method
Hello,
I am working on a simple project where I move a character with external input. I want my character to move to a specified point. To keep it short I just imported the Standard Assets and used the Ethan-Model for that. I thought, it would make everything easier if I work with the given character controller etc. Here is my code so far:
// move it
public void MoveCharacterToPoint(Vector3 point)
{
// compute difference between current position and targeted position
var offset = point - transform.position;
// check if the distance is far enough
if (offset.magnitude > 0.1f)
{
// normalize offset und multiply movement speed
offset = offset.normalized * moveSpeed;
Debug.Log("Move Character...");
// issue move command
m_Character.Move(offset, false, false);
// move testobject
testCylinder.transform.position = new Vector3 (x, y, z);
}
}
I am having trouble making my character move correctly with that code. Using this my character my character moves to the given point normally and then "dances" around the given point. It seems like Unity doesn't realise that my character reached the destination already.
I have also tried to multiply the offset with DeltaTime. In that case the character moves very, very slowly, almost not noticable, and an increase of the movement speed has no effect.
I would greatly apprediate hints on how to solve this.
If you want it to be a straight movement, you can then use Vector3.Lerp and change the t value over time.
That works, but while using Vector3.Lerp the character has no movement animation at all. He just slides through the landscape. That is why I wanted to use move, which doesn't seem to work in this case.
Answer by brunocoimbra · Feb 03, 2016 at 11:11 PM
Sorry, didn't noticed that you were dealing with animations (never used the ThirdPersonController until now).
Maybe not the best solution, but it does work. Create a new script and rename it to "MovePoint", then copy-paste the code below:
using UnityEngine;
public class MovePoint : MonoBehaviour
{
private static MovePoint point;
private ThirdPersonCharacter character;
private CapsuleCollider capsuleCollider;
private void OnTriggerEnter(Collider collider)
{
if (collider.gameObject == character.gameObject)
{
character.Move(Vector3.zero, false, false);
Destroy(point.gameObject);
point = null;
}
}
public static void CreatePoint(Vector3 position, ThirdPersonCharacter character)
{
if (point)
{
Destroy(point.gameObject);
point = null;
}
point = new GameObject("MovePoint").AddComponent<MovePoint>();
point.character = character;
point.capsuleCollider = point.gameObject.AddComponent<CapsuleCollider>();
point.capsuleCollider.isTrigger = true;
point.capsuleCollider.radius = 0.1f;
point.capsuleCollider.height = 10f;
}
}
And modify you method to that:
public void MoveCharacterToPoint(Vector3 point)
{
// Before everything else:
MovePoint.CreatePoint(point, m_Character);
// Then do all your existing logic.
}
Your character will need a collider attached to it, and a rigidbody too. If the rigidbody causes trouble to your movimentation, just set "isKinematic" to true.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
c# script not doing anything. 2 Answers
Moving two objects and avoiding overlap with each other 1 Answer
Trying to fix my movement after the camera broke it. 0 Answers