Input Force is NaN NaN NaN

Found this script after some searching. It would seem to do what I need but can’t get past one NaN error:

Yes, I know it mean not a number, and mesh colliders are not involved (that was one other NaN solution I found), but the answer to this is a bit above my skill level right now, so here I am.

using UnityEngine;
using System.Collections;

public class BallisticLauncherTest : MonoBehaviour {

public GameObject ballGameObject;
public Transform target;

// Use this for initialization
void Start()
	ThrowBallAtTargetLocation(target.position, 10f);    

// Throws ball at location with regards to gravity (assuming no obstacles in path) and initialVelocity (how hard to throw the ball)
public void ThrowBallAtTargetLocation(Vector3 targetLocation, float initialVelocity)
	Vector3 direction = (targetLocation - transform.position).normalized;
	float distance = Vector3.Distance(targetLocation, transform.position);

	float firingElevationAngle = FiringElevationAngle(Physics.gravity.magnitude, distance, initialVelocity);
	Vector3 elevation = Quaternion.AngleAxis(firingElevationAngle, transform.right) * transform.up;
	float directionAngle = AngleBetweenAboutAxis(transform.forward, direction, transform.up);
	Vector3 velocity = Quaternion.AngleAxis(directionAngle, transform.up) * elevation * initialVelocity;

	// ballGameObject is object to be thrown
	ballGameObject.GetComponent<Rigidbody>().AddForce(velocity, ForceMode.VelocityChange);

// Helper method to find angle between two points (v1 & v2) with respect to axis n
public static float AngleBetweenAboutAxis(Vector3 v1, Vector3 v2, Vector3 n)
	return Mathf.Atan2(
		Vector3.Dot(n, Vector3.Cross(v1, v2)),
		Vector3.Dot(v1, v2)) * Mathf.Rad2Deg;

// Helper method to find angle of elevation (ballistic trajectory) required to reach distance with initialVelocity
// Does not take wind resistance into consideration.
private float FiringElevationAngle(float gravity, float distance, float initialVelocity)
	float angle = 0.5f * Mathf.Asin((gravity * distance) / (initialVelocity * initialVelocity)) * Mathf.Rad2Deg;
	return angle;


You have a NaN source somewhere, most likely in one of your helper functions.

I suspect “FiringElevationAngle” as you use “Mathf.Asin” without checking the parameter. Asin will return NaN if the parameter is outside the range [-1, 1].

NaN values are like cancer. If you execute any calculation that involves NaN the result will be NaN as well.

Try this instead of what you have at line 20:

Vector3 velocity = Quaternion.AngleAxis(directionAngle, transform.up).eulerAngles * elevation * initialVelocity;

Edit: This was wrong. Leaving it here because of the comments

In line 16, I replaced “float firingElevationAngle = FiringElevationAngle(Physics.gravity.magnitude, distance, initialVelocity);” with “float firingElevationAngle = 45”. Then It will shoot the ball out in a projectile.

But looks like the FiringElevationAngle line of function is itself missing. Do we have to compute this on our own according to the below wikipedia page…?