- Home /
[Solved]GUI button problem.
Hello.
I have a script which makes it so, that when you click on the GUILayout button the camera lerps from one position to another. The problem is, that for some reason it doesn't work. I have no errors at all.
using UnityEngine;
using System.Collections;
public class PlaGUI : MonoBehaviour {
private Vector3 DefPos;
public int fontSize;
private Camera Cam;
private float t;
private float MoveChange = 0f;
void Start () {
}
void OnMouseUp(){
MoveChange++;
}
void OnGUI(){
if(MoveChange == 1f){
GUI.skin.label.fontSize = GUI.skin.box.fontSize = GUI.skin.button.fontSize = fontSize;
DefPos = new Vector3(0,600,0);
GUILayout.BeginArea(new Rect(Screen.width /2 - 13, Screen.height/2 + transform.localScale.x, 26, 400));
if(GUILayout.Button("X",GUILayout.Height(26f))){
Cam = Camera.main;
t+=Time.deltaTime;
Quaternion Rot = Quaternion.Euler(90,0,0);
Cam.transform.position = Vector3.Lerp(Cam.transform.position, DefPos, t*1.5f);
Cam.transform.rotation = Quaternion.Lerp (Cam.transform.rotation, Rot, t*1.5f);
}
GUILayout.EndArea();
}
}
void Update () {
if(MoveChange == 2f){
MoveChange = 1f;
}
}
}
[Edit] My fault, another script was interrupting, now it's working.
Thanks in advice for any answears, and forgive me if I'm missing something obvious here.
-Xentarok
P.S: If you have any questions about write them down in the comments.
P.S.2: Also, is there a way to have GUI button be on the edge of GameObject, because
GUILayout.BeginArea(new Rect(Screen.width /2 - 13, Screen.height/2 + transform.localScale.x, 26, 400));
is unfortunately not working.
Answer by Sundar · Aug 09, 2013 at 07:54 PM
Try this
MoveChange += 0.1f;
Ok $$anonymous$$oveChange += 1.0f; Can you try without deltatime in lerp like Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, DefPos, 1.5f);
Camera.main.transform.rotation = Quaternion.Lerp (Camera.main.transform.rotation, Rot, 1.5f);
And also Check camera position after selecting button
Okay, I found out what is wrong. One of my other scripts is interrupting the camera movement, keeping it in the same place.
I'm fixing it now, but thanks for the answear anyway!
Your answer