I don't really like checking it with the velocity and other physics and anyway it doesn't seem to work, saving the position in Vector3 object in the end of the Update(current loop) and then comparing it with current position(in next loop) doesn't work as it holds the same numbers as current position. I've also tried using FixedUpdate instead of Update but it was no good either. I did try writing curr_pos = new Vector3(transform.position.x...)
and it got me the same numbers for last and current position aswell and i also tried comparing it to just current position without any variables
I also tried doing it as simple as using Input.GetKey("w")
or Input.GetKey(KeyCode.up)
public Vector3 prev_pos, curr_pos;
void Update(){
prev_pos = this.transform.position;
...
if(prev_pos != curr_pos){
bool moving = true;
}
else{
bool moving = false;
}
if(sprinting && !crouching && moving){
speed = sprint_speed;
}
curr_pos = prev_pos;
}
I have a character controller on my player and am moving it with this controller
CharacterController controller = GetComponent<CharacterController>();
if(controller.isGrounded) {
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if(Input.GetButton ("Jump")){
moveDirection.y = jumpSpeed;
}
}
CodePudding user response:
To solve the problem you have to correct your algorithm. At the bottom is a simpler example that calculates the location of the previous position at the end of each frame.
public Vector3 prev_pos;
void Update(){
var moving = prev_pos != transform.position;
if(moving)
{
// when moving..
}
prev_pos = transform.position;
}
CodePudding user response:
Okay so basically i've put my check if the character is moving after the part where i am moving my character in the code, don't really know why i didn't think of it earlier but also the answer of @KiynL does work