how to decrease floats by 1/2

So i am a newbie at game development, so this may be an extremely easy question, but, how do i get the half value of a float. Say for instance i already have a float called distance, distance is equal to four, and the new float i want is going to be half of distance, and i want it to be called dishalf. Ive already tried : float dishalf = distance/2f; float dishalf = 1/2distance; and those didn’t work how i expected them to. The value of dishalf kept changing. The particular instance im using it in is for a lerp, but the value wasnt set in stone, therefore distance kept decreasing. id really appreciate it if anyone could help. There was a guy that closed my question, I don’t know why, but this is a legit question I have. Someone pls help. If i didn’t explain something correctly pls let me know.

float distance = 4;

float distHalf = distance / 2;

/ divide

  • multiply
  • subtract
  • add
    these are the basic rules. (1/2 distance = 1/(2*distance) by the way)
    If they don’t work as expected, you may wish to check you distance value. Expectantly if you are using them in a lerp, distance might be changing constantly, throwing off your expectations for what half of distance should be

@JxWolfe that really didnt help, it still kept decreasing.

By the way, I’ve read somewhere that division had a bigger cost than multiplication in terms of memory usage.
So it’s better to do distHalf = distance * .5f, especially in a loop such as an Update().

The problem could be with the way you’re using Lerp. I just read your comment. You’re multiplying time by Time.deltaTime as the third parameter of Vector3.Lerp (). Why? Don’t you want it to change steadily from one state to another?

Here’s a Vector3.Lerp example:

private float   timeFraction = 0;
public  Vector3 fromVector = new Vector3 (0, 0, 0);
public  Vector3 toVector = new Vector3 (1, 1, 1);
public  Vector3 lerpResult;

private void Update ()
{
	// Lerp
	lerpResult = Vector3.Lerp (fromVector, toVector, timeFraction);

	// Increase timeFraction
	timeFraction += Time.deltaTime;

	// Reset timeFraction
	if (timeFraction > 1)  timeFraction = 0;
}