Hi,
for a sidescroller I wanted to make a jump, that feels right. That means more jumpforce (1500) and more gravity(9.8), so that it feels like the Player got a little bit more weight. The Problem that I have is, after a jump, the Player glitches a litte bit through the ground, because of the high gravity (Image). Is there any way to solve that?
Here is the Player script:
public class Player : Charakter {
private static Player instance;
public static Player Instance{
get{
if(instance == null){
instance = GameObject.FindObjectOfType<Player> ();
}
return instance;
}
}
[SerializeField]
private Transform[] groundPoints;
[SerializeField]
private float groundRadius;
[SerializeField]
private LayerMask whatIsGround;
[SerializeField]
private bool airControl;
[SerializeField]
private float jumpForce;
public Rigidbody2D MyRigidbody {get;set;}
public bool Jump {get;set;}
public bool OnGround {get;set;}
private Vector2 startPos;
public bool isInsideBubble = false;
//public override bool IsDead get{return health <= 0; }}
public override void Start () {
base.Start();
startPos = transform.position;
MyRigidbody = GetComponent<Rigidbody2D>();
}
void Update(){
HandleInput();
}
void FixedUpdate () {
float horizontal = Input.GetAxis ("Horizontal");
OnGround = IsGrounded();
HandleMovement (horizontal);
Flip (horizontal);
HandleLayers();
}
private void HandleMovement(float horizontal){
if(MyRigidbody.velocity.y<0){
MyAnimator.SetBool ("land", true);
}
if((OnGround || airControl)){
MyRigidbody.velocity = new Vector2 (horizontal * movementSpeed, MyRigidbody.velocity.y);
}
if(Jump && MyRigidbody.velocity.y == 0 ){
MyRigidbody.AddForce (new Vector2 (0, jumpForce));
isInsideBubble = false;
}
if(isInsideBubble == true){
MyRigidbody.velocity = new Vector3(0,4,0);
MyRigidbody.AddForce (new Vector2 (0, jumpForce));
isInsideBubble = false;
MyRigidbody.isKinematic = false;
}
MyAnimator.SetFloat ("speed", Mathf.Abs (horizontal));
}
private void HandleInput(){
if(Input.GetKeyDown(KeyCode.Space)){
MyAnimator.SetTrigger("jump");
}
if(Input.GetKeyDown(KeyCode.V)){
ThrowKnife(0);
}
}
private void Flip(float horizontal){
if(horizontal > 0 && !facingRight || horizontal < 0 && facingRight){
ChangeDirection();
}
}
private bool IsGrounded(){
if (MyRigidbody.velocity.y <= 0){
foreach(Transform point in groundPoints){
Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);
for(int i = 0; i< colliders.Length; i++){
if(colliders[i].gameObject != gameObject){
return true;
}
}
}
}
return false;
}
private void HandleLayers(){
if(!OnGround){
MyAnimator.SetLayerWeight (1, 1);
}
else{
MyAnimator.SetLayerWeight (1, 0);
}
}
}
Greeting
Jannik