- Home /
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.
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.
@mandeep- The edit button exists for a reason. Please don't post alterations to your question as answers!
i am really sorry syclamoth i will take care next time...
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!
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
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.
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
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.
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
Follow this Question
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