- Home /
[c#] A little script help plz ... Cannot implicitly convert type `double' to `float'
Not quite sure why I'm getting "Cannot implicitly convert type double' to
float'" errors for the lines marked ...
Any ideas?
public class myProjectile : MonoBehaviour {
Collider bullet1; // Collider passed in
float rads1;
float ForceX1;
float ForceY1;
void FixedUpdate (){
if (theTouch.phase == TouchPhase.Ended){
rads1 = Math.Atan2( targetItem.transform.position.y - bullet1.transform.position.y,
targetItem.transform.position.x - bullet1.transform.position.x); /// <- error
ForceX1 = Math.Cos(rads1)*1000.0f; /// <- error
ForceY1 = Math.Sin(rads1)*1000.0f; /// <- error
bullet1.rigidbody.AddForce (ForceX1,ForceY1 , 0.0f);
}
}
}
Answer by Eric5h5 · Dec 18, 2011 at 10:01 AM
System.Math functions return doubles, not floats. Use Unity's Mathf functions instead, since they return floats. If you use System.Math, then you have to cast the double to float.
By the way, that code shouldn't be using touch phase input in FixedUpdate. TouchPhase.Ended might not occur during the frame in which FixedUpdate fires, in which case you'll miss the event.
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Initialising List array for use in a custom Editor 1 Answer
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer
Flip over an object (smooth transition) 3 Answers