Unity3D:c#脚本控制物件移动,材…

    xiaoxiao2021-03-25  174

    通过c#脚本来控制物件移动,并且改变物件颜色,被控体在碰撞物体后被碰撞的物体将会变蓝,如果物体已经是蓝色则变绿。 脚本1:Move.cs     将脚本赋值给摄像机达到控制目的 using UnityEngine; using System.Collections; public class Move : MonoBehaviour {     GameObject cube;     GameObject[] Cube;     // Use this for initialization     void Start()     {               cube = GameObject.Find("cn");//这里依照被控体的名称来修改         cube.GetComponent().material.color = Color.red;     }     // Update is called once per frame     void Update()     {         // 在每一帧中都实时检测         if (Input.GetKey(KeyCode.W))         {             cube.transform.Translate(-5 * Time.deltaTime, 0, 0, Space.Self);         }         if (Input.GetKey(KeyCode.S))         {             cube.transform.Translate(5 * Time.deltaTime, 0, 0, Space.Self);         }         if (Input.GetKey(KeyCode.A))         {             cube.transform.Translate(0, 0, -5 * Time.deltaTime, Space.Self);         }         if (Input.GetKey(KeyCode.D))         {             cube.transform.Translate(0, 0, 5 * Time.deltaTime, Space.Self);         }     } } 脚本2:CheckCollision.cs       依附被控体,在碰撞事件发生后修改被控体碰撞的物件属性 using UnityEngine; using System.Collections; public class CheckCollision : MonoBehaviour { // Use this for initialization void Start () {     } // Update is called once per frame void Update () { }     ///     /// 每次游戏对象碰撞时候都会执行这一方法     ///     /// 可以通过此获得碰撞游戏对象     void OnCollisionEnter(Collision co)     {                   if(co.gameObject.GetComponent().material.color == Color.blue)         {             co.gameObject.GetComponent().material.color = Color.green;         }         else             co.gameObject.GetComponent().material.color = Color.blue;     } }
    转载请注明原文地址: https://ju.6miu.com/read-632.html

    最新回复(0)