Making the speed of my box the same all the time.

I’m not sure why I can;t figure this out, as it seems to easy but I guess I’m thinking to hard on it.

if(currentWaypoint < waypoint.Length) {
			target20 = waypoint[currentWaypoint];
			RaycastHit hit;
			Vector3 pos1 = transform.position;
			pos1.y += 15;
			int layerMask = 1 << floorMask;
			if(Physics.Raycast(pos1/*Starting Point*/, -Vector3.up, out hit /*Direction of Ray*/, Mathf.Infinity/*Distance of ray*/, layerMask)) {
				ySaver = hit.point.y+yStarter;
			}
			target20.y = ySaver;
			Vector3 moveDirection = target20 - transform.position;
			Vector3 testDirection = moveDirection.normalized;	
			transform.Translate(testDirection);
			
			if(moveDirection.magnitude < .1) {
				currentWaypoint++;
				counterParts--;
			}
		}

So right now when it goes from one waypoint to another it works perfectly. However the distance between the waypoints are affecting the speed of the movement. If a waypoint is farther away form another, it will speed up drastically compared to close waypoints. How would I go about solving this?

If your confused I can build a webplayer.

Here is a webplayer (simply hover over a blue/green box and left click drag to move it. The faster you move your mouse the faster the box moves in that area of the line - so how would I make it a constant speed throughout the whole thing.

Click Me

bump no ideas :\ I think I fell short of all mine

bump :\

I didn’t study your code but shouldn’t you have time.deltatime in there somewhere to smooth it out ?

Whenever I try the deltaTime or normalized it freezes up the cube and it can’t move.

I like the way the webplayer demo acts.
I don’t get the stuff you do with “y” components only. Maybe you should elaborate a bit.
Anyway you need transform.Translate(testDirection*Time.deltaTime); as you already have normalized in the previous line.
Also I think it has to do with the density of the waypoints and the scale of the scene and boxes.

As you are normalizing and not using the Time.deltaTime you are making the box go 1 unit distance per Update, which translates to (for 30
FPS) 30 unit distances per second. So if each box is 1 units long, if the distance between waypoints is long enough it goes at this full speed.
As we lay the waypoints with the mouse, the distance between waypoints is very close unless we move the mouse very quickly. Lets say the mean distance between each waypoint is 0.2 units. The box tries to move at 1 units/update however as it meets the waypoint it halts motion so it appears to be moving slow, or it possibly overshoots and gets past its target waypoint and the next waypoint. And at the next update gets back to the target, moving back and forth so fast, and leading to a erractic movement.

If I do Time.deltaTime all it does is make the cube move extremely slow, but still doesn’t solve the fact that it moves different speeds. If you move the mouse quicker it will still move the box quicker as well, so time.deltatime does not fix this at all.

There is something else happening here that we cannot see. Other than the normalization of the frame speed, the code that you have would not produce the results that you are getting.

In tests: If I drew a line slow, it would move slow, if I drew it fast, it would move fast. The only thing I can think of is the magnitude check at the end. If your move was always greater then the magnitude then you would never reach the destination. (This would look like jittering at a point) This also is not happening.

Your translation line actually should look like this:
transform.Translate(moveDirection.normalized * speed * Time.deltaTime);

Perhaps there is something else that is producing your problem.

Wow I luv you BigB. That code worked. I swear i tired it before too, and it was always glitching the box making it stay in the same spot.

I think I never used normalized and the time.deltatime at the same time, I always tried one or the other :frowning: