Placing my own animation idle&move to player
Hi there,
I made my first animations for idle and move, I want to put them to my player of course in sense when he is not moving - play animation for idle, when press "W/S/A/D" - play animation for move. Earlier I was using tutorial from Unity channel and now I wrote exactly that same script for it but it doesn't work.
Here it is:
using UnityEngine;
public class WalkingKyle : MonoBehaviour
{
public float speed = 6f;
Vector3 movement;
Animator anim;
Rigidbody playerRigidbody;
int floorMask;
float camRayLenght = 100f;
void Awake()
{
floorMask = LayerMask.GetMask("Floor");
anim = GetComponent<Animator>();
playerRigidbody = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
Move(h, v);
Turning();
Animating(h, v);
}
void Move(float h, float v)
{
movement.Set(h, 0f, v);
movement = movement.normalized * speed * Time.deltaTime;
playerRigidbody.MovePosition(transform.position + movement);
}
void Turning()
{
Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit floorHit;
if (Physics.Raycast(camRay, out floorHit, camRayLenght, floorMask))
{
Vector3 playerToMouse = floorHit.point - transform.position;
playerToMouse.y = 0f;
Quaternion newRotation = Quaternion.LookRotation(playerToMouse);
playerRigidbody.MoveRotation(newRotation);
}
}
void Animating(float h, float v)
{
bool walking = h != 0f || v != 0f;
anim.SetBool("IsWalking", walking);
}
}
I noticed I don't have preview view for these animation and maybe problem is in here? I don't know how to do previw view, I looked for on youtube creating animations tutorial but nothing about it I saw. Also, I noticed I don't have avatar in animator controller of my player. As previously - I don't know how to do it and if problem is inside this or not.
Can anyone help?
Edit
I've just seen I don't have motion put to the states. I tried add my animations to them but I can't with both. Does someone know why I'm unable to do that?
$$anonymous$$any problems here:
You shouldn't handle your input in a FixedUpdate() but in Update(), so you need to split your code in two scripts, one for input (Update) one for physics (FixedUpdate). $$anonymous$$oreover If you use your FixedUpdate() correctly, your movement
variable should depend on Time.fixedDeltaTime
and not Time.deltaTime
But that won't solve your animation problem.
I think the Animator isn't set properly, or maybe you just forgot to call anim.Play()
https://docs.unity3d.com/ScriptReference/Animator.Play.html
But I'm not really good on animations..
Heh, thanks for correct :) As I said I was doing this from Unity $$anonymous$$m tutorials and they did it like this so idk :P
Yeah, I didn't call anim.Play()
but in previous project, this one from Unity $$anonymous$$m, wasn't call it and every animation worked good. But I think I know where is problem. In my states of idle and move, I noticed there is no motion put to them. I tried to put there my animations but I couldn't. Do you know why?
$$anonymous$$aybe you should put a screenshot, I have no idea right now, It can be anything