- Home /
Slow Character Movement
Hey everyone,
I have been trying to make my character slow down when the right mouse button ("Fire2") is pressed, as this button allows the player to zoom and aim at an object. I have tried to add an IF Statement but still would not work, can anyone help me with this issue?
This is the player movement script I have wrote by following ETeeskiTutorials on YouTube.
Thanks a lot for the help guys, really appreciate it.
#pragma strict
//Movement Variables
var WalkAcceleration : float = 5;
var WalkDeacceleration : float = 5;
var MaxWalkingAcceleration : float = 20;
@HideInInspector //Hide next line from inspector
var WalkDeaccelerationVelocityX : float;
@HideInInspector //Hide next line from inspector
var WalkDeaccelerationVelocityZ : float;
@HideInInspector //Hide next line from inspector
var HorizontalMovement : Vector2;
var MainCamera : GameObject;
function Update ()
{
//Get and save the velocity of X and Z axis as rigidbody - MaxWalkingAcceleration
HorizontalMovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
//Magniture function returns the length of this vector - MaxWalkingAcceleration
if (HorizontalMovement.magnitude > MaxWalkingAcceleration)
{
HorizontalMovement = HorizontalMovement.normalized;
HorizontalMovement *= MaxWalkingAcceleration; //Set speed to MaxWalkingAcceleration
}
//Apple Horizontal moveemnt to rigidbody - MaxWalkingAcceleration
rigidbody.velocity.x = HorizontalMovement.x;
rigidbody.velocity.z = HorizontalMovement.y;
//De-accleration when Character is not moving - WalkDeacceleration
if (Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0)
{
//Decrease speed to WalkDeacceleration smoothly
rigidbody.velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0 , WalkDeaccelerationVelocityX, WalkDeacceleration);
rigidbody.velocity.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0 , WalkDeaccelerationVelocityZ, WalkDeacceleration);
}
//Rotation Y was inherited from MouseLook script - WalkAcceleration
transform.rotation = Quaternion.Euler(0, MainCamera.GetComponent(MouseLookScript).CurrentYRotation, 0);
//Horizontal Value was changed from "Input.GetAxis("Horizontal") * WalkAcceleration" to 0 - WalkAcceleration
rigidbody.AddRelativeForce(0, 0, Input.GetAxis("Vertical") * WalkAcceleration);
}
I am not even seeing a if Input.GetButtonDown("Fire2") in your script... $$anonymous$$ake sure you set up your buttons in the player settings too, or Unity will have no idea what Fire2 is.
Answer by Xtro · Jul 30, 2013 at 05:57 PM
1) Read the axis value into a variable at the beginning of the update method (Never call the same input method twice in an update call)
2) Use the variable in the code instead of GetAxis call.
3) After reading the axis into the variable, check the fire button and reset the variable.
It should be like that (C# code)
var Vertical = Input.GetAxis("Vertical");
if (Input.GetButtonDown("Fire2")) Vertical = 0;
//...rest of your code...
rigidbody.AddRelativeForce(0, 0, Vertical * WalkAcceleration);
Thanks for the advice, I'll play around with the script in a little while Xtro and make it function.
Your answer
Follow this Question
Related Questions
Aiming With my mouse 1 Answer
Problem with gun rotation after flipping character (2D) 1 Answer
Third Person Aiming 0 Answers
how to make tank aiming 1 Answer
Aim at touch (2D). 2 Answers