Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kevinspawner · Sep 06, 2013 at 02:05 AM · guimovementplayer

GUI movement Control. Please Help!!! Thanks

 {
     public float speed = 0.00f;
     public float rotSpeed = 10.0f;
     public float strafeSpeed =10.0f;
     private float engineSpeed = 0.0f;
     public float pitchConstant = 1.0f;
     public float yawConstant = 0.0f;
     public GameObject boostersprefab;
     public GameObject boosterPos;
         
     private GameObject instantiated;
     private bool instanBoostersExist;        
     
     // GUI texture
     
     public GUITexture forwardPitchButton;
     public GUITexture backwardPitchButton;
     public GUITexture leftPitchButton;
     public GUITexture rightPitchButton;        
     
     
     
     
     // Booster Start Function (Instantiate the particles)
     void boostersStart()        
     {  
         if(instanBoostersExist == false)    
         
         {            
           instantiated = (GameObject)Instantiate(boostersprefab, boosterPos.transform.position, boosterPos.transform.rotation);
           instantiated.transform.parent    = boosterPos.transform;
           instanBoostersExist = true;            
         }        
     }        
     
     // Booster Stop Function (Stop Instantiating the particles)
     void boosterStop()
     {        
         if (instanBoostersExist == true)
         {        
             Destroy(instantiated,0.2f);
             instanBoostersExist = false;
         }    
     }
     
         
     
     void Update()
         
     {        
     // Axis Declaration    
     float pitch = Input.GetAxis("Pitch")*pitchConstant;         
     float yaw   = Input.GetAxis  ("Yaw")*yawConstant;        
     float roll =  Input.GetAxis ("Roll");
     float gas =   Input.GetAxis ("Gas");        
     
         
     Vector3 strafe = new Vector3 (Input.GetAxis("Horizontal")*strafeSpeed*Time.deltaTime,Input.GetAxis("Vertical")*strafeSpeed*Time.deltaTime,0);// Vertical and Horizontal Axis is feeded to the strafe movement.        
             
         // Engine Mechanism        
         
          if (engineSpeed <10 && engineSpeed > -3)
          {
           engineSpeed += gas;            
           boostersStart();          
          }
         
          if(engineSpeed > 10)
          {
           engineSpeed = (9.99f);    
          }
         
          if (engineSpeed<-3)
          {
           engineSpeed = (-2.99f);        
          }
         
           else if(Input.GetKey("z"))// Brake
          {
           engineSpeed = (0.00f);
           boosterStop();          
          }    
     
      
         
         
         
         
     // SpaceShip Physics Movement
         
     rigidbody.AddRelativeTorque(pitch*rotSpeed*Time.deltaTime,yaw*rotSpeed*Time.deltaTime,roll*rotSpeed*Time.deltaTime); // realative torque is added
     
     rigidbody.AddRelativeForce(0,0,engineSpeed*speed*Time.deltaTime); // relative force    
         
     rigidbody.AddRelativeForce(strafe);    // rigidbody added to the strafe movement
         
         
     }    
 }


How do I feed the movement function to GUI button?, I already created GUI texture and declared it int he program. I tried many way but couldn't get it working. i just want to move the ship when the GUI button is pressed instead of keyboard controls. Please help me out. Thanks a lot.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image getyour411 · Sep 06, 2013 at 02:42 AM 0
Share

