Unity3d C# 创建 物体

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;
        }
    }
    
}
View Code

 

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");
        }
        }
    }
}
View Code

 

 

查询脚本手册可以参考 unity3d圣典

posted on 2014-11-26 16:04  lovezj9012  阅读(566)  评论(0编辑  收藏  举报

导航