当前位置:   article > 正文

Unity面试编程题_unity上机题

unity上机题

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

效果图


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

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

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

结构:


上代码:

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

  1. using UnityEngine;
  2. using System.Collections;
  3. public class QizhongjiCS : MonoBehaviour {
  4. private float dianX = 0;
  5. public GameObject hook;
  6. private float yuandianX;
  7. private Vector3 hookV;
  8. private int flag=0;
  9. private delegate void HookMove();
  10. private HookMove _hookMove=null;
  11. private int speed=3;
  12. public HookZhongWu hzw;
  13. private int zwFlag=0;
  14. private GameObject zhongwu; //抓住的重物;
  15. public GameObject shengzi;
  16. public GameObject heng;
  17. // Use this for initialization
  18. void Start () {
  19. hookV = hook.transform.localPosition;
  20. yuandianX = hookV.x;
  21. }
  22. // Update is called once per frame
  23. void Update () {
  24. if (flag == 0) {
  25. if (Input.GetMouseButtonDown(0))
  26. {
  27. dianX = Input.mousePosition.x;
  28. }
  29. else if (Input.GetMouseButton(0))
  30. {
  31. float dx = Input.mousePosition.x - dianX;
  32. if (Mathf.Abs(dx) > 0.1f)
  33. {
  34. hookV.x = yuandianX + dx / 32;
  35. if (hookV.x > -6 && hookV.x < 6)
  36. {
  37. hook.transform.localPosition = hookV;
  38. }
  39. }
  40. }
  41. else if (Input.GetMouseButtonUp(0))
  42. {
  43. //yuandianX = hookV.x;
  44. flag = 1;
  45. _hookMove = hookDown;
  46. StartCoroutine(downZhua());
  47. }
  48. }
  49. }
  50. IEnumerator downZhua() {
  51. yield return new WaitForSeconds(0.01f);
  52. //1、向下移动, +speed
  53. //2、判断移动的位置 如果大于某个位置 ,返回 speed为负
  54. if (_hookMove != null)
  55. {
  56. _hookMove();
  57. yield return StartCoroutine(downZhua());
  58. }
  59. else {
  60. yield return null;
  61. }
  62. //3、判断移动回到原点 整个钩子向原始位置水平运动
  63. //4、判断钩子回到原点 停止协程 flag=0
  64. yield return StartCoroutine(downZhua());
  65. }
  66. void hookDown() {
  67. hook.transform.Translate(Vector3.down * Time.deltaTime * speed);
  68. changeShengZi();
  69. if (hook.transform.localPosition.y < -2) {
  70. if (zwFlag == 1) {
  71. zhongwu.transform.parent = null;
  72. zhongwu = null;
  73. zwFlag = 0;
  74. }
  75. _hookMove = hookUp;
  76. }
  77. }
  78. void hookUp()
  79. {
  80. hook.transform.Translate(Vector3.up * Time.deltaTime * speed);
  81. changeShengZi();
  82. if (hook.transform.localPosition.y >3.2)
  83. {
  84. _hookMove = HMove;
  85. }
  86. }
  87. void HMove()
  88. {
  89. hook.transform.Translate(Vector3.left * Time.deltaTime * speed);
  90. if (hook.transform.localPosition.x <=-4.5)
  91. {
  92. if (zwFlag == 0)
  93. {
  94. flag = 0;
  95. _hookMove = null;
  96. }
  97. else {
  98. _hookMove = hookDown;
  99. }
  100. }
  101. }
  102. public void zhuaZhongWu(GameObject zhongwu) {
  103. _hookMove = hookUp;
  104. zwFlag = 1;
  105. this.zhongwu = zhongwu;
  106. }
  107. void changeShengZi() {
  108. Vector3 hookPosition = hook.transform.position;
  109. Vector3 hengliangP = heng.transform.position;
  110. float dy = hookPosition.y - hengliangP.y;
  111. Vector3 shengziP = shengzi.transform.position;
  112. shengziP.y = hengliangP.y + dy / 2;
  113. shengzi.transform.position = shengziP;
  114. //改变 绳子长度
  115. Vector3 shengziSclae = shengzi.transform.localScale;
  116. shengziSclae.y = dy/2;
  117. shengzi.transform.localScale = shengziSclae;
  118. }
  119. }

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

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

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

  1. sing UnityEngine;
  2. using System.Collections;
  3. public class HookZhongWu : MonoBehaviour {
  4. public QizhongjiCS qzj;
  5. void OnTriggerEnter(Collider collision)
  6. {//当碰撞时
  7. print("OnTriggertEnter+" + collision.gameObject.name);
  8. if (collision.gameObject.name == "zhongwu") {
  9. collision.gameObject.transform.parent = this.gameObject.transform;
  10. Vector3 v = collision.gameObject.transform.localPosition;
  11. v.y = -1.2f;
  12. collision.gameObject.transform.localPosition = v;
  13. qzj.zhuaZhongWu(collision.gameObject);
  14. }
  15. }
  16. }


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

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

  1. using UnityEngine;
  2. using System.Collections;
  3. public class Cube : MonoBehaviour {
  4. private float timer = 0f;
  5. private int h = 0;
  6. private int m = 0;
  7. private int s = 0;
  8. private string timeStr = string.Empty;
  9. // Update is called once per frame
  10. void Update () {
  11. timer += Time.deltaTime;
  12. if (timer >= 1f) {
  13. s++;
  14. timer = 0;
  15. }
  16. if (s >= 60) {
  17. m++;
  18. s = 0;
  19. }
  20. if (m >= 60) {
  21. h++;
  22. m = 0;
  23. }
  24. if (h >= 99) {
  25. h = 0;
  26. }
  27. }
  28. void OnGUI(){
  29. timeStr = string.Format ("{0:D2}:{1:D2}:{2:D2}", h, m, s);
  30. GUI.Label (new Rect (10, 10, 100, 200), timeStr);
  31. }
  32. }


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

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