if(GUI.Button (...this button...) move left

if(GUI.Button (...this button...) move right

etc.

avatar image kevinspawner · Sep 06, 2013 at 03:05 AM 0
Share

Thanks. $$anonymous$$uch appreciated. Do I need to create a gui function or how do i achieve this?

avatar image kevinspawner · Sep 06, 2013 at 03:07 AM 0
Share

Can you write for one direction. I can understand it better, Thanks a lot for your effort.

avatar image kevinspawner · Sep 06, 2013 at 03:20 AM 0
Share

Thanks. So I need to create a bool isBraking and set it to true on void OnGUI right?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by getyour411 · Sep 06, 2013 at 03:15 AM

Change this

  else if(Input.GetKey("z"))// Brake
 {
 engineSpeed = (0.00f);
 boosterStop();
 }

 

to something like

 if(isBraking) {
 engineSpeed = (0.00f);
 boosterStop();
 }

in your OnGUI you'll have button named 'Brake'

 if(GUI.Button,.....,) isBraking=true;
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kevinspawner · Sep 06, 2013 at 03:24 AM 0
Share

Sorry, Thanks for your reply. Am trying out now. I answered in the other question.

avatar image kevinspawner · Sep 06, 2013 at 06:32 AM 0
Share
     public GameObject boosterPos;
         
     private GameObject instantiated;
     private bool       instanBoostersExist;
     
     public bool isBraking = false;
     //public bool isGas     = false;
     
     // GUI textures
     
     public GUITexture forward$$anonymous$$chButton;
     public GUITexture backward$$anonymous$$chButton;
     public GUITexture left$$anonymous$$chButton;
     public GUITexture right$$anonymous$$chButton;    
     
     public Texture2D brakeButton = null;
     public Texture2D gasButton =null;
     
     
     
     void OnGUI()
     {
     
       if(GUI.Button(new Rect(Screen.width /2 +100, Screen.height /2 +100, brakeButton.width,brakeButton.height),brakeButton)) 
             
         {            
           isBraking=true;    
         }
         
      if(GUI.Button(new Rect(Screen.width /2 -63, Screen.height /2 -63, gasButton.width,gasButton.height),gasButton))    
         {
               
         }    
         
     }
     
     
     
     
     // Booster Start Function (Instantiate the particles)
     void boostersStart()        
     {  
         if(instanBoostersExist == false)    
         
         {            
           instantiated = (GameObject)Instantiate(boostersprefab, boosterPos.transform.position, boosterPos.transform.rotation);
           instantiated.transform.parent    = boosterPos.transform;
           instanBoostersExist = true;            
         }        
     }        
     
     
     // Booster Stop Function (Stop Instantiating the particles)
     void boosterStop()
     {        
         if (instanBoostersExist == true)
         {        
             Destroy(instantiated,0.2f);
             instanBoostersExist = false;
         }    
     }    
         
     
     void Update()
         
     {        
     // Axis Declaration    
     float pitch =   Input.GetAxis  ("$$anonymous$$ch")*pitchConstant;         
     float yaw   =   Input.GetAxis  ("Yaw")*yawConstant;        
     float roll  =   Input.GetAxis  ("Roll");
     float gas   =   Input.GetAxis  ("Gas");        
     
         
     Vector3 strafe = new Vector3 (Input.GetAxis("Horizontal")*strafeSpeed*Time.deltaTime,Input.GetAxis("Vertical")*strafeSpeed*Time.deltaTime,0);// Vertical and Horizontal Axis is feeded to the strafe movement.        
             
         // Engine $$anonymous$$echanism        
         
          if (engineSpeed <10 && engineSpeed > -3 )
          {
           engineSpeed += gas;            
           boostersStart();          
          }
         
          if(engineSpeed > 10)
          {
           engineSpeed = (9.99f);    
          }
         
          if (engineSpeed<-3)
          {
           engineSpeed = (-2.99f);        
          }
         
          if(isBraking)               // Brake
          {
           engineSpeed = (0.00f);
           boosterStop();          
          }     
         
         
         
         
     // SpaceShip Physics $$anonymous$$ovement
         
     rigidbody.AddRelativeTorque(pitch*rotSpeed*Time.deltaTime,yaw*rotSpeed*Time.deltaTime,roll*rotSpeed*Time.deltaTime); // realative torque is added
     
     rigidbody.AddRelativeForce(0,0,engineSpeed*speed*Time.deltaTime); // relative force    
         
     rigidbody.AddRelativeForce(strafe);    // rigidbody added to the strafe movement
         
         
     }    
 }

How do I make the gas button work, I tried the same boolean way like you said int he earlier comment, but it doesn't work.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why does my player want to fly? 1 Answer

Stopping an object immediately 1 Answer

How to Convert each 2D Array to GameObject 2 Answers

Taking a hit 3 Answers

Rotate game object and then return to its original rotation 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges