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
1
Question by mandeep · Mar 12, 2012 at 11:53 AM · cameraguinavigationorbit

Control the scene camera using arrow gui buttons

Hi everyone

I'm attempting to control my camera arround my game object through keyboard arrows but i need to put arrow buttons on gui so anyone can control the same thing from gui also.

right now i have this script.

 //WASD to orbit, left Ctrl/Alt to zoom
 using UnityEngine;
 
 [AddComponentMenu("Camera-Control/Keyboard Orbit")]
 
 public class KeyboardOrbit : MonoBehaviour {
 public Transform target;
 public float distance = 20.0f;
 public float zoomSpd = 2.0f;
 
 public float xSpeed = 240.0f;
 public float ySpeed = 123.0f;
 
 public int yMinLimit = -723;
 public int yMaxLimit = 877;
 
 private float x = 0.0f;
 private float y = 0.0f;
 
 public void Start () {
     Vector3 angles = transform.eulerAngles;
     x = angles.y;
     y = angles.x;
 
     // Make the rigid body not change rotation
     if (rigidbody)
         rigidbody.freezeRotation = true;
     }
 }

 public void LateUpdate () {
     if (target) {
         x -= Input.GetAxis("Horizontal") * xSpeed * 0.02f;
         y += Input.GetAxis("Vertical") * ySpeed * 0.02f;
 
         y = ClampAngle(y, yMinLimit, yMaxLimit);
 
         distance -= Input.GetAxis("Fire1") zoomSpd 0.02f;
         distance += Input.GetAxis("Fire2") zoomSpd 0.02f;
 
         Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
         Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position;
 
         transform.rotation = rotation;
         transform.position = position;
     }
 }
 
 public static float ClampAngle (float angle, float min, float max) {
     if (angle < -360.0f)
     angle += 360.0f;
     if (angle > 360.0f)
     angle -= 360.0f;
     return Mathf.Clamp (angle, min, max);
 }

here camera orbit is controlled by keyboard.i need it to be controlled by arrow buttons on gui.

THANKS.

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 Kleptomaniac · Mar 12, 2012 at 12:05 PM 0
Share

Could you please format your code properly by highlighting all of it and pressing the '101010' button in the question toolbar? It's very hard to read otherwise.

avatar image syclamoth · Mar 12, 2012 at 12:58 PM 0
Share

@mandeep- The edit button exists for a reason. Please don't post alterations to your question as answers!

avatar image mandeep · Mar 12, 2012 at 03:45 PM 0
Share

i am really sorry syclamoth i will take care next time...

avatar image syclamoth · Mar 12, 2012 at 03:59 PM 0
Share

Augh! You did it again!

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by syclamoth · Mar 12, 2012 at 12:51 PM

First off, add some private variables for communicating between the GUI functions and the movement functions- call them something like GUIHorizontal and GUIVertical.

 private float GUIHorizontal = 0;
 private float GUIVertical = 0;

Then, wherever you have a call to 'Input.GetAxis(whatever)' replace that with the appropriate GUI axis (horizontal or vertical)- for example:

 x -= GUIHorizontal * xSpeed * 0.02f;

Now, make a GUI function that sets the horizontal and vertical numbers according to buttons you are currently holding down. Obviously I don't know where you need to place the buttons on your screen, so I'll just use GUILayout for the purposes of this tutorial.

 void OnGUI()
 {
     if(Event.current.type == EventType.Repaint)
     {
         GUIHorizontal = 0;
         GUIVertical = 0;
     }
     GUILayout.BeginVertical();
     if(GUILayout.RepeatButton("UP"))
     {
         GUIVertical = 1;
     }
     GUILayout.BeginHorizontal();
     if(GUILayout.RepeatButton("LEFT"))
     {
         GUIHorizontal = -1;
     }
     if(GUILayout.RepeatButton("RIGHT"))
     {
         GUIHorizontal = 1;
     }
     GUILayout.EndHorizontal();
     if(GUILayout.RepeatButton("DOWN"))
     {
         GUIVertical = -1;
     }
     GUILayout.EndVertical();
 }

This will move the camera around according to those four buttons, instead of the keyboard!

Comment
Add comment · Show 5 · 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 mandeep · Mar 12, 2012 at 03:54 PM 0
Share

hi @syclamoth

sorry i am here for the first time i dont know the process. but now i am learning i tries the above script and got back the error.

so i tried the script you gave me, it's give me following errors

-error CS1519: Unexpected symbol -=' in class, struct, or interface member declaration -error CS1519: Unexpected symbol *' in class, struct, or interface member declaration

-error CS8025: Parsing error

i know this error might be so simple but i don't have any primary knowledge of "C lang".

can u plz help me.

Thnax

avatar image syclamoth · Mar 12, 2012 at 04:08 PM 0
Share

A: This isn't C, it's C#. There's a big, big difference.

B: You just copied the bit under 'for example' into the top of your class, didn't you? Did you read the context of that part of the answer at all? You were supposed to replace the line

 x -= Input.GetAxis("Horizontal") * xSpeed * 0.02f;

in the original script!

You can't just put random lines of code in the wrong places and expect it to work. I can show you how to do things, but there's a certain amount of baseline knowledge that you need if I am to communicate with you.

avatar image mandeep · Mar 13, 2012 at 09:46 AM 0
Share

Hi Syclamoth

I used the script and got the solution, as u mentioned in your first Ans.

thank u very much.the script is working fine.

but the arrow keys on the keyboard have stopped working after applying the modified script how can i make it to work on both keyboard as well as on GUI.

here's the modified code..

 //WASD to orbit, left Ctrl/Alt to zoom
 using UnityEngine;
 using System.Collections;
 
 [AddComponent$$anonymous$$enu("Camera-Control/$$anonymous$$eyboard Orbit")]
 
 public class Guikeys : $$anonymous$$onoBehaviour {
     public Transform target;
     public float distance = 20.0f;
     public float zoomSpd = 2.0f;
 
     public float xSpeed = 240.0f;
     public float ySpeed = 123.0f;
 
     public int y$$anonymous$$inLimit = -723;
     public int y$$anonymous$$axLimit = 877;
 
     private float x = 0.0f;
     private float y = 0.0f;
     
     private float GUIHorizontal = 0;
     private float GUIVertical = 0;
 
     public void Start () {
         Vector3 angles = transform.eulerAngles;
         x = angles.y;
         y = angles.x;
 
         // $$anonymous$$ake the rigid body not change rotation
         if (rigidbody)
             rigidbody.freezeRotation = true;
     }
 
     
     
 void OnGUI()
 {
     if(Event.current.type == EventType.Repaint)
     {
         GUIHorizontal = 0;
         GUIVertical = 0;
     }
     GUILayout.BeginVertical();
     if(GUILayout.RepeatButton("UP"))
     {
         GUIVertical = 1;
     }
     GUILayout.BeginHorizontal();
     if(GUILayout.RepeatButton("LEFT"))
     {
         GUIHorizontal = -1;
     }
     if(GUILayout.RepeatButton("RIGHT"))
     {
         GUIHorizontal = 1;
     }
     GUILayout.EndHorizontal();
     if(GUILayout.RepeatButton("DOWN"))
     {
         GUIVertical = -1;
     }
     GUILayout.EndVertical();
 }
 
 
     public void LateUpdate () {
         if (target) {
             x -= GUIHorizontal * xSpeed * 0.02f;
             y += GUIVertical * ySpeed * 0.02f;
             
             y = ClampAngle(y, y$$anonymous$$inLimit, y$$anonymous$$axLimit);
             
         distance -= Input.GetAxis("Fire1") *zoomSpd* 0.02f;
             distance += Input.GetAxis("Fire2") *zoomSpd* 0.02f;
             
             Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
             Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position;
             
             transform.rotation = rotation;
             transform.position = position;
         }
     }
 
     public static float ClampAngle (float angle, float $$anonymous$$, float max) {
         if (angle < -360.0f)
             angle += 360.0f;
         if (angle > 360.0f)
             angle -= 360.0f;
         return $$anonymous$$athf.Clamp (angle, $$anonymous$$, max);
         
     
     }
 }




