Unity3D【脚本】点击物体后屏幕放大 - (射线、坐标的转换、摄像机的移动和改变属性)

using UnityEngine;
using System.Collections;

public class DParaticle : MonoBehaviour {
	
	//摄像机
	public GameObject camera_1;
	Vector3 p;
	Quaternion r;
	Vector3 s;

	//目标物体
	public GameObject target1, target2, target3, target4, target5;
	bool target1Bool,target2Bool,target3Bool,target4Bool,target5Bool;

	void Start () {
		//保存摄像机初始位置
		p=camera_1.transform.position;
		r=camera_1.transform.rotation;
		s=camera_1.transform.localScale;
	}

	void Update () {
		if(Input.GetMouseButton(0)){
			Ray ray = camera_1.camera.ScreenPointToRay(Input.mousePosition);
			RaycastHit hit;
			if(Physics.Raycast(ray,out hit)){
				if(hit.collider.gameObject==target1){
					if (!target1Bool) {
						camera_1.transform.LookAt(target1.transform);
						camera_1.camera.orthographicSize=3.0f;
					}else{
						first ();
					}
					target1Bool =! target1Bool;
				}else if(hit.collider.gameObject==target2){
					if(!target2Bool){
						//camera_1.transform.LookAt(target2.transform);
						camera_1.camera.orthographicSize=3.0f;
					}else{
						first ();
					}
					target2Bool=!target2Bool;
				}else if(hit.collider.gameObject==target3){
					if(!target3Bool){
						camera_1.transform.LookAt(target3.transform);
						camera_1.camera.orthographicSize=3.0f;
					}else{
						first ();
					}
					target3Bool=!target3Bool;
				}else if(hit.collider.gameObject==target4){
					if(!target4Bool){
						camera_1.transform.LookAt(target4.transform);
						camera_1.camera.orthographicSize=3.0f;
					}else{
						first ();
					}
					target4Bool=!target4Bool;
				}else if(hit.collider.gameObject==target5){
					if(!target5Bool){
						camera_1.transform.LookAt(target5.transform);
						camera_1.camera.orthographicSize=3.0f;
					}else{
						first ();
					}
					target5Bool=!target5Bool;
				}
			}
		}
	}

	void first () {
		camera_1.transform.position=p;
		camera_1.transform.rotation=r;
		camera_1.transform.localScale=s;
		camera_1.camera.orthographicSize=13.1f;
	}
}


源代码是可以运行的,但我优化了下源代码,然后脚本没有测试过。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值