Hello Im David, im somewhat new to unity, im trying to make a 3rd person controller with the character walking and jumping. I followed brackeys tutorial and got the controller down but having trouble triggering the animations.
I tried looking at other tutorials to see if i can add on to what i have but not having the best of luck. I’m using a free char model that has idle, walk and run animations. im also using a jump animation from mixamo, but the preview for the animation wasn’t working right.
Heres the code i have right now, but ill also include the project it self. Any help will be much appreciated. Also if possible i would like to learn the fix, and not just be handed it.
Project Files: platformer demo files.zip - Google Drive
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonMovement : MonoBehaviour
{
public CharacterController controller;
public Transform cam;
public float speed = 6f;
public float turnSmoothTime = 0.1f;
float turnSmoothVelocity;
// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
if (direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir.normalized * speed * Time.deltaTime);
}
}
}
U have to get a reference to the the animator on the player so it would
public Transform cam;
public float speed = 6f;
public Animation playerAni;
void Start(){
playerAni = GetComponent();
}
void Update(){
if (direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir.normalized * speed * Time.deltaTime);
playerAni.Play(string animation, PlayMode mode = PlayMode.StopSameLayer);
}
}
speedyfast734:
U have to get a reference to the the animator on the player so it would
public Transform cam;
public float speed = 6f;
public Animation playerAni;
void Start(){
playerAni = GetComponent();
}
void Update(){
if (direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir.normalized * speed * Time.deltaTime);
playerAni.Play(string animation, PlayMode mode = PlayMode.StopSameLayer);
}
}
ok, how should i set up the animator?
It’s a wee bit involved to just describe… I highly recommend working through one or two of the Unity mecanim tutorials.
The basic steps are this: you set up different animations to trigger in different states. (say “idle” and “walk”)
Then you set up control variable(s) (such as “speed”) to tell the animator to change states, such that above a tiny speed, it goes from idle to walk.
Setting that “speed” property is all you have to do from the code side.
Once the animator has been set up properly, it all just works like magic.
Kurt-Dekker:
It’s a wee bit involved to just describe… I highly recommend working through one or two of the Unity mecanim tutorials.
The basic steps are this: you set up different animations to trigger in different states. (say “idle” and “walk”)
Then you set up control variable(s) (such as “speed”) to tell the animator to change states, such that above a tiny speed, it goes from idle to walk.
Setting that “speed” property is all you have to do from the code side.
Once the animator has been set up properly, it all just works like magic.
got it, so its not the big red button that says make game.
For the tutorial you mentioned, did you mean this tutorial?
https://www.youtube.com/watch?v=Xx21y9eJq1U
I’m not sure of that particular tutorial, but pretty much any basic mecanim tutorial will give you an idea of how the parts play with each other, as my post above will as well. My post above explains it, but you need to do it yourself to grasp it because it’s a bit abstract.