- Home /
Problem with jumping, total noob.
Hello!
I am trying to make a simple script for a 2D game that controls the player's motion. It was working great until I added a walking animation; now the player simply teleports upwards a few inches and floats back down (obviously not what is intended). I am not sure what the problem is caused by.
Please feel free to tear apart the script in the process of helping me fix the issue. Let me know everything that I could do better, even if it is totally unrelated. Thanks!
#pragma strict
private var move :float;
private var sprinting : float;
var jumping : boolean;
var jumpforce = Vector2 (0, 30000);
var velo : Vector2;
var animator : Animator;
function Start () {
animator = GetComponent("Animator");
}
function FixedUpdate () {
move = 0.0;
sprinting = 1.0;
if (rigidbody2D.velocity.y <= 0.2){
jumping = false;
}
else{
jumping = true;
}
if (Input.GetKey (KeyCode.A) == true){
move = -1;
animator.SetBool("Walking", true);
}
if (Input.GetKey (KeyCode.D) == true){
move = 1;
animator.SetBool("Walking", true);
}
if (Input.GetKeyUp (KeyCode.A) == true){
move = 0;
animator.SetBool("Walking", false);
}
if (Input.GetKeyUp (KeyCode.D) == true){
move = 0;
animator.SetBool("Walking", false);
}
if (Input.GetKey (KeyCode.LeftShift)){
sprinting = 2;
}
if (Input.GetKeyUp (KeyCode.LeftShift)){
sprinting = 1;
}
if ((Input.GetKeyDown(KeyCode.W))){
rigidbody2D.AddForce(Vector2(0,30000));
}
velo = rigidbody2D.velocity;
velo.x = (4 * move * sprinting);
rigidbody2D.velocity = velo;
}
Please insert a space between your )
's and your {
's. We (as a community) may not be able to agree on the placement of the notorious {
, but we can all agree on the fact that:
if (condition){
Is very ugly and inconsistent. If you're going to put the {
on the same line, at least separate it from the statement:
if (condition) {
Also, you should have consistent spacing for function calls. Why define functions like this:
function Name () {
But then use them like this:
Name();
And then later use them like this:
Name ();
Noone's going to tell you off for using either method, but at least stay consistent throughout your code.
That said, are you sure the position isn't being set by the animation itself?
Thanks, I'll try to be consistent from now on. How should I check to see if the animation is setting the position? By the way, walking back and forth works fine, just the jumping is messed up. Thanks for the help!
my best recommendation would be watch the tutorials of walkerboystudios, they are amazing, I learnt from them
with:
rigidbody2D.AddForce ( Vector2(0, > 200) );
you need to add:
rigidbody2D.AddForce( new Vector2(0, 200) );
or create a new variable Vector2 before and do like this:
Vector2 jumpForce = new Vector2(0, 200);
if (something happen)
{
rigidbody2D.AddForce(jumpForce);
}
sorry for bad code...
Your answer
Follow this Question
Related Questions
driving game power ups 1 Answer
how to capsule cast from within Entities.ForEach job? ECS 1 Answer
I am having a problem trying to simulate football(soccer) physics 2 Answers
CharacterController unexpected collisions with MeshCollider 0 Answers
3rd person character controller not responding correctly 1 Answer