I’m pretty new to unity so I usually just follow tutorials to from YouTubers. This script is supposed to tell unity that when I “Jump” (press space) It makes me jump but when I go in play mode nothing happens. Here is my script. Another problem is that since I’m so new I don’t know where on the internet to look for help so I hope this works and one of you Unity pros can fix this for me.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour{
public CharacterController2D controller;
public float runSpeed = 40f;
float horizontalMove = 0f;
bool jump = false;
// Update is called once per frame
void Update()
{
horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
if (Input.GetButtonDown("Jump"))
{
jump = true;
}
}
void FixedUpdate () //FixedUpdate happens a specific time every second
{
controller.Move(horizontalMove * Time.fixedDeltaTime, false, jump);
jump = false;
}
}
1st: You should post your code in code tags (see the ribbon before you post, or edit your post and change it).
2nd: You are setting the jump bool to true when you press space, but you are making jump = false every time FixedUpdate runs (a lot).
3rd: You havent defined what happens when jump = true. Once it becomes true something should happen.
Basically, you will want to have a rigidbody2d on your object, when jump = true, a force is applied one time upwards on the character. Also, you want some way to check if the player is grounded. When the player is grounded, they can jump, but when they are not (aka in the air) then grounded = false and they cannot jump.
That is the video that I tried using and I followed all the steps
also I tried doing what you said about adding a force whenever I jump but I couldn’t figure out how to. I also already have a rigid body 2D on my player
Definitely follow what @eses suggested. As for adding force, use the rigidbody2D.AddForce method.
Something like this:
rb2D.AddForce(transform.up * thrust);
Since you are so new, a word of advice (and i mean this in the most helpful way possible) is to get really good at Googling your issues. Im sure you have tried googling this but try to break it down into pieces.
For example, you are having jumping issue and you are using a rigidbody2D, so look up Rigidbody2D on the docs and scroll down until you see the methods. Those are the methods that will appear when you type your rigidbody reference.
Look for the AddForce and then click on that. If it still doesnt work, google “Rigidybody2D.AddForce not working”.
Another helpful thing is to use Debug.Logs liberally. The are a great way to easily test if parts of your code is running. Throw one in Start to make sure the script is even running, throw one in your Jump function (when you press space) to make sure that it is registering the input, etc. I use these all the time, even after like 3 years of working with Unity and I was as new as you are to Unity and programming.
@Cornysam I tried using add force and watched the whole video but nothing seems to work. I also tried searching RigidBody2D.Adforce not working but still didn’t find anything. In the Brackeys video he gave a link to the character controller so I used that, maybe their is something wrong with the code or just a bug with unity. I still have no idea what is wrong though. EDIT: I also tried Debug.Log and it was picking up the jump but just not executing it
Try increasing the jumpForce a lot. Depending on the character’s mass and the gravity, even a value of like 50 may not show anything. Make it like 150 or more to see something.
I am having a similar problem, except that I am getting the error code that ‘jump’ is not defined, but I have it set up in my PlayerInput window: I have checked the script for the PlayerInput and Jump is clearly defined in it, does anyone have suggestions other than to google it, because to be honest this forum is a last resort after having taken google as far as I could.
You spelled jump with a lowercase j in your text above, but the screenshot shows “Jump” with a capital J. C# is case sensitive. If that doesnt work, share your code.
Jump appears in the code correctly, I was just stating an example not a direct quote of what is in the script. The issue was not in the code, it was in the Input system. Had to delete it and create a new one, now the script recognizes the Jump command, but for some reason the Mixamo animation doesn’t work. I have the transitions set up like the tutorials say, but I can’t figure out why the transition from idle, run, walk, any state, or just putting Jump in the animator by itself doesn’t work. I am more than likely going to have to delete and re download the animation and convert it to humanoid again. Thanks for your reply. Do you happen to know how to set up a character’s ability to pick up and throw an object using the input system? All the tutorials I find using google do not explicitly teach this.
Ive seen tutorials on picking up and throwing things but they seem to do them differently and with different results. I dont know how exactly but essentially you need a PickUp animation and a Throw animation. Then, when the player reaches a an object that can be picked up, it detects with with a raycast or collider it checks for the Input. Once input is called it “picks up the item” via animation and the code. Then you have some sort of bool or check that knows there is an item to throw and after some Input press you “throw” the object.
This might be a little late but, check if your Ground Check is right. I moved my Ground Check so that it was a little under the collision box and it worked.
This is the bare minimum of information to report:
what you want
what you tried
what you expected to happen
what actually happened, log output, variable values, and especially any errors you see
links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)
The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?
If you post code, only post the relevant code and always use the format button above. Do not post photographs of code.