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
  3. Dismiss Notice

Bug Adapted Unity 2D script not working as intended

Discussion in '2D' started by Wolfgang385, May 15, 2024.

  1. Wolfgang385

    Wolfgang385

    Joined:
    Nov 11, 2022
    Posts:
    14
    Hi guys so I made a unity script to sort of cannonball launch a projectile, but i ran into a problem as the cannon ball only moves up and down (y-axis) and doesn't move at all on the x-axis which eventually leads to a NAN error cuz of the math being used. What can I change in my script to make it work? (I call JumpAttack in a random if statement just to test it for now so dw abt that cuz it's working)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class IDK : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     private Transform target;
    9.  
    10.     [SerializeField]
    11.     private float initialAngle;
    12.  
    13.     private Rigidbody2D rb;
    14.     private Vector2 p;
    15.     private float gravity = -9.8f;
    16.     private float angle;
    17.  
    18.     private GameObject player;
    19.  
    20.     // Start is called before the first frame update
    21.     void Start()
    22.     {
    23.         player = GameObject.FindGameObjectWithTag("Player");
    24.  
    25.         rb = GetComponent<Rigidbody2D>();
    26.         p = target.position;
    27.         angle = initialAngle * Mathf.Deg2Rad;
    28.  
    29.     }
    30.  
    31.     void JumpAttack()
    32.     {
    33.         Vector2 planarTarget = new Vector2 (p.x, 0);
    34.         Debug.Log("1");
    35.         Vector2 planarPosition = new Vector2 (transform.position.x, 0);
    36.         Debug.Log("2");
    37.         float yOffset = transform.position.y - p.y;
    38.         Debug.Log("3");
    39.         float distance = Vector2.Distance(planarTarget, planarPosition);
    40.         Debug.Log("4");
    41.         float initialVelocity = (1 / Mathf.Cos(angle)) * Mathf.Sqrt((0.5f * gravity * Mathf.Pow(distance, 2)) / (distance * Mathf.Tan(angle) + yOffset));
    42.         Debug.Log("5");
    43.         Vector2 velocity = new Vector2(initialVelocity * Mathf.Cos(angle), initialVelocity * Mathf.Sin(angle));
    44.         Debug.Log("6");
    45.         float angleBetweenObjects = Vector2.SignedAngle(Vector2.zero, planarTarget - planarPosition) * (p.x > transform.position.x ? 1 : -1);
    46.         Debug.Log("7");
    47.         Vector2 finalVelocity = Quaternion.AngleAxis(angleBetweenObjects, Vector2.up) * velocity;
    48.         Debug.Log("8");
    49.         // Here is the actual movement
    50.         rb.velocity = finalVelocity;
    51.         Debug.Log("9");
    52.     }
    53. }
    54.  
     
  2. Wolfgang385

    Wolfgang385

    Joined:
    Nov 11, 2022
    Posts:
    14
    I also used the Debug Logs and found that it's making it through all the steps okay, so nothing is wrong there either (idk why it would be but I checked just in case)
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,683
    It will always do all the steps, you have no flow control logic so your Debug.Log are pointless here.

    What you do need to do is learn how to debug your code and you can find a link here. Attach a debugger (Visual Studio, Rider etc), place a breakpoint and step through the code inspecting the values and/or use "Debug.Log()" to output useful values. You can then see if you're getting what you expect.
     
    dstears and spiney199 like this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,382
    Then guard it or don't do it.

    This line:

    blindly divides by a term that we KNOW can be zero... we KNOW that cosine crosses the zero line so you could never divide by this without first guarding the divisor.

    That's just math. We know these things. It's nothing to do with code.

    Everything about the code is irrelevant if you don't get the math solid.


    If you don't immediately grok where your bad numbers are, get your lines down to one computation per line.

    And don't print "7"... print the values that matter! Print the values in your computations. Reason about them. Are they correct? If not, get them correct. That's debugging.

    This is exactly what Melv suggested here:


    If you have more than one or two dots (.) in a single statement, you're just being mean to yourself.

    Putting lots of code on one line DOES NOT make it any faster. That's not how compiled code works.

    The longer your lines of code are, the harder they will be for you to understand them.

    How to break down hairy lines of code:

    http://plbm.com/?p=248

    Break it up, practice social distancing in your code, one thing per line please.

    "Programming is hard enough without making it harder for ourselves." - angrypenguin on Unity3D forums

    "Combining a bunch of stuff into one line always feels satisfying, but it's always a PITA to debug." - StarManta on the Unity3D forums





    And as Melv says, debug!!

    By debugging you can find out exactly what your program is doing so you can fix it.

    https://docs.unity3d.com/Manual/ManagedCodeDebugging.html

    Use the above techniques to get the information you need in order to reason about what the problem is.

    You can also use
    Debug.Log(...);
    statements to find out if any of your code is even running. Don't assume it is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.
     
    Last edited: May 15, 2024