- Home /
,My Character is falling through my Tilemap
, 1. I have 2. different tilemaps ("Background","Foreground") -> Foreground has a "Tilemap Collider 2D" 2. I have 2. characters -> Capsule Collider 2D & Rigidbody 2D (not falling through tilemap) -> Character Controller & Character Movement (Script) (falling through tilemap)
This is the script I wrote:
public class CharacterMovement2 : MonoBehaviour { //Public Variables public float gravity; //Private Variables private CharacterController _characterController; private float currentGravity;
public float speed;
// Start is called before the first frame update
void Start()
{
_characterController = gameObject.GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
Vector3 finalMovement = Movement() + Gravity();
_characterController.Move(finalMovement * Time.deltaTime);
}
//erstelle eine Methode, die einen Vector3 wiedergibt Vector3 Movement() { //erstelle einen leeren ("zero") Vector3 "moveVector" Vector3 moveVector = Vector3.zero;
moveVector += transform.up * Input.GetAxis("Vertical");
moveVector += transform.right * Input.GetAxis("Horizontal");
moveVector *= speed;
return moveVector;
}
Vector3 Gravity()
{
Vector3 gravityMovement = new Vector3(0, -currentGravity, 0);
currentGravity += gravity * Time.deltaTime;
if (_characterController.isGrounded)
{
if( currentGravity > 1f)
{
currentGravity = 1f;
}
}
return gravityMovement;
}
}
Your answer
Follow this Question
Related Questions
Rigidbody colliding with every section of floor 2 Answers
Jumping and reverse gravity with a Rigidbody2D (based on the Ruby tutorials) 1 Answer
BoxCollider2D failling verification when Instantiate (Fixed, Cause: big derp) 1 Answer
How do I make a rigidbody2d that doesn't do anything on collision? 1 Answer