Movement script

Hey so i’m new to this stuff and cant for the life of me figure out why this wont work. I previously watched a youtube video on basic stuff but then I changed the code so I was able to set my own velocity for the movement. In doing so it now only goes one way when I hold down one of the keys and its the d key.

please help

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

public class Player : MonoBehaviour
{
    [SerializeField] private Transform groundCheckTransform = null;
    public LayerMask playerMask;
    private float horizontalInput;
    private bool jumpKeyWasPressed;
    private Rigidbody rIgidbody;
    private bool isGrounded;
    private float movementSpeed;






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

        rIgidbody = GetComponent<Rigidbody>();
        horizontalInput = 0;
        movementSpeed = 0;

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown("space"))
        {
            jumpKeyWasPressed = true;

        }
   

        horizontalInput = Input.GetAxis("Horizontal");

   
    }
// use for phsyics shit that needs to be updated frame by frame
    private void FixedUpdate ()
    {
   
        if (Input.GetKey("d"))
        {
            movementSpeed = 2;
            rIgidbody.velocity = new Vector3(movementSpeed, rIgidbody.velocity.y, 0);
       
        }
        else
        {

            movementSpeed = 0;

        }

        if (Input.GetKey("a"))
        {
            movementSpeed = -2;
            rIgidbody.velocity = new Vector3(movementSpeed, rIgidbody.velocity.y, 0);

        }
        else
        {

            movementSpeed = 0;

        }


        rIgidbody.velocity = new Vector3(movementSpeed, rIgidbody.velocity.y, 0);
   
        if (Physics.OverlapSphere(groundCheckTransform.position, 0.3f, playerMask).Length == 0)
        {
            return;
        }

        if (isGrounded == false)
        {
            return;
        }


        if (jumpKeyWasPressed)
        {
            rIgidbody.AddForce(Vector3.up * 5, ForceMode.VelocityChange);
            jumpKeyWasPressed = false;
  
        }

   


    }

    void OnCollisionEnter(Collision collision)
    {
        isGrounded = true;
   
    }

    void OnCollisionExit(Collision collision)
    {
        isGrounded = false;
   
    }







}

Your D and A checks BOTH have an else clause that sets movementSpeed to zero.

A better way is to set movement speed to zero ALWAYS, then check for D, set it to 2, check for A, set it to -2.

THEN after all that is done, use the value in movementSpeed: since that code is identical, why type it twice the way it is on line 52 and line 65? Two chances for typos… minimize that.

Actually looking at it further, you’re running that code AGAIN on line 76 above!

The way you can solve stuff like this on your own is to use lots of Debug.Log() statements to print those variables out and find where code is running.

In fact, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

If you are running a mobile device you can also see the console output. Google for how.

1 Like

thanks for the advice, it works now.

1 Like