Answers for "pokemon c# unity walking 2D"

C#
0

pokemon c# unity walking 2D

// Systems to use.

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

public class PlayerController : MonoBehaviour
{
    // Variables
    
    public float WalkingSpeed = 3f;
    public float RunningSpeed = 8f;
    public bool IsRunning = false;
    private float HorizontalMovement;
    private float VerticalMovement;
    private Rigidbody2D RB;

    void Start()
    {   
        // Game Loaded?
        Debug.Log("Game Loaded [PlayerController]");
        // Set Variable
        RB = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {   
        // Movement Inputs.

        HorizontalMovement = Input.GetAxisRaw("Horizontal");
        VerticalMovement = Input.GetAxisRaw("Vertical");

        // Running System.
        
        // Delete all of this if you don't want sprinting.

        if (Input.GetKey(KeyCode.LeftShift))
        {
            IsRunning = true; // Needs to be specific for the running system I want. Change it to Input.GetKeyDown for it to be a toggle sprinting system
        }
        else
        {
            IsRunning = false;
        }
    }

    // Fixed Update is just better in general- In my opinion

    void FixedUpdate() {
        // Movement System
            // Check if player is running
        
        if (IsRunning)
        {
            RB.velocity = new Vector2(HorizontalMovement * RunningSpeed, VerticalMovement * RunningSpeed);
        }
        else
        {
            RB.velocity = new Vector2(HorizontalMovement * WalkingSpeed, VerticalMovement * WalkingSpeed);
        }
        
    }
}
Posted by: Guest on September-01-2021

C# Answers by Framework

Browse Popular Code Answers by Language