- Home /
Flight Sim Control
Hi everyone, I'm trying to script basic flight controls for a flight sim I would like to make. I can't seem to get the object to move forward. I want the player to be able to control the speed using the keypad + and - keys. Making the speed increase or decrease and stop moving. This is the script I have, I'm not getting any errors but it just doesn't seem to work at all.
Any help would be greatly appreciate or even suggestions to make it better.
using UnityEngine;
using System.Collections;
public class PlayController : MonoBehaviour
{
//Variables for speed control
public float curSpeed;
public float maxSpeed = 1200;
public float minSpeed = 0;
void Start ()
{
CurSpeed ();
}
void FixedUpdate ()
{
if (Input.GetKeyDown (KeyCode.KeypadPlus))
{
rigidbody.AddForce (Vector3.forward * curSpeed);
}
if (Input.GetKeyDown (KeyCode.KeypadMinus))
{
rigidbody.AddForce (Vector3.forward * curSpeed);
}
transform.Rotate (Input.GetAxis("Vertical"), 0.0f,
-Input.GetAxis("Horizontal")); }
void CurSpeed()
{
float incSpeed = 5;
float decSpeed = 5;
if (curSpeed > 0 && curSpeed < maxSpeed)
{
curSpeed += incSpeed;
}
else if (curSpeed == maxSpeed)
{
curSpeed = maxSpeed;
}
if (curSpeed <= maxSpeed && curSpeed > minSpeed)
{
curSpeed -= decSpeed;
}
else if (curSpeed == minSpeed)
{
curSpeed = minSpeed;
}
}
}
Answer by getyour411 · Mar 30, 2014 at 08:21 PM
CurSpeed method is only called once in the lifetime of this class (at init); you probably meant to include a call to it in each of the if(keypress) bits.
Using GetKeyDown means the player has to taptaptap, if that's what you want then no changes needed, if you wanted a 'while holding button down' just cahnge that to GetKey
Nothing is happening, it's still staying in one spot and not moving forward at all.
Like what getyour411 said, you have to put the CurSpeed() function inside an update function, such as void Update().