How to double my character's movement speed whenever spacebar is pressed?
I am a Beginner to Unity and I am trying to code a very simple spaceship game for my first project and so far my ship moves great but I am hoping to make it have a boost effect whenever I push the space bar. I tired to create an if statement similar to one of the ones I found in a Unity tutorial, but nothing works when I push the space bar. My code that I am using for my character movement is below. The bit of code I am having trouble with is the if statement in the Void Update part. Anyone have any insights or pointers for me? Thanks in advance!
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
void FixedUpdate ()
{
var x = Input.GetAxis("Horizontal") * Time.deltaTime * 250.0f;
var z = Input.GetAxis("Vertical") * Time.deltaTime * 8.0f;
//Movement script
transform.Rotate(0, x, 0);
transform.Translate(0, 0, z);
}
void Update ()
{
if (Input.GetKey ("Space"))
{
var z = Input.GetAxis ("Vertical") * Time.deltaTime * 16.0f;
// Spacebar is supposed to double movement speed when down, creating a boost effect
}
else
{
//if spacebar is not pressed, speed should be regular.
var z = Input.GetAxis("Vertical") * Time.deltaTime * 8.0f;
}
}
void OnTriggerEnter(Collider other)
{
//This is my pickup logic
if (other.gameObject.CompareTag ("Pick Up"))
{
other.gameObject.SetActive (false);
}
}
}
Your answer
Follow this Question
Related Questions
How to double my character's movement speed whenever spacebar is pressed? 0 Answers
can't check if my NPC has a specific script attached 0 Answers
MoveTowards is curving for no reason 0 Answers
How to crouch a FPS Controller? 2 Answers
Character's jumping mechanism stuck into something invisible 1 Answer