Jump Force/Velocity is Wrong and Strange. Help
Hello, it's been a long time i was trying to resolve this problem by myself, but I don't see where it is. The problem is in my Jumping - it's very glitchy and my character jumps on different hight everytime. If you can help and need some information, please ask, i'll give every info i can :) I hold my space button to jump and this video will show what happening - https://www.youtube.com/watch?v=b26j5ISyivw It's just randomly jumping and I want in to be a Straight Jump Move like in every game I think the problem is in JumpForce, please help!
Here is my Script PlayerController
using UnityEngine; using System.Collections;
public class PlayerControllerScript : MonoBehaviour {
public float maxSpeed = 10f;
public float jumpForce = 700f;
bool facingRight = true;
Animator anim;
bool grounded = false;
public Transform groundCheck;
public float groundRadius = 0.2f;
public LayerMask whatIsGround;
public float move;
public float score;
public float spawnX, spawnY;
void Start ()
{
anim = GetComponent<Animator>();
spawnX = transform.position.x;
spawnY = transform.position.y;
}
void Update()
{
if (grounded && Input.GetKey ("space")) {
anim.SetBool ("Ground", false);
GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0f, jumpForce));
GetComponent<Rigidbody2D> ().velocity = (new Vector2 (move * maxSpeed * Time.deltaTime, GetComponent<Rigidbody2D> ().velocity.y));
}
if (Input.GetKeyDown (KeyCode.R))
Application.LoadLevel (Application.loadedLevel);
}
void FixedUpdate()
{
grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
float move = Input.GetAxis ("Horizontal");
anim.SetBool ("Ground", grounded);
anim.SetFloat ("vSpeed", GetComponent<Rigidbody2D>().velocity.y);
anim.SetFloat ("Speed", Mathf.Abs (move));
GetComponent<Rigidbody2D>().velocity = new Vector2 (move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
if (move > 0 &&!facingRight)
Flip ();
else if (move < 0 && facingRight)
Flip ();
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
void OnCollisionEnter2D(Collision2D col){
if (col.gameObject.layer == LayerMask.NameToLayer ("killObjects") || col.gameObject.layer == LayerMask.NameToLayer ("dieCollider"))
transform.position = new Vector3 (spawnX, spawnY, transform.position.z);
if (col.gameObject.name == "endLevel")
Application.LoadLevel ("scene2");
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.layer == LayerMask.NameToLayer ("food")) {
Destroy (col.gameObject);
score++;
}
}
}
Answer by LucianoMacaDonati · Dec 30, 2016 at 04:18 PM
Hello !! I'm not completely sure where your problem is, but we could start narrowing it down by removing the bad practices, and some things I see could cause strange behaviors.
For jumping, I would recommend applying a force in the .up direction instead of manually modifying the velocity in your update (you're actually doing it in BOTH update and fixedUpdate). Look here to achieve this.
Besides this, I would strongly recommend that you don't use GetComponent in your updates. This is somewhat an expensive operation and it can be simply solved in several ways.
Let me know how it goes. Good luck.
Okay,thanks for reply!) I changed my GetComponent to private function in script
private Rigidbody2D rb2d;
void Start ()
{
rb2d = gameObject.GetComponent<Rigidbody2D> ();
anim = GetComponent<Animator>();
spawnX = transform.position.x;
spawnY = transform.position.y;
}
But I'm not sure that I add .up correctly, but no errors are showed)
void Update()
{
if (grounded && Input.Get$$anonymous$$ey ("space")) {
anim.SetBool ("Ground", false);
rb2d.AddForce (Vector2.up * jumpForce);
rb2d.velocity = (new Vector2 (move * maxSpeed * Time.deltaTime, rb2d.velocity.y));
}
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.R))
Application.LoadLevel (Application.loadedLevel);
}
void FixedUpdate()
{
grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
float move = Input.GetAxis ("Horizontal");
anim.SetBool ("Ground", grounded);
anim.SetFloat ("vSpeed", rb2d.velocity.y);
anim.SetFloat ("Speed", $$anonymous$$athf.Abs (move));
rb2d.velocity = new Vector2 (move * maxSpeed,rb2d.velocity.y);
if (move > 0 &&!facingRight)
Flip ();
else if (move < 0 && facingRight)
Flip ();
}
Do you think I must to get rid of some functions for velocity or AddForce in Update or FixedUpdate ? And i am doing right what you said?)
Why are you changing the velocity on both updates ? Why update the velocity at all ??
Why don't you remove the "grounded" check for now until you get the jumping working ? Simply do
if (Input.Get$$anonymous$$eyDown("space"))
rb2d.AddForce (Vector2.up * jumpForce);
Comment out the other stuff for now.
Your answer
Follow this Question
Related Questions
Jumping on enemies help 0 Answers
problem with jump script 2 Answers
I can't do jump in my 2D game 1 Answer