Adapted Unity 2D script not working as intended

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)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class IDK : MonoBehaviour
{
    [SerializeField]
    private Transform target;

    [SerializeField]
    private float initialAngle;

    private Rigidbody2D rb;
    private Vector2 p;
    private float gravity = -9.8f;
    private float angle;

    private GameObject player;

    // Start is called before the first frame update
    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player");

        rb = GetComponent<Rigidbody2D>();
        p = target.position;
        angle = initialAngle * Mathf.Deg2Rad;

    }

    void JumpAttack()
    {
        Vector2 planarTarget = new Vector2 (p.x, 0);
        Debug.Log("1");
        Vector2 planarPosition = new Vector2 (transform.position.x, 0);
        Debug.Log("2");
        float yOffset = transform.position.y - p.y;
        Debug.Log("3");
        float distance = Vector2.Distance(planarTarget, planarPosition);
        Debug.Log("4");
        float initialVelocity = (1 / Mathf.Cos(angle)) * Mathf.Sqrt((0.5f * gravity * Mathf.Pow(distance, 2)) / (distance * Mathf.Tan(angle) + yOffset));
        Debug.Log("5");
        Vector2 velocity = new Vector2(initialVelocity * Mathf.Cos(angle), initialVelocity * Mathf.Sin(angle));
        Debug.Log("6");
        float angleBetweenObjects = Vector2.SignedAngle(Vector2.zero, planarTarget - planarPosition) * (p.x > transform.position.x ? 1 : -1);
        Debug.Log("7");
        Vector2 finalVelocity = Quaternion.AngleAxis(angleBetweenObjects, Vector2.up) * velocity;
        Debug.Log("8");
        // Here is the actual movement
        rb.velocity = finalVelocity;
        Debug.Log("9");
    }
}

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)

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.

2 Likes

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.

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.