- Home /
How to brake the player?
I'm making a 3D adventure game, and of course I'm searching A LOT just to make shure I'm doing it everything allright, but sometimes I can't find an answer for my threats with Unity. I'm doing everything as it should be, look:
private AnimationController animCtrl;
private SwitchSystem switchSys;
private Rigidbody rbody;
private CanWalk canWalk;
[SerializeField]private float speed = 5000f;
[SerializeField]private float angularSpeed;
[SerializeField]private float speedMultiplier;
[SerializeField]private float speedDivisor;
[SerializeField]private KeyCode runButton;
[SerializeField]private KeyCode crowtchButton;
private float inputV;
public bool invertVertical;
[SerializeField]private Transform camTransform;
private float moveZ;
void Start () {
}
void FixedUpdate () {
rbody = GetComponent<Rigidbody>();
canWalk = GetComponent<CanWalk>();
switchSys = GetComponent<SwitchSystem>();
animCtrl = GetComponent<AnimationController>();
if (canWalk.CanMove)
{
if (!invertVertical) {inputV = Input.GetAxis("Vertical");}
if (invertVertical) {inputV = -Input.GetAxis("Vertical");}
if (!Input.GetKey(runButton) && !Input.GetKey(crowtchButton))
{moveZ = inputV * speed * Time.fixedDeltaTime;}
if (Input.GetKey(runButton) && !Input.GetKey(crowtchButton))
{moveZ = inputV * speed * speedMultiplier * Time.fixedDeltaTime;}
if (Input.GetKey(crowtchButton) && !Input.GetKey(runButton))
{moveZ = inputV * speed / speedDivisor * Time.fixedDeltaTime;}
if (Input.GetKey(crowtchButton) && Input.GetKey(runButton))
{moveZ = inputV * speed * Time.fixedDeltaTime;}
//Forward - invert backward
if (inputV >= 0 && !switchSys.Switch_1_2)
{rbody.AddForce(camTransform.forward * moveZ * Time.fixedDeltaTime, ForceMode.Force);}
if (inputV >= 0 && switchSys.Switch_1_2)
{rbody.AddForce(transform.forward * moveZ * Time.fixedDeltaTime, ForceMode.Force);}
//Backward - invert forward
if (inputV <= 0 && !switchSys.Switch_1_2)
{rbody.AddForce(camTransform.forward * moveZ * Time.fixedDeltaTime, ForceMode.Force);}
if (inputV <= 0 && switchSys.Switch_1_2)
{rbody.AddForce(transform.forward * moveZ * Time.fixedDeltaTime, ForceMode.Force);}
}
}
But I can't stop my player sliding 'till some secs after I've released the walk key, I mean... Not without compromising all the walking system, be it with physics materials on the player, be it with physics magerials in the ground... I just can't! I need help! And that's for yesterday!
If the player is not affected by any outside forces, then just set his RigidBody's velocity to Vecto3.zero before applying any force due to movement keys being pressed.
I've found another trouble with my walking script. The player should walk towards the direction the camera looks, but he was not meant to fly... If I look up with my camera, the player follows this direction just as he follows the other camera facing directions. Ideas?
Your answer
Follow this Question
Related Questions
Non slippery movement 1 Answer
how to have air control after a rocket jump? 0 Answers
Player is Speeding up in collisions 0 Answers
Player touch input and continuous one direction movement combination 0 Answers
Why Won't it jump? 1 Answer