- Home /
move character controller forward using gui button.
i am using the following script to control the character controller camera rotation (up,down,left , right).
i want to move my character forward and backward using gui button.
my character controller is not moving in my camera direction.
following is the code. please check out the commented part of the script and let me know where and what is wrong in the script!!!
//WASD to orbit, left Ctrl/Alt to zoom
using UnityEngine;
using System.Collections;
[AddComponentMenu("Camera-Control/Keyboard Orbit")]
public class camerarottest: MonoBehaviour {
// public Transform target;
// public float distance = 20.0f;
private Vector3 moveDirection = Vector3.zero;
public float zoomSpd = 2.0f;
public float walkspeed = 3.0f;
public float xSpeed = 240.0f;
public float ySpeed = 123.0f;
public int yMinLimit = -723;
public int yMaxLimit = 877;
public static float x = 0.0f;
private float y = 0.0f;
private float GUIHorizontal = 0;
private float GUIVertical = 0;
public GUIStyle uptexture;
public GUIStyle lefttexture;
public GUIStyle righttexture;
public GUIStyle downtexture;
//public GUIStyle Zoomintexture;
//public GUIStyle Zoomouttexture;
//public GUIStyle reset;
public Texture2D camerabox;
public void Start () {
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
//print(angles);
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
void OnGUI()
{
if(Event.current.type == EventType.Repaint)
{
GUIHorizontal = 0;
GUIVertical = 0;
}
GUI.BeginGroup(new Rect(182,80, 800, 600));
GUI.DrawTexture(new Rect(700,430,78,79),camerabox);
GUILayout.BeginVertical();
if(GUI.RepeatButton(new Rect(710,430,56,29),"",uptexture))
{
GUIVertical = -1;
}
GUILayout.BeginHorizontal();
if(GUI.RepeatButton(new Rect(700,443, 29, 55),"", lefttexture))
{
GUIHorizontal = 1;
}
if(GUI.RepeatButton(new Rect(750,443, 30, 55),"", righttexture))
{
GUIHorizontal = -1;
}
GUILayout.EndHorizontal();
if(GUI.RepeatButton(new Rect(710,480, 56, 30),"", downtexture))
{
GUIVertical = 1;
}
GUILayout.EndVertical();
if(GUI.RepeatButton(new Rect(725,515, 27, 27),"",moveForward))
{
//this part is not giving any error but not working as expectation.
//i want to move the character controller camera forward when this button s clicked.
CharacterController controller = GetComponent<CharacterController>();
moveDirection = transform.forward;
moveDirection = transform.Translate (transform.forward * 2.0f * Time.deltaTime, Space.Self);
moveDirection = transform.TransformDirection(moveDirection);
controller.Move(moveDirection * Time.deltaTime);
/* CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded) {
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,0);
if (moveDirection != Vector3.zero) {
Vector3 rotation = transform.rotation;
rotation.SetLookRotation(moveDirection);
transform.rotation = rotation;
}
moveDirection = transform.TransformDirection(moveDirection);
// moveDirection *= speed;
}
controller.Move(moveDirection * Time.deltaTime);
*/
}
if(GUI.RepeatButton(new Rect(725,550, 27, 27),"",moveBackward))
{
CharacterController controller = GetComponent<CharacterController>();
moveDirection = -transform.forward;
moveDirection = transform.Translate (transform.forward * 2.0f * Time.deltaTime, Space.Self);
moveDirection = transform.TransformDirection(moveDirection);
controller.Move(moveDirection * Time.deltaTime);
}
GUI.EndGroup();
}
public void Update () {
x -= Input.GetAxis("Horizontal") * xSpeed * 0.02f;
y += Input.GetAxis("Verticalnew") * ySpeed * 0.02f;
x -= GUIHorizontal * xSpeed * 0.02f;
y += GUIVertical * ySpeed * 0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
transform.rotation = rotation;
}
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);
}
}
Solved by myself
var cameraObject : GameObject; var curSpeed : float = 3.0f;
change this:
controller.$$anonymous$$ove(forward Time.deltatime curSpeed); to:
controller.$$anonymous$$ove(cameraObject.transform.forward * curSpeed);
and assign the camera you want to use to the variable in the inspector :) hope this helps to whom who wants to move character controller using gui button :)
Answer by Hims · May 24, 2013 at 06:05 AM
Solved by myself
var cameraObject : GameObject; var curSpeed : float = 3.0f;
change this:
controller.Move(forward Time.deltatime curSpeed); to:
controller.Move(cameraObject.transform.forward * curSpeed);
and assign the camera you want to use to the variable in the inspector :) hope this helps to whom who wants to move character controller using gui button :)