Hey everyone,
I’m writing a game destined for mobile, where the player has a third-person view of an airplane. They move the plane by gyroscope. In the Unity Editor it’s currently running at about 200 frames a second with about 9 draw calls.
There is nothing visible yet in my game, except the player’s airplane.
For the sake of my sanity later, I want “input” to be handled by one script and “movement” to be handled by another script. The idea being that I can swap out input scripts when porting platforms (say for an accelerometer script, or a virtual game controller, or whatever)
When the gyro is below a certain deadspot value, nothing happens. When it is above this deadspot value, I call another script to move the airplane.
fairly straight-forward - if the gyro’s attitude is beyond a certain point, call the “BankAirplane” function of AirplaneMovement.js.
function BankAirplane(attitude:float){
// banks the airplane and moves it in the correction direction.
// amount is the amount of the bank
//variables for reading the plane's current position in world space
var PositionX : float = transform.position.x;
var PositionY : float = transform.position.y;
var PositionZ : float = transform.position.z;
var DestX: float;
if (attitude < 0) SpeedMultiplierBank = SpeedMultiplierBank * -1;
DestX = PositionX + (SpeedMultiplierBank * Time.deltaTime);
if (DestX > PosXnegMax && DestX < PosXposMax){
transform.Translate(SpeedMultiplierBank * Time.deltaTime, PositionY, PositionZ, Space.World); //move the plane along the X access
//write code to rotate the plane
}
//transform.Rotate(0,0,attitude * Time.deltaTime); //this was my inital gyroscope test
if (SpeedMultiplierBank < 0) SpeedMultiplierBank = SpeedMultiplierBank * -1;
}
I haven’t written my ‘rotate the aircraft’ bit yet because I’m trying to make the “move across the screen” bit smooth.
I think your issue is with the transform.Translate function.
I’m not sure what SpeedMultiplierBank is, but you are also moving it on the other axis by its own position. If you don’t want it to move on the y and z axis, they should be set to zero, not the planes current position.
Ok so here is the question… do you have another place where you are controlling the movement of the plane? I see in the bankairplane function that you are using a transform.translate that is affecting the X only. It is going to conflict with anything else that is trying to translate the item. If you need to do this during the bank at the same time you are doing the base movement, then maybe consider putting the jet in a child object and do the bank on local translation.
I may be off, but in the video I saw it jump position while moving both on the x and y the most. I hope this gives you a different direction to search down!
Ok it did not take the first time… long story short, I see that you are calling transform.translate on the X in the banking function. Are you calling it in another loop? It may be fighting position between two different scripts.
If that is the case, I would make the jet a child object and do the banking translation on the localposition, while using the parent object for all screen movement. I hope this helps!
Wow - Awest your comment solved another n00b issue I was having - my player game object was occasionally flying off to the edge of the game universe if I didn’t have “resets” in my code.
The “cause” of this choppiness issue was that I had two transform.translate - one for Z and one for X - I’ve changed my logical so that I calculate the change in Z and the change in X and then call transform.translate only once per frame, and the problem is solved!
Here is the revised function if anyone needs it…
function RepositionAirplane (attitude: float, power: float) {
//these parameters are only used for 'direction' of input - there are
//tuning variables for adjusting speed.
if (LoopInProgress == false){
//variables for reading the plane's current position in world space
var PositionX : float = transform.position.x;
var PositionY : float = transform.position.y;
var PositionZ : float = transform.position.z;
var DestX: float; // our X destination
var DestZ: float; // our Z destination
var ApplyX : float; //how much X to apply
var ApplyZ : float; //how much Z to apply
// work out how much change in X we're going to apply for this frame
if (attitude == 0) {
ApplyX = 0;
DestX = PositionX;
}
else if (attitude < 0) {
ApplyX = SpeedMultiplierBank * Time.deltaTime * -1;
}
else if (attitude > 0){
ApplyX = SpeedMultiplierBank * Time.deltaTime;
}
// work out how much change in Y we're going to apply for this frame
if (power == 0) {
ApplyZ = 0;
DestZ = PositionZ;
}
else if (power < 0) {
ApplyZ = SpeedMultiplierPower * Time.deltaTime * -1;
}
else if (power > 0){
ApplyZ = SpeedMultiplierPower * Time.deltaTime;
}
//work out what our destination X & Y points are
DestX = PositionX + ApplyX;
DestZ = PositionZ + ApplyZ;
if (DestX < PosXnegMax || DestX > PosXposMax) ApplyX = 0; // if the destination is less than the maximum X boundary, we're not moving
if (DestZ < PosYnegMax || DestZ > PosYposMax) ApplyZ = 0; // if the destination is less than the maximum X boundary, we're not moving
transform.Translate(ApplyX, 0, ApplyZ, Space.World); //move the plane
}
}