unity 1 学习 物体旋转和通过脚本调用单击事件函数,find函数找物体的方法

1, 物体旋转

void Update () {

transform.Rotate (new Vector3 (0, 1, 0),400);
//transform.Rotate (new Vector3 (0, 1, 0));
//transform.RotateAround (new Vector3 (0, 0, 0)/*旋转的中心*/, new Vector3 (0, 1, 0), 2/*角度*/);
		
	}
2, main camera 上修改背景颜色

3, 屏幕单击的射线

using UnityEngine;
using System.Collections;

public class Rays : MonoBehaviour {

	private RaycastHit objhit;
	private Ray _ray;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {

		if (Input.GetMouseButtonDown(0)) {
			_ray = Camera.main.ScreenPointToRay (Input.mousePosition);
			if (Physics.Raycast(_ray, out objhit, 100)) {
				GameObject gm = objhit.collider.gameObject;
//				Debug.Log ("_rayhit");
//				Debug.Log (gm);
				gm.GetComponent<MeshRenderer>().material.color = Color.green;


			}
		}
	
	}
}

3 运行时单击某个物体,出现按钮,单击按钮物体变为设置的颜色。


using UnityEngine;
using System.Collections;

public class Rays : MonoBehaviour {

	private RaycastHit objhit;
	private Ray _ray;
	public GameObject colorPanle;


	// Use this for initialization
	void Start () {
		colorPanle.SetActive (false);//画布是否激活可见
	
	}
	
	// Update is called once per frame
	void Update () {

		if (Input.GetMouseButtonDown(0)) {
			_ray = Camera.main.ScreenPointToRay (Input.mousePosition);
			//射线从相机发出,经过屏幕上鼠标点击的位置
			Debug.DrawLine (_ray.origin, objhit.point, Color.blue, 2);
			//画出这条射线
			if (Physics.Raycast(_ray, out objhit, 100)) {
				//从射线出发最远100m的范围内
//				GameObject gm = objhit.collider.gameObject;
//				Debug.Log ("_rayhit");
//				Debug.Log (gm);
//				gm.GetComponent<MeshRenderer>().material.color = Color.green;
				colorPanle.SetActive (true);

			}
		}
	
	}

	public void GButtonDown()
	{
		GameObject obj = GameObject.Find("Sphere");
		obj.GetComponent<MeshRenderer> ().material.color = Color.green;
		colorPanle.SetActive (false);
	}
		
}

4,找到对象,然后获取下面的脚本,调用脚本里面的函数。

gaze = GameObject.Find ("Gaze").GetComponent<GameGaze>();

gaze.GetPos (light.gameObject, ref x, ref y);
GetPos函数在Gaze游戏对象下面的脚本 GameGaze里面。



你好Unity3D#Unity获取游戏对象详解


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值