- Home /
The question is answered, right answer was accepted
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. Help?
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;
}
}
Answer by Bunny83 · Jul 13, 2017 at 02:00 PM
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.
Your "FiringElevationAngle" method need to have the ability to return "no angle possible" which is the case when the argument in Asin is outside it's valid range
private float FiringElevationAngle(float gravity, float distance, float initialVelocity)
{
float v = (gravity * distance) / (initialVelocity * initialVelocity);
if (v > 1f || v < -1f)
return -1f;
return 0.5f * $$anonymous$$athf.Asin(v) * $$anonymous$$athf.Rad2Deg;
}
You should check the result if it returns a negative result in which case you can't reach the target.
Yeah, way above my current skill level but THAN$$anonymous$$ YOU Bunny83, as it works now.
Answer by Jwizard93 · Jul 12, 2017 at 07:21 PM
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 fact in a few places you may want to do something like this. You shouldn't try to set a Vector3 equal to a Quaternion, ins$$anonymous$$d set the VEctor3 equal to the Quaternion.eulerAngles.
$$anonymous$$y mistake. You are multiplying Quaternions and vectors. This does actually return rotated vectors. $$anonymous$$y solution is likely not the right solution. Ins$$anonymous$$d have you tried outputting each variable involved with creating Vector3 velocity to see where NaN shows up? Better yet run the debugger it's easier.
That seems to bring up: error CS0019: Operator '*' cannot be applied to operands of type UnityEngine.Vector3' and
UnityEngine.Vector3'
Yes my solution was bad. Ignore it. But what about when you look at the chain of calculation for velocity vector? NaN should show up somewhere first and likely propagate all the way the way to velocity.
Answer by TTCreator · Jul 25, 2017 at 05:03 PM
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...?
I actually ended up going with a different formula that's much simpler (c#):
public GameObject ballObject; //Ball
private Transform target;
target = playerObject.transform; //I have this under Start
ballObject.GetComponent<Rigidbody> ().velocity = BallisticVel (target); //use this line to fire
Vector3 BallisticVel (Transform target) //the actual mathy part
{
var dir = target.position - transform.position; //get target direction
var h = dir.y; // get height difference
dir.y = 0; // retain only the horizontal direction
var dist = dir.magnitude; //get horizontal distance
dir.y = dist; //set elevation to 45 degrees
dist += h; //correct for different heights
var vel = $$anonymous$$athf.Sqrt(dist * Physics.gravity.magnitude);
return vel * dir.normalized; //returns Vector3 velocity
}
Follow this Question
Related Questions
Compiler Errors 0 Answers
Why do I keep getting this error? 2 Answers
Building on android. 1 Answer
Error with InvokeRepeating 1 Answer
NavMesh surface 1 Answer