First time scripting really. Movement is from an example script and works just fine. Would like to get an input.getkeydown to boost the ball at a publicly variable speed IN THE SAME direction that the ball is traveling, as defined by getaxis. I can addforce with another vector3 but obviously doesn’t get the desired effect.
C#
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float speed;
public float boost;
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rigidbody.AddForce(movement * speed * Time.deltaTime);
if(Input.GetKeyDown(KeyCode.LeftShift))
{
rigidbody.AddForce(movement * boost * Time.deltaTime);
}
}
}
Also, would “invoke” be the best way to define a delay until the player can boost again?
That statement seems to contradict itself. The direction the ball is travelling is rigidbody.velocity, and getaxis defines the direction of the player input. Use whichever one you want, but it seems like the vector defined by the input axes would be more appropriate. I would use something like:
You also can’t poll Input.GetKeyDown() from FixedUpdate(). The value is refreshed every Update() so you will not reliably catch key presses. You can use Input.GetKey() safely inside FixedUpdate().
You could use invoke to flip a bool in a certain amount of time to reactivate boost. I don’t think that would be substantially better or worse than using a counter. Either should work.