Unity面试编程题

32 篇文章 0 订阅
31 篇文章 0 订阅

1、实现吊机吊物体的功能

效果图


上面做了个横梁,上面一块Cube作为钩子,下面的Cube作为要吊起的重物,中间的绳子用Capsule

思路:鼠标左右滑动实现钩子的左右滑动,松开鼠标---钩子下滑,当钩子等撞到重物的时候停止下降 并带着重物一同上升,回到一定高度后,开始水平回到初始位置,并判断(是否勾住重物)如果勾住重物了 ,在原点位置,下降  然后放下重物,如果没勾住重物 则再远点不动,等待第二次操作。

在每次操作的过程中,不能进行第二次操作

结构:


上代码:

QizhongjiCS.cs控制钩子的滑动、钩子的下降、上升

using UnityEngine;  
using System.Collections;  
  
public class QizhongjiCS : MonoBehaviour {  
    private float dianX = 0;  
    public GameObject hook;  
    private float yuandianX;  
    private Vector3 hookV;  
    private int flag=0;  
    private delegate void HookMove();  
    private  HookMove _hookMove=null;  
    private int speed=3;  
  
    public HookZhongWu hzw;  
    private int zwFlag=0;  
    private GameObject zhongwu;                 //抓住的重物;  
  
    public GameObject shengzi;  
    public GameObject heng;  
    // Use this for initialization  
    void Start () {  
        hookV = hook.transform.localPosition;  
        yuandianX = hookV.x;  
    }  
      
    // Update is called once per frame  
    void Update () {  
        if (flag == 0) {  
            if (Input.GetMouseButtonDown(0))  
            {  
                dianX = Input.mousePosition.x;  
            }  
            else if (Input.GetMouseButton(0))  
            {  
                float dx = Input.mousePosition.x - dianX;  
                if (Mathf.Abs(dx) > 0.1f)  
                {  
  
                    hookV.x = yuandianX + dx / 32;  
                    if (hookV.x > -6 && hookV.x < 6)  
                    {  
                        hook.transform.localPosition = hookV;  
                    }  
  
                }  
  
  
            }  
            else if (Input.GetMouseButtonUp(0))  
            {  
                //yuandianX = hookV.x;  
                flag = 1;  
                _hookMove = hookDown;  
                StartCoroutine(downZhua());  
            }   
        }  
    }  
    IEnumerator downZhua() {  
        yield return new WaitForSeconds(0.01f);  
        //1、向下移动, +speed  
        //2、判断移动的位置 如果大于某个位置 ,返回 speed为负  
        if (_hookMove != null)  
        {  
            _hookMove();  
            yield return StartCoroutine(downZhua());  
        }  
        else {  
            yield return null;  
        }  
          
        
  
        //3、判断移动回到原点    整个钩子向原始位置水平运动  
  
        //4、判断钩子回到原点   停止协程  flag=0  
        yield return StartCoroutine(downZhua());  
    }  
    void hookDown() {  
        hook.transform.Translate(Vector3.down * Time.deltaTime * speed);  
        changeShengZi();  
        if (hook.transform.localPosition.y < -2) {  
            if (zwFlag == 1) {  
                zhongwu.transform.parent = null;  
                zhongwu = null;  
                zwFlag = 0;  
            }  
            _hookMove = hookUp;  
        }  
    }  
    void hookUp()  
    {  
        hook.transform.Translate(Vector3.up * Time.deltaTime * speed);  
        changeShengZi();  
        if (hook.transform.localPosition.y >3.2)  
        {  
             
                _hookMove = HMove;  
              
              
        }  
    }  
    void HMove()  
    {  
         
        hook.transform.Translate(Vector3.left * Time.deltaTime * speed);  
          
        if (hook.transform.localPosition.x <=-4.5)  
        {  
            if (zwFlag == 0)  
            {  
                flag = 0;  
                _hookMove = null;  
            }  
            else {  
                _hookMove = hookDown;  
            }  
              
        }  
    }  
    public void zhuaZhongWu(GameObject zhongwu) {  
        _hookMove = hookUp;  
        zwFlag = 1;  
        this.zhongwu = zhongwu;  
    }  
    void changeShengZi() {  
        Vector3 hookPosition = hook.transform.position;  
        Vector3 hengliangP = heng.transform.position;  
        float dy = hookPosition.y - hengliangP.y;  
        Vector3 shengziP = shengzi.transform.position;  
        shengziP.y = hengliangP.y + dy / 2;  
        shengzi.transform.position = shengziP;  
  
  
        //改变 绳子长度  
        Vector3 shengziSclae = shengzi.transform.localScale;  
        shengziSclae.y = dy/2;  
        shengzi.transform.localScale = shengziSclae;  
    }  
}  

----------------------------------------------

HookZhongWu.cs用来判断 钩子是否和重物碰撞

碰上的时候 将重物设置成钩子的子对象  就可以实现带着往上升的效果了

sing UnityEngine;  
using System.Collections;  
  
public class HookZhongWu : MonoBehaviour {  
    public QizhongjiCS qzj;  
    void OnTriggerEnter(Collider collision)  
    {//当碰撞时  
        print("OnTriggertEnter+" + collision.gameObject.name);  
        if (collision.gameObject.name == "zhongwu") {  
            collision.gameObject.transform.parent = this.gameObject.transform;  
            Vector3 v = collision.gameObject.transform.localPosition;  
            v.y = -1.2f;  
            collision.gameObject.transform.localPosition = v;  
            qzj.zhuaZhongWu(collision.gameObject);  
        }  
    }  
}  


