在unity中对物体进行放大缩小,绕X,Y,Z轴进行旋转,左右上下平移,显示隐藏

以下代码实现对物体的一些列操作:


using UnityEngine;


public class sanweixuanzhuandx : MonoBehaviour
{


   




    private float positionX;
    private float positionY;
    private float positionZ;
    private float scalePosiontX = 0.1f;
    private float scalePosiontY = 0.1f;
    private float scalePosiontZ = 0.1f;




    Vector3 scale;
    float offset = 0.001f;
    float maxSize = 10.0f;
    float minSize = 0.001f;
    public float speed = 100f;
    // Use this for initialization  
    void Start()
    {
        scale = this.transform.localScale;


     
    }


    // Update is called once per frame  
    void Update()
    {
        if (Input.GetMouseButton(0))  //右键改变x,y,z位置
        {
            positionX = Input.GetAxis("Mouse X") * scalePosiontX;
            positionY = Input.GetAxis("Mouse Y") * scalePosiontY;
            positionZ = -Input.GetAxis("Mouse ScrollWheel") * scalePosiontZ;
            transform.position += new Vector3(positionX, positionY, positionZ);
        }




        //鼠标滚轮的效果  
        //Zoom out  
        if (Input.GetAxis("Mouse ScrollWheel") < 0)
        {
            if (scale.x <= maxSize)
            {
                scale.x += offset;
                scale.y += offset;
                scale.z += offset;
                this.transform.localScale = scale;
            }


        }
        //Zoom in  
        if (Input.GetAxis("Mouse ScrollWheel") > 0)
        {
            if (scale.x > minSize)
            {
                scale.x -= offset;
                scale.y -= offset;
                scale.z -= offset;
                this.transform.localScale = scale;
            }
        }
        if (Input.GetKey(KeyCode.J))
        {
            // gameObject.active = false;
            transform.Rotate(new Vector3(0, 0, 1));
        }
        if (Input.GetKey(KeyCode.K))
        {
            // gameObject.active = false;
            transform.Rotate(new Vector3(1, 0, 0));
        }
        //鼠标左键旋转物体  
        /*if (Input.GetMouseButton(0))
        {
            float axis = Input.GetAxis("Mouse X");
            this.transform.Rotate(new Vector3(1, 0, 0) * Time.deltaTime * speed * axis);
        }*/
        /*if (Input.GetMouseButton(2))
       {
            float axis = Input.GetAxis("Mouse X");
            this.transform.Rotate(new Vector3(1, 0, 0) * Time.deltaTime * speed * axis);
        }*/
        if (Input.GetKey(KeyCode.I))
        {
            // gameObject.active = false;
            transform.Rotate(new Vector3(0, 1, 0));
            
            }
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值