Search Unity

Why cant i stop my RigidBody2D

Discussion in 'Scripting' started by epochplus5, Oct 19, 2020.

  1. epochplus5

    epochplus5

    Joined:
    Apr 19, 2020
    Posts:
    677
    why isnt this line working on my RigidBody2D for the player?

    rb.velocity = Vector3.zero;

    its working in once instance to stop a bomb from travelling through the ground
     
  2. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    Your RigidBody2D must be set somewhere else and ran before the velocity gets cleared over and over; if the problem is that it is still moving. Maybe create a boolean that says that the object must stop and stay stopped so that the RigidBody2D will not have velocity increase anywhere else at anytime while you are trying to stop it.
     
  3. epochplus5

    epochplus5

    Joined:
    Apr 19, 2020
    Posts:
    677
    Thanks Adehm....nice sword

    do i need to enclose all my moving data into a bool?

    here is my script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5.  
    6. public class PlaneController : MonoBehaviour
    7. {
    8.     Rigidbody2D rb;
    9.     [Tooltip("World units per second.")]
    10.     public float moveSpeed;
    11.  
    12.    
    13.     bool stalling = false;
    14.     bool groundCrash = false;
    15.  
    16.     public float currentRotationSpeed;
    17.     float desiredRotationSpeed;
    18.     bool turningLeft = false;
    19.     bool turningRight = false;
    20.    
    21.     public Animator anim;
    22.  
    23.     private bool CanDropBomb = true;
    24.     public GameObject bomb1;
    25.     public GameObject bullet1;
    26.     private bool firingBullet1 = false;
    27.     private bool dropBomb = false;
    28.     public float bulletSpeed = 500;
    29.     public float bombSpeed = 90;
    30.     public GameObject BulletSpawnObject;
    31.     public GameObject BombSpawnObject;
    32.  
    33.     private Renderer[] renderers;
    34.     private bool isWrappingX = false;
    35.  
    36.     SpriteRenderer spriteRenderer;
    37.  
    38.     void Start()
    39.     {
    40.         renderers = GetComponentsInChildren<Renderer>();
    41.     }
    42.  
    43.     private void Awake()
    44.     {
    45.         rb = GetComponent<Rigidbody2D>();
    46.         spriteRenderer = GetComponent<SpriteRenderer>();
    47.     }
    48.  
    49.     // Update is called once per frame
    50.     void Update()
    51.     {
    52.         float angle = transform.eulerAngles.z;
    53.  
    54.  
    55.         if (turningLeft)
    56.         {
    57.             desiredRotationSpeed = 100;
    58.         }
    59.  
    60.         else if (turningRight)
    61.         {
    62.             desiredRotationSpeed = -100;
    63.         }
    64.  
    65.         else
    66.         {
    67.             SetFlipY();
    68.         }
    69.  
    70.         currentRotationSpeed = Mathf.MoveTowards(currentRotationSpeed, desiredRotationSpeed, 1);
    71.         transform.Rotate(0, 0, currentRotationSpeed * Time.deltaTime);
    72.  
    73.         if (stalling)
    74.         {
    75.             if (angle < 110 && angle > 70)
    76.             {
    77.                 Debug.Log("Angle = " + angle);
    78.                 rb.gravityScale = 25;
    79.  
    80.                 stalling = false;
    81.             }
    82.             else
    83.             {
    84.                 rb.gravityScale = 15;
    85.  
    86.                 stalling = false;
    87.             }
    88.  
    89.             StartCoroutine(StallTime());
    90.         }
    91.  
    92.         if (firingBullet1)
    93.         {
    94.             GameObject newBullet1 = Instantiate(bullet1, BulletSpawnObject.transform.position, transform.rotation);
    95.             newBullet1.GetComponent<Rigidbody2D>().AddRelativeForce(Vector2.right * bulletSpeed);
    96.             Destroy(newBullet1, 2.0F);
    97.             FindObjectOfType<AudioManager>().Play("Gun1");
    98.             firingBullet1 = false;
    99.         }
    100.  
    101.         if (dropBomb)
    102.         {
    103.             GameObject newBomb = Instantiate(bomb1, BombSpawnObject.transform.position, transform.rotation);
    104.             newBomb.GetComponent<Rigidbody2D>().AddRelativeForce(Vector2.right * bombSpeed);
    105.             dropBomb = false;
    106.             Destroy(newBomb, 3.5F);
    107.             StartCoroutine(DropBombDelay());
    108.             CanDropBomb = false;
    109.         }
    110.  
    111.         if (groundCrash)
    112.         {
    113.             rb.velocity = Vector3.zero;
    114.             FindObjectOfType<AudioManager>().Play("Explosion1");
    115.             groundCrash = false;
    116.             anim.SetBool("Crashed", true);
    117.         }
    118.     }
    119.  
    120.     //rotating the plane
    121.     //Turning left
    122.     public void TurningLeft ()
    123.     {
    124.         turningLeft = true;
    125.     }
    126.  
    127.     public void StopTurningLeft()
    128.     {
    129.         desiredRotationSpeed = 0;
    130.         turningLeft = false;
    131.     }
    132.  
    133.     //Turning right
    134.     public void TurningRight()
    135.     {
    136.         turningRight = true;
    137.     }
    138.  
    139.     public void StopTurningRight()
    140.     {
    141.         desiredRotationSpeed = 0;
    142.         turningRight = false;
    143.     }
    144.  
    145.  
    146.  
    147.     private void FixedUpdate()
    148.     {
    149.         rb.velocity = transform.right * moveSpeed;
    150.         ScreenWrap();
    151.     }
    152.  
    153.     void SetFlipY()
    154.     {
    155.         // I'm not going to base it off of rot but rather off of the
    156.         // sign of the x component of the transform.right vector.
    157.         bool flipy = transform.right.x < 0;
    158.         spriteRenderer.flipY = flipy;
    159.     }
    160.  
    161.    void ScreenWrap()
    162.     {
    163.         bool isVisible = CheckRenderers();
    164.  
    165.         if (isVisible)
    166.         {
    167.             isWrappingX = false;
    168.             return;
    169.         }
    170.  
    171.         if (isWrappingX)
    172.         {
    173.             return;
    174.         }
    175.  
    176.         Vector3 newPosition = transform.position;
    177.  
    178.         if (newPosition.x > 1 || newPosition.x < 0)
    179.         {
    180.             newPosition.x = -newPosition.x;
    181.             isWrappingX = true;
    182.         }
    183.  
    184.         transform.position = newPosition;
    185.     }
    186.  
    187.     bool CheckRenderers()
    188.     {
    189.         foreach (Renderer renderer in renderers)
    190.         {
    191.             if (renderer.isVisible)
    192.             {
    193.                 return true;
    194.             }
    195.         }
    196.  
    197.         return false;
    198.     }
    199.  
    200.     void OnTriggerEnter2D(Collider2D col)
    201.     {
    202.         if (col.gameObject.tag == "HeightBorder")
    203.         {
    204.             stalling = true;
    205.         }
    206.  
    207.         if (col.gameObject.tag == "GroundBorder")
    208.         {
    209.             groundCrash = true;
    210.         }
    211.  
    212.  
    213.     }
    214.  
    215.     IEnumerator StallTime()
    216.     {
    217.  
    218.         yield return new WaitForSeconds(0.5f);
    219.         rb.gravityScale = 0;
    220.     }
    221.  
    222.     public void FireBullet()
    223.     {
    224.         firingBullet1 = true;
    225.     }
    226.  
    227.     public void DropBomb()
    228.     {
    229.         if (CanDropBomb)
    230.         dropBomb = true;
    231.  
    232.     }
    233.  
    234.     IEnumerator DropBombDelay()
    235.     {
    236.         yield return new WaitForSeconds(3f);
    237.         CanDropBomb = true;
    238.     }
    239. }
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Identify all the places where velocity might be restored and guard that. To me this looks to be just line 149 above but I didn't study the code super-closely.