![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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; } } }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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圣典