作者:表发给上完善 | 来源:互联网 | 2023-08-18 16:43
最近忙着找Unity3D开发工程师的工作,看了一些面试题,看到这个,就写了一下,做个记录吧题目:实现吊机吊物体的功能没有详细说明怎么做,那么就写一个简单点的吧先附上效果图——上面做了个
最近忙着找Unity3D开发工程师的工作,看了一些面试题,看到这个,就写了一下,做个记录吧
题目:实现吊机吊物体的功能
没有详细说明怎么做,那么就写一个简单点的吧 先附上 效果图——
上面做了个横梁,上面一块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用来判断 钩子是否和重物碰撞
碰上的时候 将重物设置成钩子的子对象 就可以实现带着往上升的效果了
using 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);
}
}
}