2、写一个计时器工具,从整点开始计时,格式为:00:00:00

创建工程后添加一个Cube物体,为其添加一个脚本。

using UnityEngine;
using System.Collections;
public class Cube : MonoBehaviour {

    private float timer = 0f;
    private int h = 0;
    private int m = 0;
    private int s = 0;
    private string timeStr = string.Empty;

    // Update is called once per frame
    void Update () {
        timer += Time.deltaTime;
        if (timer >= 1f) {
            s++;
            timer = 0;
        }
        if (s >= 60) {
            m++;
            s = 0;
        }
        if (m >= 60) {
            h++;
            m = 0;
        }
        if (h >= 99) {
            h = 0;
        }
    }

    void OnGUI(){
        timeStr = string.Format ("{0:D2}:{1:D2}:{2:D2}", h, m, s);
        GUI.Label (new Rect (10, 10, 100, 200), timeStr);
    }
}


3、用鼠标实现在场景中拖动物体,用鼠标滚轮实现缩放

在场景中添加一个Plan,Camera,Directional Light,Cube。添加两个脚本scrollerScirpt(挂在Camera),CubeDragScript(挂在Cube上)。

1.鼠标滚轮实现缩放:

将摄像机的镜头拉近或者拉远,调整摄像机的视角就可以实现,主要实现代码如下

void Update () {
        //鼠标滚轮的效果
        //Camera.main.fieldOfView 摄像机的视野
        //Camera.main.orthographicSize 摄像机的正交投影
        //Zoom out
        if (Input.GetAxis("Mouse ScrollWheel") < 0)
        {
            if (Camera.main.fieldOfView <= 100)
                Camera.main.fieldOfView += 2;
            if (Camera.main.orthographicSize <= 20)
                Camera.main.orthographicSize += 0.5F;
        }
        //Zoom in
        if (Input.GetAxis("Mouse ScrollWheel") > 0)
        {
            if (Camera.main.fieldOfView > 2)
                Camera.main.fieldOfView -= 2;
            if (Camera.main.orthographicSize >= 1)
                Camera.main.orthographicSize -= 0.5F;
        }
}

2.鼠标实现在场景中拖动物体:

  解决思路就是将世界坐标转换成屏幕坐标,然后计算物体与鼠标之间移动量,循环鼠标被按下操作,得到鼠标的当前位置,加上计算好的移动量,将新的坐标赋值给物理就行了。主要是开启一个协同程序(Corountine)来处理

   主要代码如下:

void Start ()
    {
        StartCoroutine(OnMouseDown());
    }
 
    IEnumerator OnMouseDown()
    {
        //将物体由世界坐标系转换为屏幕坐标系
        Vector3 screenSpace = Camera.main.WorldToScreenPoint(transform.position);//三维物体坐标转屏幕坐标
        //完成两个步骤 1.由于鼠标的坐标系是2维,需要转换成3维的世界坐标系 
        //    //             2.只有3维坐标情况下才能来计算鼠标位置与物理的距离,offset即是距离
        //将鼠标屏幕坐标转为三维坐标,再算出物体位置与鼠标之间的距离
        Vector3 offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
        while (Input.GetMouseButton(0))
        {
            //得到现在鼠标的2维坐标系位置
            Vector3 curScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
            //将当前鼠标的2维位置转换成3维位置,再加上鼠标的移动量
            Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
            //curPosition就是物体应该的移动向量赋给transform的position属性
            transform.position = curPosition;
            yield return new WaitForFixedUpdate(); //这个很重要,循环执行
        }
    }


4、鼠标左键游戏对象使其旋转

UnityEngine;  
using System.Collections;  
public class DragRotateWithSlider : MonoBehaviour {  
      
    private bool onDrag = false;    //是否被拖拽  
    public float speed = 5f;    //旋转速度  
    private float tempSpeed;    //阻尼速度  
    private float axisX;    //鼠标沿水平方向移动的增量  
    private float axisY;    //鼠标沿垂直方向移动的增量  
    private float cXY;      //鼠标移动的距离  
  
    //接收鼠标按下的事件  
    void OnMouseDown ()   
    {  
        axisX = 0f; //为移动的增量赋初值  
        axisY = 0f;  
    }  
  
    //鼠标拖拽时的操作  
    void OnMouseDrag()  
    {  
        onDrag = true;  //被拖拽  
        axisX = Input.GetAxis("Mouse Y");   //获得鼠标增量  
        axisY = -Input.GetAxis("Mouse X");    
        cXY = Mathf.Sqrt(axisX * axisX + axisY * axisY);    //计算鼠标移动的长度  
        if (cXY == 0f)  
        {  
            cXY = 1f;  
        }  
    }  
  
    //Count TempSpeed  
    float Rigid()   //计算阻尼速度  
    {  
        if (onDrag)  
        {  
            tempSpeed = speed;  
        }  
        else  
        {  
            if (tempSpeed > 0)  
            {  
                tempSpeed -= speed * 2 * Time.deltaTime / cXY;//通过除以鼠标移动长度实现拖拽越长速度减缓越慢  
            }  
            else  
            {  
                tempSpeed = 0;  
            }  
        }  
        return tempSpeed;   //返回阻尼的值  
    }  
  
    void Update()  
    {  
        gameObject.transform.Rotate(new Vector3(axisX, axisY, 0) * Rigid(), Space.World);  
        if (!Input.GetMouseButton(0))  
        {  
            onDrag = false;  
        }  
    }  
}






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值