- Home /
Throttle interpreted as nothing?
I'm currently working on my first solo project, which is a fairly rudimentary flying game. I've got the basic physics working now, so I've decided to start implementing some other necessary functions. One of these is the throttle. The throttle is intended to work like in Pilotwings 64, where if no button is pressed, the throttle is set to medium, then you can press the accelerator button to max the throttle out and the decelerator to nearly cut power to the engine completely.
if (Input.GetKeyDown("a")){
float throttle = HighThrottle;
}
if (Input.GetKeyDown("z")){
float throttle = LowThrottle;
}
else{
float throttle = MediumThrottle;
}
float AirDensity = 2000 - (transform.position.y / 3);
rb.AddRelativeForce(Vector3.forward * throttle * AirDensity);
The plane behaved fine when thrust was dictated by a static variable called "thrust", but when I implemented this new feature, the plane doesen't move at all. Any ideas?
Answer by Bunny83 · Mar 19, 2017 at 02:09 AM
Ok some basics about variable scope. When you have an opening curly bracket you always start a new scope which ends at the corresponding closing bracket. Every variable declared within that brackets do no longer exist once you leave that scope.
Just in case a declaration of a variable is where it is defined. You declare a variable by specifing a type followed by a name.
So in each of your code blocks you declare a local variable which only exists **within those brackets*.
What you should do, since you want to set the throttle in a one-time event, declare the variable at class scope (i.e. outside of the method you have that code in).
public class SomeClass : MonoBehaviour
{
float throttle; // declare it here.
void Update()
{
if (Input.GetKeyDown("a"))
{
throttle = HighThrottle;
}
if (Input.GetKeyDown("z"))
{
throttle = LowThrottle;
}
else
{
throttle = MediumThrottle;
}
float AirDensity = 2000 - (transform.position.y / 3);
rb.AddRelativeForce(Vector3.forward * throttle * AirDensity);
}
}
Thanks. Now the plane moves. But it still doesen't seem to react to when I press A or Z. Here's the full code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlanePhysics : $$anonymous$$onoBehaviour {
public float thrust;
public Rigidbody rb;
public float liftfactor;
float lift;
float AirDensity;
float speedPerSec;
Vector3 oldPosition;
public float $$anonymous$$chForce;
public float YawForce;
public float RollForce;
public Text Speed;
public Text Lift;
public float LowThrottle;
public float $$anonymous$$ediumThrottle;
public float HighThrottle;
float speed;
float throttle;
void Start() {
rb = GetComponent<Rigidbody>();
}
void FixedUpdate() {
speedPerSec = Vector3.Distance (oldPosition, transform.position) / Time.deltaTime;
speed = Vector3.Distance (oldPosition, transform.position);
oldPosition = transform.position;
Speed.text = speed.ToString ("0");
if (Input.Get$$anonymous$$eyDown("a"))
{
throttle = HighThrottle;
}
if (Input.Get$$anonymous$$eyDown("z"))
{
throttle = LowThrottle;
}
else
{
throttle = $$anonymous$$ediumThrottle;
}
float AirDensity = 2000 - (transform.position.y / 3);
rb.AddRelativeForce(Vector3.forward * throttle * AirDensity);
rb.AddRelativeForce(Vector3.up * liftfactor * speed);
float lift = (liftfactor * speed);
Lift.text = lift.ToString ("0");
float yaw = Input.GetAxis("Yaw");
rb.AddTorque(transform.up * YawForce * yaw);
float roll = Input.GetAxis("Roll");
rb.AddTorque(transform.forward * RollForce * roll);
float pitch = Input.GetAxis("$$anonymous$$ch");
rb.AddTorque(transform.right * $$anonymous$$chForce * pitch);
}
}
Never check $$anonymous$$ey Down events in FixedUpdate. FixedUpdate does not run every frame if your (visual) frame rate is high. Since a down event is only true for one frame it's very likely to miss them. Always use Update for XXXDown.
Your answer
Follow this Question
Related Questions
Composite body physics 2 Answers
How to rotate rocket using thrusters 1 Answer
2D 360 degress platformer example needed 0 Answers
Audio for colliding blocks 0 Answers