using UnityEngine; using System.Collections; public class create : MonoBehaviour { // Use this for initialization public GameObject newObject; void OnGUI() { if(GUI.Button(new Rect(10,10,100,50),"开始")) { //创建游戏物体 GameObject instance = (GameObject)Instantiate(newObject,transform.position,transform.rotation); //获取move脚本中的变量run GameObject.Find("3rd Person Controller").GetComponent<move>().run=true; } } }
using UnityEngine; using System.Collections; public class move : MonoBehaviour { public bool run=false; float speed =5.0f; // Update is called once per frame void Update () { if(run) { //获取sphere的transform Transform tf = GameObject.Find("Sphere").GetComponent<SphereCollider>().transform; Transform ren = GameObject.Find("3rd Person Controller").GetComponent<CharacterController>().transform; if(ren.position.x>=tf.position.x) { //控制物体移动 transform.Translate(Vector3.left,Camera.main.transform); //播放动画 animation.Play("run"); } } } }
查询脚本手册可以参考 unity3d圣典
时间: 2024-10-25 00:33:52