Thanks

avatar image syclamoth · Mar 13, 2012 at 09:57 AM 0
Share

You never told me you wanted it to work on both! Well, you have everything you need to be able to make it work- just be creative. I recommend using something like this to combine the inputs:

 x -= $$anonymous$$athf.Clamp(GUIHorizontal + Input.GetAxis("Horizontal"), -1, 1) * xSpeed * 0.02f;

This will combine the two, but not allow both to be used simultaneously.

avatar image mandeep · Mar 13, 2012 at 11:02 AM 0
Share

Hi,@Syclamoth

it's working it was very simple i merged both script and now i can use both key board and gui button..

---Final Script---

//WASD to orbit, left Ctrl/Alt to zoom using UnityEngine; using System.Collections;

[AddComponent$$anonymous$$enu("Camera-Control/$$anonymous$$eyboard Orbit")]

public class Guikeys : $$anonymous$$onoBehaviour { public Transform target; public float distance = 20.0f; public float zoomSpd = 2.0f;

 public float xSpeed = 240.0f;
 public float ySpeed = 123.0f;

 public int y$$anonymous$$inLimit = -723;
 public int y$$anonymous$$axLimit = 877;

 private float x = 0.0f;
 private float y = 0.0f;
 
 private float GUIHorizontal = 0;
 private float GUIVertical = 0;
 
 public Texture uptexture;
 public Texture lefttexture;
 public Texture righttexture;
 public Texture downtexture;
 
 public void Start () {
     Vector3 angles = transform.eulerAngles;
     x = angles.y;
     y = angles.x;

     // $$anonymous$$ake the rigid body not change rotation
     if (rigidbody)
         rigidbody.freezeRotation = true;
 }

 
 

void OnGUI() { if(Event.current.type == EventType.Repaint) { GUIHorizontal = 0; GUIVertical = 0; } GUILayout.BeginVertical(); if(GUI.RepeatButton(new Rect(70,600,30,30), uptexture)) { GUIVertical = 1; } GUILayout.BeginHorizontal(); if(GUI.RepeatButton(new Rect(35, 630, 30, 30), lefttexture)) { GUIHorizontal = -1; } if(GUI.RepeatButton(new Rect(103, 630, 30, 30), righttexture)) { GUIHorizontal = 1; } GUILayout.EndHorizontal(); if(GUI.RepeatButton(new Rect(70, 658, 30, 30), downtexture)) { GUIVertical = -1; } GUILayout.EndVertical(); }

 public void LateUpdate () {
     if (target) {
         
         
         x -= Input.GetAxis("Horizontal") * xSpeed * 0.02f;
         y += Input.GetAxis("Vertical") * ySpeed * 0.02f;
         
         
         x -= GUIHorizontal * xSpeed * 0.02f;
         y += GUIVertical * ySpeed * 0.02f;
         
         y = ClampAngle(y, y$$anonymous$$inLimit, y$$anonymous$$axLimit);
         
     distance -= Input.GetAxis("Fire1") *zoomSpd* 0.02f;
         distance += Input.GetAxis("Fire2") *zoomSpd* 0.02f;
         
         Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
         Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position;
         
         transform.rotation = rotation;
         transform.position = position;
     }
 }

 public static float ClampAngle (float angle, float $$anonymous$$, float max) {
     if (angle < -360.0f)
         angle += 360.0f;
     if (angle > 360.0f)
         angle -= 360.0f;
     return $$anonymous$$athf.Clamp (angle, $$anonymous$$, max);
     
 
 }

}

thanx a lot for your Support.

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

6 People are following this question.

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

Related Questions

Rotating object with GUI Elements present 2 Answers

picture in picture 3d. 0 Answers

Dirt On Camera (BF3 and Metro Last Light) 1 Answer

dynamic GUI Texture.. clipping and rotation HUD 1 Answer

GameObject GUI Script Drawing to a Specific Camera 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