当前位置:   article > 正文

Unity面试上机题之鼠标拖动物体和滚轮缩放_unity 上机实操题

unity 上机实操题

创建DragCube脚本挂载在Cube上
代码如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class DragCube: MonoBehaviour
{
    private Camera cam;//发射射线的摄像机
    private GameObject go;//射线碰撞的物体
    public static string btnName;//射线碰撞物体的名字
    private Vector3 screenSpace;
    private Vector3 offset;
    private bool isDrage = false;
    
    Vector3 scale;
    void Start()
    {
        cam = Camera.main;
        scale = gameObject.transform.localScale;
    }

    void Update()
    {
        //整体初始位置 
        Ray ray = cam.ScreenPointToRay(Input.mousePosition);
        //从摄像机发出到点击坐标的射线
        RaycastHit hitInfo;


        if (isDrage == false)
        {
            if (Physics.Raycast(ray, out hitInfo))
            {
                //划出射线,只有在scene视图中才能看到
                Debug.DrawLine(ray.origin, hitInfo.point);
                go = hitInfo.collider.gameObject;
                //print(btnName);
                screenSpace = cam.WorldToScreenPoint(go.transform.position);
                offset = go.transform.position - cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
                //物体的名字  
                btnName = go.name;
                //组件的名字


            }
            else
            {
                btnName = null;
            }


        }


        if (Input.GetMouseButton(0))
        {
            Vector3 currentScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
            Vector3 currentPosition = cam.ScreenToWorldPoint(currentScreenSpace) + offset;


            if (btnName != null)
            {
                go.transform.position = currentPosition;
            }



            isDrage = true;
        }
        else
        {
            isDrage = false;
        }
        if (Input.GetAxis("Mouse ScrollWheel") < 0)
        {
            //if (Camera.main.fieldOfView <= 100)
            //    Camera.main.fieldOfView += 2;
            
            if (scale.x<5)
            {
                scale.x += 0.2f;
                scale.y += 0.2f;
                scale.z += 0.2f;
                gameObject.transform.localScale = scale;
            }
        }
        //Zoom in
        if (Input.GetAxis("Mouse ScrollWheel") > 0)
        {
            //缩放摄像机视角fieldOfView值
            //if (Camera.main.fieldOfView > 40)
            //    Camera.main.fieldOfView -= 2;
            //缩放物体
            if (scale.x > 0)
            {
                scale.x -= 0.2f;
                scale.y -= 0.2f;
                scale.z -= 0.2f;
                gameObject.transform.localScale = scale;
            }

        }

    }

    




}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号