S&box Wiki

Revision Difference

Player_Animator#528827

<cat>Dev.Intro</cat>⤶ <title>Player Animator</title>⤶ ⤶ # What is a Player Animator⤶ ⤶ The Animator is responsible for maintaining your AnimGraph with up to date information. You can also set the position and rotation of your player in the Animator. ⤶ ⤶ ⤶ # Example⤶ ⤶ ```⤶ public class StandardPlayerAnimator : PlayerAnimator⤶ {⤶ public override void Tick()⤶ {⤶ DoRotation();⤶ DoWalk();⤶ ⤶ //⤶ // Let the animation graph know some shit⤶ //⤶ SetParam( "b_grounded", GroundEntity != null );⤶ ⤶ //⤶ // Look in the direction what the player's input is facing⤶ //⤶ SetLookAt( "lookat_pos", Player.EyePos + Input.Rot.Forward * 1000 );⤶ }⤶ ⤶ public virtual void DoRotation()⤶ {⤶ //⤶ // Our ideal player model rotation is the way we're facing⤶ //⤶ var idealRotation = Rotation.LookAt( Input.Rot.Forward.WithZ( 0 ), Vector3.Up );⤶ ⤶ //⤶ // If we're moving, rotate to our ideal rotation⤶ //⤶ Rot = Rotation.Slerp( Rot, idealRotation, WishVelocity.Length * Time.Delta * 0.01f );⤶ ⤶ //⤶ // Clamp the foot rotation to within 120 degrees of the ideal rotation⤶ //⤶ Rot = Rot.Clamp( idealRotation, 120 );⤶ }⤶ ⤶ void DoWalk()⤶ {⤶ //⤶ // These tweak the animation speeds to something we feel is right,⤶ // so the foot speed matches the floor speed. Your art should probably⤶ // do this - but that ain't how we roll⤶ //⤶ SetParam( "walkspeed_scale", 2.0f / 190.0f );⤶ SetParam( "runspeed_scale", 2.0f / 320.0f );⤶ ⤶ //⤶ // Work out our movement relative to our body rotation⤶ //⤶ var moveDir = WishVelocity.Normal;⤶ var forward = Rot.Forward.Dot( moveDir );⤶ var sideward = Rot.Right.Dot( moveDir );⤶ ⤶ //⤶ // Set our speeds on the animgraph⤶ //⤶ SetParam( "forward", forward );⤶ SetParam( "sideward", sideward );⤶ SetParam( "wishspeed", WishVelocity.Length );⤶ }⤶ }⤶ ```⤶ ⤶ ⤶ # Setting ⤶ ⤶ Setting the Animator is the same as setting a <page>Player Controller</page>.⤶ ⤶ ```⤶ public override void Respawn()⤶ {⤶ base.Respawn();⤶ ⤶ Animator = new StandardPlayerAnimator();⤶ }⤶ ```⤶ ⤶ Like the <page>Player Controller</page>, this property can be changed at any time and is replicated to the client. Setting to null will mean your player isn't animated.