I almost hate to ask this because this is the most noobish problem I can possibly encounter, but it’s one that’s hitting me pretty hard. I’m trying to implement Valkyria Chronicles style movement, where each character has a float value that depletes as they walk. Right now, my incredibly sloppy test code (just to make sure the player only walks five meters) looks like this:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class MovementHandler : MonoBehaviour {
public Image movementSlider;
[Range(0.0f, 1.0f)]
public float movementDebug = 1f;
public int issuedCommands = 0;
public float movementPotential = 5f;
public float amountHasMoved = 0f;
public float playerSpeed = 1f;
private float internalMovementPotential;
private float internalIssuedCommands;
private bool movementEnded = false;
float movementInternal;
// Use this for initialization
void Start () {
internalMovementPotential = movementPotential;
}
void FixedUpdate () {
if (Input.GetKey ("w") && movementEnded == false) {
Vector3 newPosition = transform.position;
newPosition.z += (playerSpeed * Time.deltaTime);
transform.position = newPosition;
movementSlider.fillAmount = (internalMovementPotential -= Time.deltaTime) / movementPotential;
if (internalMovementPotential <= 0f) {
movementEnded = true;
Debug.Log ("stopped");
}
}
}
}
The movement potential variable is set to 1 and the speed is set to 5, so the player SHOULD move 5 meters, but instead they move 5.099998 - 5.099999. All I want to do is make it so the player stops after they’ve exhausted 5 meters of walk time. This should be possible, right? I’m not used to having to deal with such precise numbers.
Having looked at your code I think the problem is multiplying by Time.deltaTime. If the time between frames is variable sometimes you’re more than 5f. What if you tested newPosition += (playerSpeed * Time.deltaTime) to make sure it was not more than 5f.
Don’t use FixedUpdate, switch to Update. FixedUpdate is for physics only when dealing with rigidbodies. FixedUpdate can be called multiple times before Update ever does.
My character controller is going to be using rigidbodies to handle collisions so I need to have it in fixedupdate. That’s why my test area is based in it.
Oh no no no…I understand that but you are not using physics directly. You are moving the transform manually. If you where changing the velocity then I could understand or adding a force. But you are not, so that is why I suggested switching to Update.
Guh, sorry to have gotten snippy. It’s been a long week and it was very late at the time. This does seem to increase the accuracy a fair bit but it still seems to result in the player stopping a bit slowly. This is fine for distances of 5 metres or so, but increased to 15 or 20 and this can lead to nearly a metre of inaccuracy.
So that wasn’t giving me enough accuracy either, then I remembered that I did have some precision heavy code, it just wasn’t meant for things that move so I didn’t think to use it. I’m writing a CSG system that relies heavily on biases and thresholds to control how it handles nearby verts.
I’m not sure I understand you code. Why do you need the “or” (||) clause? Mathf.Abs(internalMovementPotential - 0f) <= 0.0001f
Anything less than zero would also be less than the above, right? Also why subtract 0f, it should do nothing.
I’m glad it’s working. Just wanted to point out something that might improve your code.
It’s mostly to handle things going very wrong on older machines. My target specs are super low since I do all my dev work on a Mac Mini and there are times where a decent amount of frameskipping can just cause everything to go pear shaped.
However, it occurs to me, on a clear head, that the Abs itself is kinda pointless when I can just use the 0.001f value in general. As for the -0? I have no idea where that came from. It’s in every instance of my CSG handling code yet it doesn’t seem to do anything.