- Home /
Question by
alexsluborski · Dec 10, 2019 at 04:18 AM ·
movementanimatorscript.3d models
Unity3D player moveforward stutter,Unity3D player stutter while doing running animation
Hello, I wrote a player moveforward script and animated running using animator but when I move the player forward, the player seems to stutter and I can't figure out why but I think the animator transition is the issue. I also added collision edge spheres to the player but I don't think they are causing the issue. Here is a video of it: https://www.youtube.com/watch?v=2zD2HwAbZBM
My player model:
My moveforward script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "New State" , menuName = "Alex/AbilityData/MoveForward")]
public class MoveForward : StateData
{
public float Speed;
public float BlockDistance;
public AnimationCurve SpeedGraph;
public override void OnEnter(CharacterStateBase characterStateBase, Animator animator, AnimatorStateInfo stateInfo)
{
animator.SetBool("Jump", false);
}
public override void UpdateAbility(CharacterStateBase characterStateBase, Animator animator, AnimatorStateInfo stateInfo)
{
CharacterControl c = characterStateBase.GetCharacterControl(animator);
if(c.Jump)
{
animator.SetBool("Jump", true);
}
if (c.MoveRight && c.MoveLeft)
{
animator.SetBool("Move", false);
return;
}
if (!c.MoveRight && !c.MoveLeft)
{
animator.SetBool("Move", false);
return;
}
if (c.MoveRight)
{
if(!CheckFront(c))
{
c.transform.Translate(Vector3.forward * Speed * SpeedGraph.Evaluate(stateInfo.normalizedTime) * Time.deltaTime);
c.transform.rotation = Quaternion.Euler(0f, 0f, 0f);
}
}
if (c.MoveLeft)
{
if(!CheckFront(c))
{
c.transform.Translate(Vector3.forward * Speed * SpeedGraph.Evaluate(stateInfo.normalizedTime) * Time.deltaTime);
c.transform.rotation = Quaternion.Euler(0f, 180f, 0f);
}
}
}
public override void OnExit(CharacterStateBase characterStateBase, Animator animator, AnimatorStateInfo stateInfo)
{
}
bool CheckFront(CharacterControl c)
{
foreach (GameObject o in c.Frontspheres)
{
Debug.DrawRay(o.transform.position, c.transform.forward * 0.3f, Color.yellow);
RaycastHit hit;
if (Physics.Raycast(o.transform.position, c.transform.forward, out hit, BlockDistance))
{
return true;
}
}
return false;
}
}
Comment
screen-shot-2019-12-09-at-110158-pm.png
(234.7 kB)
screen-shot-2019-12-09-at-110205-pm.png
(245.6 kB)