Help with code. Running script.

I am new to coding and watched Brackets tutorial on FPS movement, after i tried some on my own and made a little strip of code to add on to the movement script. it “worked” Unity could run the game, but when I am in the game it tells me that the key I had wasen’t bound, that was my first mistake I think. Then I tried walking around and it was working like normal untill I tried my staircase. When i walked of i registered that my gravety was of. I dind’t tuch that code and can’t se how any of the changes I made could have affected it.

It is an running script. I thought it would be easy, just make another key for extra speed. But shows how new i am when it dosen’t work and it destroys something else also. Plz help.

Here is the paste:
The bold text is the new code i added on.

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

public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;

public float speed = 12f;
public float gravity = -9.82f;
public float jumpHeight = 9f;

public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;

Vector3 velocity;
bool isGrounded;

void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

if(isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}

float x = Input.GetAxis(“Horizontal”);
float z = Input.GetAxis(“Vertical”);

Vector3 move = transform.right * x + transform.forward * z;

controller.Move(move * speed * Time.deltaTime);

//New Code
if(Input.GetButtonDown(“Shift”) && isGrounded)
{
Vector3 speed = transform.right * x + transform.forward * z * 2;
}

if(Input.GetButtonDown(“Jump”) && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}

velocity.y += gravity * 3 * Time.deltaTime;

controller.Move(velocity * Time.deltaTime);
}
}

Hi and welcome.
First of all, please use code tags to improve readability. There is a sticky forum post on this subforum explaining how.

So, now about your problem. Unless you manually defined a key called ‘Shift’, it does not exist. The terms Brackey used in his tutorial (like ‘Jump’) are predefined and basically tell the system which actual keyboard key you mean when you write that. In the case of ‘Jump’, that would be space. You should also get an exception in your console, along the lines of:

ArgumentException: Input Button Shift is not setup.
To change the input settings use: Edit -> Settings -> Input

If an exception is thrown, the following code is not being executed. This is also the reason for why you do not have any gravity anymore: gravity is applied after the code you inserted, which causes the exception.
Always look for exceptions in the console, they tell you a lot of useful information :wink:

So how do we fix this? You could either do what the exception suggests you and define a key called shift, or you could tell the compiler yourself which key on the keyboard you mean. This is actually the way most people do it to my knowledge and in fact i never saw any tutorial use GetButtonDown so far. Try:

Input.GetKeyDown(KeyCode.LeftShift);

Hope this helps :slight_smile:

Hi

Thx you so much, didn’t know that. I did what you said and my gravety problem solved itself. It now works but the sprinting still dosen’t happend. Do you know why that is?

You are not using your ‘Vector3 speed’ for anything, so of course nothing happens. So far you just defined a value. And, if i may add, it’s also not the smartest way to approach what you want to do.

The code you are using moves the player at line… well… i’m not going to count (which is where code-tags really would help :wink: ). Anyways, the line ‘controller.Move(move * speed * Time.deltaTime);’ moves the player. The only differene between walking and running would be the speed. So you basically want to check if the player is running or walking and adjust his speed based on that. So first we define how much increase in speed we get from sprinting:

private float sprintSpeedMultiplier = 1.3f; // ie 30% faster

Now we replace the aforementioned controller.Move line with the following:

// First we check if our 'running condition' is met, ie grounded + leftshift
// Also note, we want to use GetKey, not GetKeyDown.
// GetKeyDown is only true for one frame!
bool running = Input.GetKey(KeyCode.LeftShift) && isGrounded;

// Now we use this information to adjust out desired speed.
// Here i'm using a tertiary operator. It basically reads as
// "If 'running' use first value, else second value"
float speedMultiplier = running ? speed * sprintSpeedMultiplier : speed;

// Now we apply the calculated speed like before:
controller.Move(move * speedMultiplier * Time.deltaTime);

As i typed this here in the browser, i hope there are no typos involved. You should get the idea tho.
That said, i’m not a huge fan of how this whole controller is handled, and would much rather lead you to follow this series on character creation (modelling, animating, scripting movement with walk / run, …, follow camera, …) by Sebastian Lague. He is really good at making sense of stuff and he also explains how everything works pretty well.
(For videos about modelling, animating and so on start at episode 1, this is where scripting begins)

will do thx,