Scripting player controls

Hi, I have just started scripting in Unity for my college project, so I am wondering if this is ok code? I don’t get any errors but when i try those controls in player mode - nothing happens?

using UnityEngine;

public class PlayerControls : Player
{
    // Additional variables for actions
    private bool isDashing = false;
    private bool isSneaking = false;

    // Start is called before the first frame update
    public override void Start()
    {
        base.Start();
    }

    // Update is called once per frame
    public override void Update()
    {
        base.Update();
        PlayerControl();
    }

    private void PlayerControl()
    {
        if (Input.GetKeyDown(KeyCode.LeftShift))
        {
            Dash();
        }

        if (Input.GetKeyDown(KeyCode.LeftControl))
        {
            Sneak();
        }

        if (Input.GetKeyDown(KeyCode.Space))
        {
            Shield();
        }

        if (Input.GetKeyDown(KeyCode.Q))
        {
            AoE();
        }
    }

    private void Dash()
    {

        if (!isDashing)
        {
            Debug.Log("Dash!");
            // transform.position += new Vector3
            // (MovementSpeed * Time.deltaTime, 0.1f, 0.0f);


            MovementSpeed *= 2f;
            isDashing = true;
            Invoke("ResetDash", 0.5f);
        }
    }

    private void Sneak()
    {
        if (!isSneaking)
        {
            Debug.Log("Sneak!");
            MovementSpeed /= 2f;
            isSneaking = true;
            Invoke("ResetSneak", 5f);
        }
    }

    private void Shield()
    {
        Debug.Log("Shield!");
    }

    private void AoE()
    {
        Debug.Log("AoE!");

    }

    private void ResetDash()
    {
        MovementSpeed = 5f;
        isDashing = false;
    }

    private void ResetSneak()
    {
        MovementSpeed = 5f;
        isSneaking = false;
    } 

Hi,

There is just a couple changes you would need to make to get this script working. First you did not define MovementSpeed, so we should add that at the top. it should look something like this:

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

public class PlayerControls : MonoBehaviour
{
    // Additional variables for actions
    private bool isDashing = false;
    private bool isSneaking = false;
    private float MovementSpeed = 2.0f;

Next you need to create some basic movement for the object first before it can dash or sneak. I used the Input.GetAxis() function built into unity to get this to work, here is more info on it and what the code looks like (I just added it at the top of the PlayerControl() method):

Input.GetAxis()

 private void PlayerControl()
    {
        float translation = Input.GetAxis("Horizontal") * MovementSpeed * Time.deltaTime;
        transform.Translate(translation, 0, 0);

By default the way you move the character left and right is with the “A” and “D” keys on the keyboard. After I made those changes, The dash and sneak methods also worked. here is the full code and changes I made.

Full Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControls : MonoBehaviour
{
    // Additional variables for actions
    private bool isDashing = false;
    private bool isSneaking = false;
    private float MovementSpeed = 2.0f;

    // Start is called before the first frame update
    public void Start()
    {

    }

    // Update is called once per frame
    public void Update()
    {
        PlayerControl();
    }

    private void PlayerControl()
    {
        float translation = Input.GetAxis("Horizontal") * MovementSpeed * Time.deltaTime;
        transform.Translate(translation, 0, 0);

        if (Input.GetKeyDown(KeyCode.LeftShift))
        {
            Dash();
        }

        if (Input.GetKeyDown(KeyCode.LeftControl))
        {
            Sneak();
        }

        if (Input.GetKeyDown(KeyCode.Space))
        {
            Shield();
        }

        if (Input.GetKeyDown(KeyCode.Q))
        {
            AoE();
        }
    }

    private void Dash()
    {

        if (!isDashing)
        {
            Debug.Log("Dash!");
            // transform.position += new Vector3
            // (MovementSpeed * Time.deltaTime, 0.1f, 0.0f);


            MovementSpeed *= 2f;
            isDashing = true;
            Invoke("ResetDash", 0.5f);
        }
    }

    private void Sneak()
    {
        if (!isSneaking)
        {
            Debug.Log("Sneak!");
            MovementSpeed /= 2f;
            isSneaking = true;
            Invoke("ResetSneak", 5f);
        }
    }

    private void Shield()
    {
        Debug.Log("Shield!");
    }

    private void AoE()
    {
        Debug.Log("AoE!");

    }

    private void ResetDash()
    {
        MovementSpeed = 5f;
        isDashing = false;
    }

    private void ResetSneak()
    {
        MovementSpeed = 5f;
        isSneaking = false;
    }
}

Hope that helps.