Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

using System.

Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour


{
// Declare a CharacterController2D component named "controller"
public CharacterController2D controller;

// Declare an Animator component named "animator"


public Animator animator;

// Declare a BoxCollider2D component named "bc"


public BoxCollider2D bc;

// Declare a float named "runSpeed" and initialize it to 40


public float runSpeed = 40f;

// Declare a float named "horizontalMove" and initialize it to 0


float horizontalMove = 0f;

// Declare a bool named "jump" and initialize it to false


bool jump = false;

// In the Start() method, get the BoxCollider2D component and assign it to bc


void Start()
{
bc = GetComponent<BoxCollider2D>();
}

// In the Update() method, check if the S key is pressed and the BoxCollider2D
is enabled
// If it is, disable the BoxCollider2D
// If the S key is released and the BoxCollider2D is disabled, enable the
BoxCollider2D
void Update()
{
if (Input.GetKeyDown(KeyCode.S) && bc.enabled == true)
{
bc.enabled = false;
}
else if (Input.GetKeyUp(KeyCode.S) && bc.enabled == false)
{
bc.enabled = true;
}

// Get the horizontal axis input and multiply it by runSpeed, then assign
the result to horizontalMove
horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;

// Set the "Speed" parameter of the animator to the absolute value of


horizontalMove
animator.SetFloat("Speed", Mathf.Abs(horizontalMove));

// If the Jump button is pressed, set jump to true and set the "IsJumping"
parameter of the animator to true
if (Input.GetButtonDown("Jump"))
{
jump = true;
animator.SetBool("IsJumping", true);
}
}

// In the OnLanding() method, set the "IsJumping" parameter of the animator to


false
public void OnLanding()

You might also like