Unity12--鼠标按键的移动实现程序和Random随机数

来源:互联网 发布:网络建设之歌 编辑:程序博客网 时间:2024/05/23 01:58

一、创建个文件InputTest文件

    publicGameObject obj1;  -----最上面写的定义

    publicGameObject obj2;

    publicGameObject obj3;//5

1.void Update () {

      

       //1.GUI的左上角是(0,0,0)点,鼠标是左下角(000)点

        Debug.Log("鼠标的位置"+Input.mousePosition);

       

      //2.物体跟随鼠标移动--改成正焦摄像机 ,拖动物体就行,

        Vector3 position =Camera.main.ScreenToWorldPoint(Input.mousePosition);//屏幕转化成世界的,鼠标转化成position

       

      //3.通过按键

        if(Input.GetMouseButton(0))

        {

            Debug.Log("鼠标左键正在被按着");

        }

        if(Input.GetMouseButtonDown(0))

        {

            Debug.Log("鼠标左键按下只执行一次");

        }

        if(Input.GetMouseButtonUp(0))

        {

            Debug.Log("鼠标左键抬起只执行一次");

        }

 

        if (Input.GetMouseButton(1))

        {

            Debug.Log("鼠标右键正在被按着");

        }

        if (Input.GetMouseButtonDown(1))

        {

            Debug.Log("鼠标右键按下只执行一次");

        }

        if (Input.GetMouseButtonUp(1))

        {

            Debug.Log("鼠标右键抬起只执行一次");

        }

 


        if (Input.GetMouseButton(2))

        {

            Debug.Log("滚轮正在被按着");

        }

        if (Input.GetMouseButtonDown(2))

        {

            Debug.Log("滚轮按下只执行一次");

        }

        if (Input.GetMouseButtonUp(2))

        {

            Debug.Log("滚轮抬起只执行一次");

        }

 

        //Fire1是鼠标左键,记住里面的按钮

        if(Input.GetButton("Fire1"))//记住单词可直接对键操作,在unityEdit-Project Settings-Input里找到

        {

            Debug.Log("Fire1");

        }

 

     //4.获取坐标轴---通过这个也能实现对物体按键前后左右前进旋转

        float horizontal=Input.GetAxis("Horizontal");//获取水平轴,一个单词也不能写错//X

        float vertical =Input.GetAxis("Vertical");//获取垂直轴//相当于 Z

     //4.1这个

       // obj2.transform.Translate(new Vector3(0, 0,1)*vertical*Time .deltaTime );

      // obj2.transform.Rotate(newVector3(0, 1, 0) * horizontal * 30 * Time.deltaTime);//绕着Y 30度旋转

      

    //4.2下面两行代码是把上面的两行优化了一下而已,但第一个必须改成世界坐标系,要不就会按自己的轴旋转

   obj2.transform.Translate(obj2.transform.forward * vertical * 5*Time.deltaTime,Space.World);

    obj2.transform.Rotate(obj2.transform.up *horizontal * 30 * 5*Time.deltaTime);//后面*5可改速度

     

    //4.3优化好的--通过按上下左右键,头会跟着旋转,可坦克用

       Vector3 direction = horizontal *Vector3.right + vertical *Vector3.forward;//坐标数乘得到和向量

        obj2.transform.forward = direction;//把合力给了向前的方向,其实走的是合力

       obj2.transform.Translate(obj2.transform.forward *Mathf.Abs(vertical + horizontal) *Time.deltaTime,  

      Space.World);//Abs绝对值其实是速度,如果水平方向,就vertical0;都不走就没速度了

    

     //5物体跟着鼠标滑动来回转动--可调坦克炮管用

        obj3.transform.Rotate(obj3.transform.up*Input.GetAxis("Mouse X") * 30 *Time.deltaTime);  // 必须加空格X记住格式

         //5.1鼠标上下滑动,物体也上下动

       obj3.transform.Rotate(obj3.transform.right * -Input.GetAxis("Mouse Y") * 30 * Time.deltaTime);

        Debug.Log(Input.GetAxis("Mouse Y"));

二、Random(RandomTest文件下)

void Update () {

      //1.认识--    Debug.Log(Random.insideUnitCircle);//打印出(x,y)坐标,

      //2.

        GameObject obj =GameObject.CreatePrimitive(PrimitiveType.Sphere);

       // Vector2 position = Random.insideUnitCircle;

       // Vector3 position =Random.insideUnitSphere;//产生实心球


        Vector3 position =Random.onUnitSphere+newVector3 (5,5,5);//产生空心球

        obj.transform.position = position;     

         }



原创粉丝点击