so im still a beginner and trying to create a personal project, but when my character collides with something, it slowly moves away, it looks like its somehow connected to the rotation but im not sure why this happens. I played with rb2d settings and code a little bit, but nothing seems to work
.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5f; // Speed of player movement
//dash variables
private bool isDashing;
public float dashSpeed = 15.0f;
public float dashCooldown = 3f;
public float dashCooldownTimer = 0f;
public float movementCooldown = 0.5f;
public float movementCooldownTimer = 0f;
private Rigidbody2D rb;
private Collider2D col;
void Start()
{
rb = GetComponent<Rigidbody2D>();
col = rb.GetComponent<Collider2D>();
isDashing = false;
}
void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
Vector2 movement = new Vector2(horizontalInput, verticalInput);
Vector3 mousePosition = Input.mousePosition;
mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
// Karakterin pozisyonunu al
Vector3 characterPosition = transform.position;
// Yön vektörünü hesapla
Vector2 direction = new Vector2(
mousePosition.x - characterPosition.x,
mousePosition.y - characterPosition.y
);
// Karakterin rotasyonunu hesapla
transform.up = direction;
// Update the cooldown timers
if (dashCooldownTimer > 0)
{
dashCooldownTimer -= Time.deltaTime;
}
if (movementCooldownTimer > 0)
{
movementCooldownTimer -= Time.deltaTime;
}
// Check for dash input
if (Input.GetKey(KeyCode.Space) && dashCooldownTimer <= 0)
{
isDashing = true;
dashCooldownTimer = dashCooldown; // Reset dash cooldown
col.enabled = false;
}
// Perform the dash
if (isDashing)
{
rb.velocity = movement * dashSpeed;
movementCooldownTimer = movementCooldown; // Start movement cooldown
isDashing = false;
}
// Perform normal movement
else if (movementCooldownTimer <= 0)
{
rb.velocity = movement * moveSpeed;
col.enabled = true;
}
}
}