简体   繁体   English

如何修改轨道脚本以在Unity3d中触摸的按钮上启动

[英]How to modify orbit script to start on button touched in Unity3d

I have the following orbit script working just fine, on a GameObject in Unity3d. 我的以下轨道脚本在Unity3d中的GameObject上运行正常。 I want to have the script run on the GameObject if a GUI.Button is touched on Android device. 如果要在Android设备上触摸GUI.Button,我想在GameObject上运行脚本。

Here is the original C# orbir script: 这是原始的C#Orbir脚本:

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 ok Please advise. 我可以编译了,请指教。 Thank you all in advace for your answers. 谢谢advace的所有人的回答。

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语句中,您要在1次迭代后禁用该块,导致几乎没有任何移动,请尝试注释掉禁用boolean以便它可以运行

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 在开始时重置模型上的x,y,z将开始的xyz作为矢量3

public Vector3 reset;

in Start() 在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() 然后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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM