Unity 2D - Terrain starts flipping when walking?!
Hello everyone, I got a bit of a stupid problem here. Basically I imported one asset as ground having a box collider (no settings changed) and being in a custom ground layer. Then I have a custom player layer where my Hero is with a circle collider, rigidbody2D, groundcheck etc ( see following pictures). Groundcheck works, my hero jumps only when placed on the ground, but when I start walking to the left or right, the ground goes crazy and flips around. When I stop walking, it basically gets thrown away. First, I thought I accidentally activated a rigidbody on it but no, I have absolutely no Idea what's going on. Here is the script and some pictures:
using UnityEngine;
using System.Collections;
public class Controller : MonoBehaviour {
public float speed;
public float jumpspeed;
Rigidbody2D rb;
Animator anim;
public Transform groundCheck;
public LayerMask WhatIsGround;
public float groundRadius;
bool grounded;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void FixedUpdate () {
grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, WhatIsGround);
if (Input.GetKey (KeyCode.LeftArrow)) {
rb.velocity = new Vector2(-speed, rb.velocity.y);
anim.SetInteger ("velc",1);
}
if (Input.GetKey (KeyCode.RightArrow)) {
rb.velocity = new Vector2(speed, rb.velocity.y);
anim.SetInteger ("velc",1);
}
if (Input.GetKey (KeyCode.Space)&& grounded==true) {
rb.velocity = new Vector2(rb.velocity.x, jumpspeed);
}
if (Input.anyKey == false) {
anim.SetInteger ("velc", 0);
}
}
}
Tell me if you need anything more, I hope anyone can help. Best regards
If you set Gravity Scale = 0 on the player's rigidbody does it make a difference?
@pako i will try it when I get home but if I set the gravity to 0, doesn't it mean that i wont land on Ground again after jumping?
@Pako tried it out, ground stopped spinning but character walking and jumping didn't work clearly if gravity is off :\
@castyll this was expected behavior. I just wanted to see if the Gravity Scale setting was part of the problem, and it is.
Well, you could simulate gravity by having a fallingspeed variable, with a value about equal to the jumpspeed. Then you'd decrease the 'y' component of rb.velocity when the player has finished the jump. I've got some code for this somewhere, and I could dig it up if you like.
However, the most interesting part is that I tried to replicate your ground flipping problem, and I just couldn't. I took some shortcuts to make things simpler, like no animation, and no layers. O$$anonymous$$, the player can jump any time, but the player can move left/right and jump, and the ground just stays put, no flipping.
So, since I can't replicate the problem, I can't give you an alternative to the gravity simulation solution, as I said above.
I agree with pako. from your screenshot there is no way that the ground moves an inch. biggest difference I see is the setting of the int on the animator.is it possible, that you accidentally animated the position of the player or something and there's keyframes trying to move it around?