Move.cs

Player movement component for a chicken-like character. Handles ground movement, jumping, double/triple jumps, flying movement, dodge, animation parameter changes and playing/stopping various sounds.

Rough CodeNative Interop
using System.Collections;
using System.Collections.Generic;
using Sandbox;
using System.Numerics;
using static Sandbox.Sprite;
using static System.Runtime.CompilerServices.RuntimeHelpers;

public sealed class Move : Component
{
	private CharacterController controller;
	private Animation anim;
	private Vector3 moveDirection = Vector3.Zero;

	public float gravity = 2f;
	public float jumpforce = 6f;
	public float speed = 2f;
	public float timer = 0f;
	public float flyforce = 20f;
	public float flyspeed = 4f;
	public float dodgedir = 0f;

	public int tapCount = 0;

	private AudioListener chickenAudio;
	public SoundEvent chickenrun;
	public SoundEvent cluck;
	public SoundEvent wingsflap;
	public SoundEvent wingsfly;
	public SoundEvent dodge;

	[RequireComponent] SkinnedModelRenderer model { get; set; }




	// Start is called before the first frame update
	void Start()
	{
		controller = GetComponent<CharacterController>();
		anim = GameObject.GetComponentInChildren<Animation>();
		chickenAudio = GetComponent<AudioListener>();


	}

	// Update is called once per frame
	void Update()
	{
		ProcessGroundMovement();
		ProcessGroundJump();
		ProcessFly();
		ProcessFlyMovement();
		ProcessDodge();
	}

	void ProcessGroundMovement()
	{

		if ( controller.IsOnGround && Input.Keyboard.Pressed( "D" ) && (timer < 1) )
		{
			MoveRight();
		}
		if ( controller.IsOnGround && Input.Keyboard.Pressed( "A" ) && (timer < 1) )
		{
			MoveLeft();
		}
	}
	void ProcessDodge()
	{
		if ( controller.IsOnGround && Input.Keyboard.Down( "LeftShift" ) )
		{
			Dodge();
		}
		else if ( controller.IsOnGround && !Input.Keyboard.Pressed( "D" ) && !Input.Keyboard.Pressed( "A" ) )
		{
			StopDodging();
		}
	}
	void ProcessGroundJump()
	{


		if ( Input.Keyboard.Pressed( "Space" ) && (timer < 1) )
		{
			Jump();
		}

		if ( controller.IsOnGround && Input.Keyboard.Pressed( "Space" ) && (timer > 1) )
		{
			StopJumping();
		}
		if ( Input.Keyboard.Down( "Space" ) )
		{
			tapCount += 1;
		}
		if ( Input.Keyboard.Released( "Space" ) )
		{
			timer = 0;
		}
		if ( controller.IsOnGround )
		{
			tapCount = 0;
		}

	}
	void ProcessFly()
	{
		if ( (tapCount > 0) && (tapCount < 3) && Input.Keyboard.Pressed( "Space" ) && !controller.IsOnGround && (timer < .5f) )
		{
			DoubleJump();
		}
		if ( (tapCount > 1) && (tapCount < 3) && Input.Keyboard.Pressed( "Space" ) && !controller.IsOnGround && (timer < .5f) )
		{
			TripleJump();
		}
	}
	void ProcessFlyMovement()
	{
		if ( !controller.IsOnGround && Input.Keyboard.Pressed( "D" ) && !Input.Keyboard.Pressed( "Space" ) )
		{
			FlyRight();
		}
		if ( !controller.IsOnGround && Input.Keyboard.Pressed( "A" ) && !Input.Keyboard.Pressed( "Space" ) )
		{
			FlyLeft();
		}
		if ( !controller.IsOnGround && !Input.Keyboard.Pressed( "anyKey" ) )//&& (chickenAudio.clip != dodge) )
		{
			FlyIdle();
		}
		else if ( controller.IsOnGround && !Input.Keyboard.Pressed( "D" ) && !Input.Keyboard.Pressed( "A" ) && !Input.Keyboard.Down( "LeftShift" ) )
		{
			StopFlying();
		}


		moveDirection.y -= gravity * Time.Delta;
		controller.Velocity = moveDirection * speed;
	}

	void MoveRight()
	{
		model.Set( "AnimPar", 1 );
		controller.Move( );
		Vector3 newRot = new Vector3( 0f, 270f, 0f );
		//transform.rotation = Quaternion( newRot );
		dodgedir = 0;
		GameObject.PlaySound( chickenrun );
		if ( Input.Keyboard.Pressed( "Space" ) )
		{
			GameObject.PlaySound( wingsflap );
		}
	}
	void MoveLeft()
	{
		model.Set( "AnimPar", 1 );
		moveDirection = Transform.Local.Forward * Vector3.Backward * speed;
		Vector3 newRot = new Vector3( 0f, 90f, 0f );
		//Rotation myRotation = ToRotation(newRot);
		dodgedir = 1;
		GameObject.PlaySound( chickenrun ); 
		
		if ( Input.Keyboard.Pressed( "Space" ) )
		{
			GameObject.PlaySound( wingsflap );
		}
	}

	

	void StopDodging()
	{
		model.Set( "AnimPar", 0 );
		GameObject.StopAllSounds();
		moveDirection.x = 0;
		if ( Input.Keyboard.Down( "LeftShift" ) )
		{
			GameObject.PlaySound( dodge );
			model.Set( "AnimPar", 5 );
		}
	}

	void Dodge()
	{
		model.Set( "AnimPar", 5 );
		if ( dodgedir == 0 )
		{
			moveDirection = new Vector3( -5, 7, 0 );
		}
		if ( dodgedir == 1 )
		{
			moveDirection = new Vector3( 5, 7, 0 );
		}

		GameObject.PlaySound( dodge );
	}

	void StopJumping()
	{
		model.Set( "AnimPar", 0 );
		moveDirection.x = 0;
		moveDirection.y = 0;
	}
	void Jump()
	{
		model.Set( "AnimPar", 2 );
		moveDirection.y = jumpforce;
		timer += Time.Delta;
		GameObject.PlaySound( wingsflap );
		if ( (tapCount > 0) && (tapCount == 1) )
		{
			GameObject.PlaySound(wingsfly);
		}
	}
	void TripleJump()
	{
		model.Set( "AnimPar", 4 );
		moveDirection.y += flyforce;
		GameObject.PlaySound(cluck);
	}
	void DoubleJump()
	{
		model.Set( "AnimPar", 4 );
		moveDirection.y += flyforce;
		GameObject.PlaySound( wingsfly );
		if ( tapCount > 1 )
		{
			GameObject.PlaySound( cluck );
		}
	}
	void StopFlying()
	{
		model.Set( "AnimPar", 0 );
	}
	void FlyIdle()
	{
		model.Set( "AnimPar", 3 );
		GameObject.StopAllSounds();
	}
	void FlyLeft()
	{
		model.Set( "AnimPar", 3 );
		moveDirection = Transform.Local.Forward * Vector3.Backward * flyspeed;
		Vector3 newRot = new Vector3( 0f, 90f, 0f );
		//transform.rotation = Quaternion.Euler( newRot );
		dodgedir = 1;
	}
	void FlyRight()
	{
		model.Set( "AnimPar", 3 );
		moveDirection = -Transform.Local.Forward * Vector3.Forward * flyspeed;
		Vector3 newRot = new Vector3( 0f, 270f, 0f );
		//transform.rotation = Quaternion.Euler( newRot );
		dodgedir = 0;
	}
}