- Home /
Question by
Chrishutchy · Jan 03, 2014 at 11:29 AM ·
inputbuttonsplanetouchscreen
How to add touchscreen controls to this script?
Hi, new to Unity. Need help with university project due in next week.
The game is a plane flying game and I want to add two onscreen touchscreen buttons for acceleration and braking (not too bothered about braking, more bothered about acceleration).
Any help would be greatly appreciated. Thanks
p.s I didn't write this script
var gameover=0;// Game Over || Turn on and off the airplane code. Becomes interesting at a later point. Game over ...
static var airplaneangley: float=0.0;// This goes to the blob shadow.
// Rotaton and position of our airplane
var rotationx=0;
var rotationy=0;
var rotationz=0;
var positionx: float=0.0;
var positiony: float=0.0;
var positionz: float=0.0;
var speed:float =0.0;//speed variable is the speed
var uplift:float =0.0;
function Update () {
//Shadowstuff-----------------------------------------------------------------------------------------------------------------------------------------
//shadow to the same angle than the airplane
airplaneangley= transform.eulerAngles.y;
//Maincode flying ----------------------------------------------------------------------------------------------------------------------------------
//Code is active when varwhatmoves is 1
if(gameover==0){
// Rotations of the airplane
if (speed>595)transform.Rotate(Input.GetAxis("Vertical")*Time.deltaTime*100,0,0); //Hoch Runter, limitiert auf eine Minimalgeschwindigkeit || Up Down, limited to a minimum speed
transform.Rotate(0,Input.GetAxis("Horizontal")*Time.deltaTime*100,0,Space.World); //Rechts Links || Left Right
if (groundtrigger.triggered==0) transform.Rotate(0,0,Input.GetAxis("Horizontal")*Time.deltaTime*50*-1); //Seitenneigung. Mal Minus 1 um in die richtige Richtung zu drehen || Tilt multiplied with minus 1 to go into the right direction
//limit tilt so that airplane doesn`t fly a roll while flying a curve
if ((Input.GetAxis ("Horizontal")<0)&&(rotationz >45)&&(rotationz <90)) transform.Rotate(0,0,Time.deltaTime*-50);//linksrum || to the left
if ((Input.GetAxis ("Horizontal")>0)&&(rotationz <315)&&(rotationz >270)) transform.Rotate(0,0,Time.deltaTime*50);//rechtsrum ||to the right
//Speed
transform.Translate(0,0,speed/20*Time.deltaTime);
//Turn variables to rotation and position of the object
rotationx=transform.eulerAngles.x;
rotationy=transform.eulerAngles.y;
rotationz=transform.eulerAngles.z;
positionx=transform.position.x;
positiony=transform.position.y;
positionz=transform.position.z;
//Rotate back in z axis , limited by no horizontal button pressed
if ((rotationz >0) && (rotationz < 90)&&(!Input.GetButton ("Horizontal"))) transform.Rotate(0,0,Time.deltaTime*-50);
if ((rotationz >0) && (rotationz > 270)&&(!Input.GetButton ("Horizontal"))) transform.Rotate(0,0,Time.deltaTime*50);
if ((rotationz >180) && (rotationz < 270)&&(!Input.GetButton ("Horizontal"))) transform.Rotate(0,0,Time.deltaTime*-50);
if ((rotationz <180) && (rotationz > 90)&&(!Input.GetButton ("Horizontal"))) transform.Rotate(0,0,Time.deltaTime*50);
//Rotate back X axis
if ((rotationx >0) && (rotationx < 180)&&(!Input.GetButton ("Vertical"))) transform.Rotate(Time.deltaTime*-50,0,0);
if ((rotationx >0) && (rotationx > 180)&&(!Input.GetButton ("Vertical"))) transform.Rotate(Time.deltaTime*50,0,0);
//Speed driving and flying ------------------------------------------------------------------------------------------
//
//We need a minimum speed limit in the air. We limit again with the groundtrigger.triggered variable
// Input Accellerate and deccellerate at ground
if ((groundtrigger.triggered==1)&&(Input.GetButton("Fire1"))&&(speed<800)) speed+=Time.deltaTime*240;
if ((groundtrigger.triggered==1)&&(Input.GetButton("Fire2"))&&(speed>0)) speed-=Time.deltaTime*240;
// Input Accellerate and deccellerate in the air
if ((groundtrigger.triggered==0)&&(Input.GetButton("Fire1"))&&(speed<800)) speed+=Time.deltaTime*240;
if ((groundtrigger.triggered==0)&&(Input.GetButton("Fire2"))&&(speed>600)) speed-=Time.deltaTime*240;
//Auftrieb -------------------------------------------------------------------------------------------------------------------------------------------------------
//When we don`t accellerate or deccellerate we want to go to a neutral speed in the air. With this speed it has to stay at a neutral height. Above this value the airplane has to climb, with a lower speed it has to sink. That way we are able to takeoff and land then.
// Neutral speed at 700
//This code resets the speed to 700 when there is no acceleration or deccelleration. Maximum 800, minimum 600
if((Input.GetButton("Fire1")==false)&&(Input.GetButton("Fire2")==false)&&(speed>595)&&(speed<700)) speed+=Time.deltaTime*240;
if((Input.GetButton("Fire1")==false)&&(Input.GetButton("Fire2")==false)&&(speed>595)&&(speed>700)) speed-=Time.deltaTime*240;
//uplift
transform.Translate(0,uplift*Time.deltaTime/10.0,0);
//Calculate uplift
uplift = -700+speed;
//We don`t want downlift. So when the uplift value lower zero we set it to 0
if ((groundtrigger.triggered==1)&&(uplift < 0)) uplift=0;
}
}
Comment
Your answer
Follow this Question
Related Questions
When pressing a button, it counts as pressing the screen and the button [New Input System] 0 Answers
C# Touch Script - Fix GameObject touch. 0 Answers
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
Using touchscreen in editor for development 0 Answers
How to execute action with two keys when they are alredy tied to other actions? 2 Answers