Hello,
I have a code that uses GetAxis and i want it to make it use GetKey (W,S,A,D).
The code is:
if(Mathf.Abs(Input.GetAxis("Vertical"))> 0.2f || Mathf.Abs(Input.GetAxis("Horizontal")) > 0.2f){
----------------------------------------and------------------------------------------
if (Input.GetAxis ("Vertical") != 0) {
ourDrone.AddRelativeForce (Vector3.forward * Input.GetAxis ("Vertical") * movementForwardSpeed);
tiltAmountForward = Mathf.SmoothDamp (tiltAmountForward, 20 * Input.GetAxis ("Vertical"), ref tiltVelocityForward, 0.1f);
}
--------------------------------and--------------------------------------------------
if (Mathf.Abs (Input.GetAxis ("Horizontal")) > 0.2f) {
ourDrone.AddRelativeForce (Vector3.right * Input.GetAxis ("Horizontal") * sideMovementAmount);
tiltAmountSideways = Mathf.SmoothDamp (tiltAmountSideways, -20 * Input.GetAxis ("Horizontal"), ref tiltAmountVelocity, 0.1f);
}
Thank you in advance.,
1 Answer
1
Try this (not tested)
private float vertical ;
private float horizontal ;
private KeyCode positiveVerticalKey = KeyCode.W ;
private KeyCode negativeVerticalKey = KeyCode.S ;
private KeyCode positiveHorizontalKey = KeyCode.D ;
private KeyCode negativeHorizontalKey = KeyCode.A ;
void Update()
{
// Forward
if( Input.GetKeyDown( positiveVerticalKey ) ) vertical = 1 ;
else if( Input.GetKeyUp( positiveVerticalKey ) ) vertical = Input.GetKey( negativeVerticalKey ) ? -1 : 0 ;
// Backward
if( Input.GetKeyDown( negativeVerticalKey ) ) vertical = -1 ;
else if( Input.GetKeyUp( negativeVerticalKey ) ) vertical = Input.GetKey( positiveVerticalKey ) ? 1 : 0 ;
// Right
if( Input.GetKeyDown( positiveHorizontalKey ) ) horizontal = -1 ;
else if( Input.GetKeyUp( positiveHorizontalKey ) ) horizontal = Input.GetKey( negativeHorizontalKey ) ? 1 : 0 ;
// Left
if( Input.GetKeyDown( negativeHorizontalKey ) ) horizontal = -1 ;
else if( Input.GetKeyUp( negativeHorizontalKey ) ) horizontal = Input.GetKey( positiveHorizontalKey ) ? 1 : 0 ;
if( Mathf.Abs(vertical)> 0.2f || Mathf.Abs(horizontal) > 0.2f )
{
if ( vertical != 0 )
{
ourDrone.AddRelativeForce (Vector3.forward * vertical * movementForwardSpeed);
tiltAmountForward = Mathf.SmoothDamp (tiltAmountForward, 20 * vertical, ref tiltVelocityForward, 0.1f);
}
if ( Mathf.Abs ( horizontal ) > 0.2f )
{
ourDrone.AddRelativeForce (Vector3.right * horizontal * sideMovementAmount);
tiltAmountSideways = Mathf.SmoothDamp (tiltAmountSideways, -20 * horizontal, ref tiltAmountVelocity, 0.1f);
}
}
}