This question was
closed Jul 16, 2017 at 04:49 AM by
Walter_Hulsebos for the following reason:
Duplicate Question
Question by
Walter_Hulsebos · Oct 15, 2016 at 06:21 PM ·
float
My calculation won't give me floats.
using UnityEngine;
public class AiRaycast : MonoBehaviour
{
public GameObject player;
public int SpotRayCount = 12, SpotRayLenght = 10, SpotAngle = 90;
void Update ()
{
float rot = -((SpotAngle/ SpotRayCount) * (SpotRayCount/2));
print((SpotAngle / SpotRayCount));
print((SpotRayCount / 2));
for (int i = 1; i <= SpotRayCount; i++)
{
print(rot);
Debug.DrawRay(transform.position, Quaternion.AngleAxis(rot, transform.up) * transform.right * SpotRayLenght, Color.blue);
rot += (SpotAngle / SpotRayCount);
}
}
}
Comment
90/12 should equal 7.5 but it prints 7, why is this?
Best Answer
Answer by doublemax · Oct 15, 2016 at 06:29 PM
If there are only integers on the right side of the equal sign, the compiler will perform a division of integers.
You need to cast at least one INT to FLOAT to get a float divison.
float f = (float)SpotAngle / SpotRayCount;
Follow this Question
Related Questions
transform.position inconsistent numbers problem 1 Answer
Analytics - Funnel parameter (float) is always 0 1 Answer
How do I convert speed and heading to a vector? 3 Answers
Why doesnt the value change? 2 Answers
Trying to get doors to close after x time since the player has passed through the previous? 0 Answers