Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

How to stop a if statement after it has finished

Discussion in 'Getting Started' started by Roadkill3846298356923865, May 6, 2024.

  1. Roadkill3846298356923865

    Roadkill3846298356923865

    Joined:
    Apr 15, 2024
    Posts:
    6
    I NNNNEEEEEDDD HHHEEELLLPPP to change the guns direction after it reaches a certain angle then stopping the statement (also im just beggining so dont mind the horrid code)
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LookAtPlayer : MonoBehaviour
    6. {
    7.  
    8.  
    9.     public Transform player;
    10.     public Rigidbody2D rb;
    11.     public GameObject enemy;
    12.  
    13.     private Vector3 LookingRight;
    14.     private Vector3 LookingLeft;
    15.  
    16.  
    17.  
    18.     bool ITSDONE;
    19.     // Start is called before the first frame update
    20.     void Start()
    21.     {
    22.      
    23.       ITSDONE = false;
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     private void Update()
    28.     {
    29.      
    30.  
    31.         Vector3 direction = player.position - transform.position;
    32.         float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg - 180f;
    33.         rb.rotation = angle;
    34.  
    35.         transform.position = enemy.transform.position;
    36.  
    37.  
    38.       if( ITSDONE = true)
    39.         {
    40.            
    41.         }
    42.    
    43.       if(rb.rotation > (180f * Mathf.Rad2Deg) )
    44.         {
    45.           transform.localScale += new Vector3(-1.1f, -1f, -1f) * Mathf.Rad2Deg;
    46.           ITSDONE = true;
    47.         }
    48.         if(rb.rotation < (180f * Mathf.Rad2Deg))
    49.         {
    50.           transform.localScale += new Vector3(1.1f, 1f, 1f) * Mathf.Rad2Deg;
    51.           ITSDONE = true;
    52.         }
    53.     }
    54.  
    55.    
    56. }
    57.  
     
  2. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,936
    RigidBody2D.rotation gives you the angle in degrees, so you don't need to try to convert to degrees.

    I don't know why you're multiplying a scale by Mathf.Rad2Deg

    You're also continuosly increasing the scales over time by using "+="
     
    Last edited: May 6, 2024
  3. Roadkill3846298356923865

    Roadkill3846298356923865

    Joined:
    Apr 15, 2024
    Posts:
    6
    Thank you that did help quite a bit but even with that the arm will still not set its scale to what I wanted it to be But just again thank you a lot
     
  4. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,936
    Add
     Debug.Log (rb.rotation)
    to your Update code to print out the angle to the console, and try moving the player to different quadrants around the enemy and take note of what is printed to the console. From that, you can gather what angles you need to check in order to set the scales you want.
     
    dstears likes this.
  5. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,551
    You can exit a method early with
    return
    .
    Code (csharp):
    1. if (ITSDONE)
    2.     return;
     
  6. Roadkill3846298356923865

    Roadkill3846298356923865

    Joined:
    Apr 15, 2024
    Posts:
    6
    Yes thank you ones again but I have the angles but the scale will not change
     
  7. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,936
    You need to learn how to use Debug.Log to troubleshoot your own issues. Are you testing the correct angle? Are you in the if clause? Are you scaling the correct item?
    You can figure this out by yourself using Debug.Log.