Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

I have a Dash function in my game but you can glitch through walls

Discussion in 'Input System' started by BlackWhite001, Jun 6, 2022.

  1. BlackWhite001

    BlackWhite001

    Joined:
    Jun 4, 2022
    Posts:
    2

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Player : MonoBehaviour
    7. {
    8.     public float speed;
    9.     public float rotationSpeed;
    10.     public float dashspeed;
    11.  
    12.     private float Cooldown;
    13.  
    14.     public int currentStamina;
    15.     //fügt die Stamina Klasse ein
    16.     public Stamina staminaa;
    17.  
    18.     void Start()
    19.     {
    20.         currentStamina = 100;
    21.         staminaa.SetMaxStamina(100);
    22.     }
    23.  
    24.     void Update()
    25.     {
    26.         float x = Input.GetAxisRaw("Horizontal");
    27.         float y = Input.GetAxisRaw("Vertical");
    28.  
    29.         Vector3 movementDirection = new Vector3(x, 0, y);
    30.         movementDirection.Normalize();
    31.  
    32.         if (Input.GetKeyDown("f") && currentStamina >= 25)
    33.         {
    34.             transform.position += transform.TransformDirection(Vector3.forward) * dashspeed;
    35.             TakeDamage(25);
    36.         }
    37.         else
    38.         {
    39.             transform.Translate(movementDirection * speed * Time.deltaTime, Space.World);
    40.         }
    41.  
    42.  
    43.  
    44.         if (movementDirection != Vector3.zero)
    45.         {
    46.             Quaternion toRotation = Quaternion.LookRotation(movementDirection, Vector3.up);
    47.             transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, rotationSpeed * Time.deltaTime);
    48.         }
    49.        
    50.  
    51.         //Staminacharge and Cooldown
    52.         if (Cooldown >= 2 && currentStamina != 100)
    53.         {
    54.             ChargeAgain(5);
    55.             Cooldown = 0;
    56.         }
    57.         else if (currentStamina != 100)
    58.         {
    59.             //Cooldown
    60.             Cooldown += Time.deltaTime;
    61.         }
    62.     }
    63.  
    64.     void TakeDamage(int damage)
    65.     {
    66.         currentStamina -= damage;
    67.         staminaa.SetStamina(currentStamina);
    68.     }
    69.  
    70.     void ChargeAgain(int charge)
    71.     {
    72.         currentStamina += charge;
    73.         staminaa.SetStamina(currentStamina);
    74.     }
    75. }
    76.  
     
  2. naknuknik

    naknuknik

    Joined:
    Mar 14, 2021
    Posts:
    19
    Changing transform equals teleporting.You should be using rigidbody for movement if you want collisions.Also this seems like the wrong subforum as the Input system seems irrelevant to the issue at hand(collisions)
     
  3. BlackWhite001

    BlackWhite001

    Joined:
    Jun 4, 2022
    Posts:
    2
    could you show me how it would look like?
     
  4. naknuknik

    naknuknik

    Joined:
    Mar 14, 2021
    Posts:
    19