- Home /
I cant make my character jump ?,Why can't he jump ?
So ı started looking at the tutorials for the c# today (its my first time) and find a 25 min video about how to code movements for my character, everyhing was working until ı added jumping. Well everything is still working but ı cant jump. anyone who knows c# can you say what ı did wrong here? here is the code: public class Playermovementscrip : MonoBehaviour { public CharacterController controller;
public float speed = 12f;
public float gravity = -9.81f;
public float JumpHeight = 3f;
public Transform groundCheck;
public float groundDistence = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
bool isGrounded;
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistence, 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);
if(Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(JumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
} I just wanted to make a little game demo for fun but no :/,ı found a 25 min tutorial on youtube for how to apply FPS Controller. everything was working til the last think ı addet (jumping) ı rewire all of the last stuff but no its still not working and ı have no idea why... (its my first day looking at c#. no accualy its my first time looking to coding launguace for gaming and stuff) here is the code:
public class Playermovementscrip : MonoBehaviour { public CharacterController controller;
public float speed = 12f;
public float gravity = -9.81f;
public float JumpHeight = 3f;
public Transform groundCheck;
public float groundDistence = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
bool isGrounded;
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistence, 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);
if(Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(JumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
as like ı said I have no clue why it isnt working its the same as the code on youtube... (FIRST PERSON MOVEMENT in Unity - FPS Controller/brackeys)
if you view the script from the inspector you need to assign "groundMask" a layer then assign that same layer to the ground your player walks on.
https://www.youtube.com/watch?v=_QajrabyTJc there is 3 diffrent times this guy adds something to the scrip. I think the whole video will be more usefull. thanks for the helping hand. :)
Your answer
Follow this Question
Related Questions
Cant Jump ?c# please 2 Answers
Making a bubble level (not a game but work tool) 1 Answer
Can't get character to Jump on the Y Axis (C#) 2 Answers
Jumping mechanism 1 Answer
why I don't have in the butter one click player movement ? 1 Answer