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.

ridgidbody.addforce occasionally freezes test enemy in place.

Discussion in 'Scripting' started by Potion, Feb 5, 2018.

  1. Potion

    Potion

    Joined:
    Feb 5, 2018
    Posts:
    2
    I am creating a platformer where the player has a melee attack. I want the attack to bounce the enemy away from the player a small amount. although it usually works it occasionally causes the enemy to stop moving left.
    It also seems to work better the more force I add to the rigid body

    Here is the C#:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. public class RedControl : MonoBehaviour
    7. {
    8.     public int curHealth = 40;
    9.     private bool moveLeft = true;
    10.  
    11.     private Transform play;
    12.  
    13.     private Rigidbody2D rb;
    14.  
    15.     private Transform enemy;
    16.  
    17.     private bool ouch = false;
    18.     private float ouchTimer = 0;
    19.     private float ouchCd = 1f;
    20.     private bool hit = false;
    21.  
    22.     void Update()
    23.     {
    24.         if (ouch)
    25.         {
    26.             if (ouchTimer > 0)
    27.             {
    28.                 ouchTimer -= Time.deltaTime;
    29.             }
    30.             else if (ouchTimer <= 0)
    31.             {
    32.                 ouch = false;
    33.             }
    34.         }
    35.      
    36.         if (Input.GetKey(KeyCode.X) && ouch == false && hit == true && play.transform.position.x > enemy.position.x)
    37.         {
    38.             rb.AddForce(Vector2.left * 100f);
    39.             ouch = true;
    40.             ouchTimer = ouchCd;
    41.             hit = false;
    42.         }
    43.         if (Input.GetKey(KeyCode.X) && ouch == false && hit == true && play.transform.position.x < enemy.position.x)
    44.         {
    45.             rb.AddForce(Vector2.right * 100f);
    46.             ouch = true;
    47.             ouchTimer = ouchCd;
    48.             hit = false;
    49.         }
    50.         if (curHealth <= 0)
    51.         {
    52.             Destroy(gameObject);
    53.         }
    54.         if (moveLeft == true && ouch == false && hit == false)
    55.         {
    56.             transform.Translate(Vector2.left * 2f * Time.deltaTime);
    57.         }
    58.         if (moveLeft == false && ouch == false && hit == false)
    59.         {
    60.             transform.Translate(Vector2.right * 2f * Time.deltaTime);
    61.         }
    62.     }
    63.     public void Damage(int damage)
    64.     {
    65.         curHealth -= damage;
    66.         if (transform.tag == "Enemy")
    67.         {
    68.             {
    69.                 rb = GetComponent<Rigidbody2D>();
    70.                 enemy = transform;
    71.                 hit = true;
    72.                 play = GameObject.FindWithTag("Player").transform;
    73.             }
    74.         }
    75.      
    76.     }
    77. }
     
    Last edited: Feb 6, 2018
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Potion likes this.