1.鼠标滚轮实现缩放:

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

  1. void Update () {
  2. //鼠标滚轮的效果
  3. //Camera.main.fieldOfView 摄像机的视野
  4. //Camera.main.orthographicSize 摄像机的正交投影
  5. //Zoom out
  6. if (Input.GetAxis("Mouse ScrollWheel") < 0)
  7. {
  8. if (Camera.main.fieldOfView <= 100)
  9. Camera.main.fieldOfView += 2;
  10. if (Camera.main.orthographicSize <= 20)
  11. Camera.main.orthographicSize += 0.5F;
  12. }
  13. //Zoom in
  14. if (Input.GetAxis("Mouse ScrollWheel") > 0)
  15. {
  16. if (Camera.main.fieldOfView > 2)
  17. Camera.main.fieldOfView -= 2;
  18. if (Camera.main.orthographicSize >= 1)
  19. Camera.main.orthographicSize -= 0.5F;
  20. }
  21. }

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

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

   主要代码如下:

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


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

  1. UnityEngine;
  2. using System.Collections;
  3. public class DragRotateWithSlider : MonoBehaviour {
  4. private bool onDrag = false; //是否被拖拽
  5. public float speed = 5f; //旋转速度
  6. private float tempSpeed; //阻尼速度
  7. private float axisX; //鼠标沿水平方向移动的增量
  8. private float axisY; //鼠标沿垂直方向移动的增量
  9. private float cXY; //鼠标移动的距离
  10. //接收鼠标按下的事件
  11. void OnMouseDown ()
  12. {
  13. axisX = 0f; //为移动的增量赋初值
  14. axisY = 0f;
  15. }
  16. //鼠标拖拽时的操作
  17. void OnMouseDrag()
  18. {
  19. onDrag = true; //被拖拽
  20. axisX = Input.GetAxis("Mouse Y"); //获得鼠标增量
  21. axisY = -Input.GetAxis("Mouse X");
  22. cXY = Mathf.Sqrt(axisX * axisX + axisY * axisY); //计算鼠标移动的长度
  23. if (cXY == 0f)
  24. {
  25. cXY = 1f;
  26. }
  27. }
  28. //Count TempSpeed
  29. float Rigid() //计算阻尼速度
  30. {
  31. if (onDrag)
  32. {
  33. tempSpeed = speed;
  34. }
  35. else
  36. {
  37. if (tempSpeed > 0)
  38. {
  39. tempSpeed -= speed * 2 * Time.deltaTime / cXY;//通过除以鼠标移动长度实现拖拽越长速度减缓越慢
  40. }
  41. else
  42. {
  43. tempSpeed = 0;
  44. }
  45. }
  46. return tempSpeed; //返回阻尼的值
  47. }
  48. void Update()
  49. {
  50. gameObject.transform.Rotate(new Vector3(axisX, axisY, 0) * Rigid(), Space.World);
  51. if (!Input.GetMouseButton(0))
  52. {
  53. onDrag = false;
  54. }
  55. }
  56. }






声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/125035
推荐阅读
相关标签
  

闽ICP备14008679号