- Home /
Character rotation (click-to-move)
Hello! I'm new in Unity3D and I have this code:
private NavMeshAgent agent;
private Animator anim;
void Start() {
agent = GetComponent<NavMeshAgent>();
anim = GetComponent<Animator> ();
}
void Update() {
RaycastHit hit;
if (Input.GetMouseButtonDown(0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
agent.SetDestination(hit.point);
anim.Play ("walk");
}
if (!agent.pathPending)
{
if (agent.remainingDistance <= agent.stoppingDistance)
{
if (!agent.hasPath || agent.velocity.sqrMagnitude == 0f)
{
anim.Play ("idle");
}
}
}
}
When I click behind of my character my character is rotating slow. I want to character change rotation to go instantly only in front. If I click behind, character to have already roation and only go, without any rotation. Anyone can help me? Thank you!
I don't see any code that is rotating the character. Between agent.SetDestination(hit.point)
and anim.Play("walk")
use the code transform.LookAt(hit.point)
.
Also check out A* Pathfinding if unity's in built AI is too simple. I've just finished setting up A* and I'm Pretty happy with it. It does automatic rotating and movement.
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Looking forward while walking with navMeshAgent 1 Answer
NavMeshAgent resume original position and facing direction C# 0 Answers