So i’m trying to make simple platformer in unity 2020.2.2f1. I made a character controller but it doesn’t work properly. Even though player jumps and moves in every direction there is something weird going on. It all started when i tried implementing jumping. I use addforce for jumping and when player is falling he sometimes falls through ground for a second, creating bouncy effect. it’s really weird. I tried changing collsion mode to continous and basically everything i could find but nothing works. What i found out is velocities of player are acting really weird. Their value sometimes is equal to billions below zero. I forgot to mention that i’m using simplified version of Brackeys movement script ( without crouching etc).For all this weird behaviors i blame Vector3.SmoothDamp but i don’t have much experience. Here is the code :
.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public float speed = 0.01f;
public float JumpForce = 2f;
private Vector3 velocity = Vector3.zero;
void Start()
{
}
void FixedUpdate()
{
Rigidbody2D rb = GetComponent<Rigidbody2D>();
if (Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(Vector2.up * JumpForce, ForceMode2D.Impulse);
}
float horizontal = Input.GetAxisRaw("Horizontal");
Vector3 targetVelocity = new Vector2(speed * horizontal, rb.velocity.y);
rb.velocity = Vector3.SmoothDamp(rb.velocity, targetVelocity, ref velocity, 0.3f);
}
private void Update()
{
Rigidbody2D rb = GetComponent<Rigidbody2D>();
if (Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(Vector2.up * JumpForce, ForceMode2D.Impulse);
}
}
}
,