场景中创建N(两个以上)个物体,鼠标可以选中任何物体,当鼠标选中为非地面时,选中的物体变为红色,之前选中的物体恢复为之前的颜色,鼠标点击到地面时,让之前选中的那个物体移动的当前点击的位置

来源:互联网 发布:咫尺网络代理商 编辑:程序博客网 时间:2024/05/02 01:54
using UnityEngine;
using System.Collections;

public class MouseRayChangeColor : MonoBehaviour {

    private GameObject game;
    private Color cubecolor;
    private RaycastHit hit;
    private RaycastHit hitchangecolor;
    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () {
    
        //RaycastHit hit;

        if(Input.GetMouseButtonDown(0)){


            if(game!=null){//重置颜色
                Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition),out hitchangecolor);
                if(!hitchangecolor.collider.gameObject.name.Contains("Plane")){
                game.gameObject.GetComponent<MeshRenderer> ().material.color = cubecolor;
                }
            }



            Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition),out hit);
            if(!hit.collider.gameObject.name.Contains("Plane")){
             cubecolor = hit.collider.GetComponent<MeshRenderer> ().material.color;
            }

            if(!hit.collider.name.Contains("Plane")){
                hit.collider.GetComponent<MeshRenderer> ().material.color = Color.red;
                game = hit.collider.gameObject;

            }
            //hitchangeposition = hit;

//            if(hit.collider.name.Contains("Plane")){
//                Vector3 temp = Vector3.Lerp (game.transform.position,hit.point,Time.deltaTime);
//                game.transform.position = new Vector3 (temp.x,0.5f,temp.z);
//
//            }



        }



        if(hitchangecolor.collider){
            Vector3 temp = Vector3.Lerp (game.transform.position,hitchangecolor.point,Time.deltaTime);
            game.transform.position = new Vector3 (temp.x,0.5f,temp.z);
        }


    }








}
0 0