• Unity3d物体模型(实现旋转缩放平移自动旋转)


    基本功能实现:物体通过鼠标左键上下移动,中间键缩放、右键旋转,30秒没操作,物体自动旋转


    实例代码:

    using UnityEngine;
    using System.Collections;
    
    
    public class Script_07_11 : MonoBehaviour 
    {
    
    	public Transform target;
    	
    	private int MouseWheelSensitivity = 50;
    	private int MouseZoomMin = 20;
    	private int MouseZoomMax = 112;
    	private float normalDistance = 500;
    	
    	private bool flag_Roable = true;//自动旋转标志
    	
    	private Vector3 normalized;
    	
    
    	private float xSpeed = 250.0f;
    	private float ySpeed = 120.0f;
    
    
    	private float x = 0.0f;
    	private float y = 0.0f;
    	
    
    	private System.DateTime  oldTime;
    	private System.DateTime  nowTime;
    
    	void Start () 
    	{
    		
    		oldTime = System.DateTime.Now;
    		//transform.LookAt(target);
    		
    		
    	}
    	
    	void Update ()
    	{
    		nowTime = System.DateTime .Now;
    		System.TimeSpan	 ts1 = new System.TimeSpan(oldTime.Ticks);
    		System.TimeSpan	 ts2 = new System.TimeSpan(nowTime.Ticks);
    		
    		System.TimeSpan ts =  ts2.Subtract(ts1).Duration();
    		
    		if(ts.Seconds>30 && !Input.anyKey)
    		{
    			flag_Roable = true;
    			oldTime = System.DateTime.Now;
    		}
    		
    		if(flag_Roable && Input.anyKey)
    		{
    			flag_Roable = false;
    		}
    		
    		if(flag_Roable)//自动旋转
    		{
    			
    			x-=Time.deltaTime*30;
    			
    			
    			var rotation = Quaternion.Euler(0,x,0);
    			transform.RotateAround(target.position,Vector3.up,0.3f);
    		}
    		else
    		{
    			if(Input.GetMouseButton(1))
    			{
    				if(Input.GetAxis("Mouse X")<0)   
    					transform.RotateAround(target.position,Vector3.down,4); 
    				if(Input.GetAxis("Mouse X")>0)
    					transform.RotateAround(target.position,Vector3.up,4);
    			}
    			else if (Input.GetAxis("Mouse ScrollWheel") != 0)
    			{
    				
    			    //normalized = (transform.position - CameraTarget).normalized;
    				
    				if (normalDistance >= MouseZoomMin && normalDistance <= MouseZoomMax)
    				{
    					normalDistance -= Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity;
    				}
    				if (normalDistance < MouseZoomMin)
    				{
    					normalDistance = MouseZoomMin;
    				}
    				if (normalDistance > MouseZoomMax)
    				{
    					normalDistance = MouseZoomMax;
    				}
    					//transform.position =	   normalized * normalDistance;
    				transform.camera.fieldOfView = normalDistance;
    				
    			}
    			else if(Input.GetMouseButton(0))
    			{
    				//print("wyz-==="+Input.GetAxis("Mouse Y"));
    				if(Input.GetAxis("Mouse Y")<0)  //down
    				{
    					Vector3 temp=Vector3.up * 60.0f * Time.deltaTime;
    					
    					print("wyz==up==="+transform.localPosition.y);
    					if(transform.localPosition.y>300)
    					{
    						temp=Vector3.up * 5.0f * Time.deltaTime;
    						transform.Translate(temp);
    					}
    					else
    					{
    						transform.Translate(temp);		
    					}
    				}
    						
    				if(Input.GetAxis("Mouse Y")>0)  //up 
    				{
    					print("wyz==donw==="+transform.localPosition.y);
    					Vector3 temp=Vector3.down * 60.0f * Time.deltaTime;
    				
    					
    					if(transform.localPosition.y<-300)
    					{
    						temp=Vector3.up * 5.0f * Time.deltaTime;
    						transform.Translate(temp);
    					}
    					else
    					{
    						transform.Translate(temp);				
    					}			
    				}
    			
    			}	
    			
    		}
    		
    	}
    	
    	
    }


  • 相关阅读:
    node起本地服务器以及实现代理,前端接口转发
    一键前端代理,一行命令开启nginx容器,代理前端页面
    go语言学习笔记
    patch需要数据格式前端算法,patch算法基础,两个对象对比取差异属性
    react-native中使用Echarts,自己使用WebView封装Echarts经验
    如何用js自己实现Animate运动函数
    vue中的表单异步校验方法封装
    Entity Framework6使用SQL Server Compact免安装部署
    WCF异常传播
    解决.net的堆碎片化带来的内存占用过大的问题
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3221786.html
Copyright © 2020-2023  润新知