I have the following orbit script working just fine, on a GameObject in Unity3d. I want to have the script run on the GameObject if a GUI.Button is touched on Android device.
Here is the original C# orbir script:
using UnityEngine;
using System.Collections;
public class Orbiter : MonoBehaviour {
GameObject cube;
public Transform center;
public Vector3 axis = Vector3.up;
public Vector3 desiredPosition;
public float radius = 2.0f;
public float radiusSpeed = 0.5f;
public float rotationSpeed = 80.0f;
void Start () {
cube = GameObject.FindWithTag("MarkerObject");
center = cube.transform;
transform.position = (transform.position - center.position).normalized * radius + center.position;
radius = 2.0f;
}
void Update () {
transform.RotateAround (center.position, axis, rotationSpeed * Time.deltaTime);
desiredPosition = (transform.position - center.position).normalized * radius + center.position;
transform.position = Vector3.MoveTowards(transform.position, desiredPosition, Time.deltaTime * radiusSpeed);
}
}
Here is the modified code that refuses to run, no matter what I've tried.
using UnityEngine;
using System.Collections;
public class Orbiter : MonoBehaviour {
private bool IsOrbited = false;
private bool MustOrbit = false;
GameObject cube;
public Transform center;
public Vector3 axis = Vector3.up;
public Vector3 desiredPosition;
public float radius = 2.0f;
public float radiusSpeed = 0.5f;
public float rotationSpeed = 80.0f;
public Texture btnTexture2;
void Start () {
cube = GameObject.FindWithTag("MarkerObject");
center = cube.transform;
transform.position = (transform.position - center.position).normalized * radius + center.position;
radius = 2.0f;
}
// Update is called once per frame
void Update () {
if (MustOrbit && !IsOrbited) {
//Rotate all models around X,Y,Z axe
if (cube != null)
transform.RotateAround (center.position, axis, rotationSpeed * Time.deltaTime);
desiredPosition = (transform.position - center.position).normalized * radius + center.position;
transform.position = Vector3.MoveTowards(transform.position, desiredPosition, Time.deltaTime * radiusSpeed);
IsOrbited = true;
MustOrbit = false;
}
}
void OnGUI() {
GUI.matrix = Matrix4x4.TRS(Vector2.zero, Quaternion.identity,new Vector3(Screen.width / 480.0f, Screen.height / 320.0f, 1));
if (!btnTexture2) {
Debug.LogError("Please assign a texture on the inspector");
return;
}
//GUI.color = new Color(0,0,0,0);
//GUI.backgroundColor = Color.clear;
if (GUI.Button(new Rect(10, 10, 120, 30), btnTexture2))
if (!IsOrbited) {
MustOrbit = true;
}
}
}
What have I done wrong in coding? I get compiled o.k. Please advise. Thank you all in advace for your answers.
inside your if statement you are disabling the block from running after 1 iteration resulting in barely any movement, try to comment out disabling the booleans so it can run
if (MustOrbit && !IsOrbited) {
//Rotate all models around X,Y,Z axe
if (cube != null)
transform.RotateAround (center.position, axis, rotationSpeed * Time.deltaTime);
desiredPosition = (transform.position - center.position).normalized * radius + center.position;
transform.position = Vector3.MoveTowards(transform.position, desiredPosition, Time.deltaTime * radiusSpeed);
// IsOrbited = true;
// MustOrbit = false;
}
to reset the x,y,z on your model in your start get the starting xyz as a vector3
public Vector3 reset;
in Start()
void Start () {
cube = GameObject.FindWithTag("MarkerObject");
center = cube.transform;
transform.position = (transform.position - center.position).normalized * radius + center.position;
radius = 2.0f;
reset = transform.position;
}
then onGUI()
void OnGUI() {
GUI.matrix = Matrix4x4.TRS(Vector2.zero, Quaternion.identity,new Vector3(Screen.width / 480.0f, Screen.height / 320.0f, 1));
if (!btnTexture2) {
Debug.LogError("Please assign a texture on the inspector");
return;
}
//GUI.color = new Color(0,0,0,0);
//GUI.backgroundColor = Color.clear;
if (GUI.Button(new Rect(10, 10, 120, 30), btnTexture2))
if (!IsOrbited) {
MustOrbit = true;
}
}
if(GUI.Button(new Rect(Screen.width*5/6,Screen.height*5/6,Screen.width/6,Screen.height/6),"reset")){
transform.position = reset;
}