Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question Character starts slowly drifting upon collision

Discussion in '2D' started by ardaatk1, Jun 6, 2024.

  1. ardaatk1

    ardaatk1

    Joined:
    Jul 27, 2023
    Posts:
    1
    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
    .
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.  
    8.     public float moveSpeed = 5f; // Speed of player movement
    9.     //dash variables
    10.     private bool isDashing;
    11.     public float dashSpeed = 15.0f;
    12.     public float dashCooldown = 3f;
    13.     public float dashCooldownTimer = 0f;
    14.     public float movementCooldown = 0.5f;
    15.     public float movementCooldownTimer = 0f;
    16.  
    17.     private Rigidbody2D rb;
    18.     private Collider2D col;
    19.  
    20.     void Start()
    21.     {
    22.         rb = GetComponent<Rigidbody2D>();
    23.         col = rb.GetComponent<Collider2D>();
    24.         isDashing = false;
    25.     }
    26.     void Update()
    27.     {
    28.  
    29.         float horizontalInput = Input.GetAxis("Horizontal");
    30.         float verticalInput = Input.GetAxis("Vertical");
    31.         Vector2 movement = new Vector2(horizontalInput, verticalInput);
    32.         Vector3 mousePosition = Input.mousePosition;
    33.         mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
    34.  
    35.         // Karakterin pozisyonunu al
    36.         Vector3 characterPosition = transform.position;
    37.  
    38.         // Yön vektörünü hesapla
    39.         Vector2 direction = new Vector2(
    40.             mousePosition.x - characterPosition.x,
    41.             mousePosition.y - characterPosition.y
    42.         );
    43.  
    44.         // Karakterin rotasyonunu hesapla
    45.         transform.up = direction;
    46.  
    47.         // Update the cooldown timers
    48.         if (dashCooldownTimer > 0)
    49.         {
    50.             dashCooldownTimer -= Time.deltaTime;
    51.         }
    52.  
    53.         if (movementCooldownTimer > 0)
    54.         {
    55.             movementCooldownTimer -= Time.deltaTime;
    56.         }
    57.  
    58.         // Check for dash input
    59.         if (Input.GetKey(KeyCode.Space) && dashCooldownTimer <= 0)
    60.         {
    61.             isDashing = true;
    62.             dashCooldownTimer = dashCooldown; // Reset dash cooldown
    63.             col.enabled = false;
    64.         }
    65.  
    66.         // Perform the dash
    67.         if (isDashing)
    68.         {
    69.             rb.velocity = movement * dashSpeed;
    70.             movementCooldownTimer = movementCooldown; // Start movement cooldown
    71.             isDashing = false;
    72.         }
    73.         // Perform normal movement
    74.         else if (movementCooldownTimer <= 0)
    75.         {
    76.             rb.velocity = movement * moveSpeed;
    77.             col.enabled = true;
    78.         }
    79.     }
    80. }
    81.  
     
    Last edited: Jun 6, 2024