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 - - YouTube
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++;
}
}
}
