Unity 面试精选

Unity上机面试题

2018-11-29  本文已影响0人  Mad_Elliot
1、实现吊机吊物体的功能
个人见解: zhantie.gif
//挂在吊机的脚本
public class diaoji_ctrl : MonoBehaviour
{
    public float move_speed;
    public float gouzi_speed;
    GameObject shengZi;
    GameObject gouZi;

    void Start()
    {
        shengZi = GameObject.Find("shengzi");
        gouZi = GameObject.Find("gouzi");
    }

    void Update()
    {
        float h = Input.GetAxis("Horizontal") * move_speed * Time.deltaTime;
        float v = Input.GetAxis("Vertical") * move_speed * Time.deltaTime;
        transform.Translate(h, 0, v); //吊机的移动

        //限制上拉与下放的极限距离
        float dis = Vector3.Distance(gameObject.transform.position, gouZi.transform.position);
        if (Input.GetKey(KeyCode.J) && dis < 2.85f)
        {
            GouziMove(gouzi_speed);
        }

        if (Input.GetKey(KeyCode.K) && dis > 0.8f)
        {
            GouziMove(-gouzi_speed);
        }
    }

    void GouziMove(float speed)
    {
        shengZi.transform.localScale = shengZi.transform.localScale + new Vector3(0, speed, 0) * Time.deltaTime;
        shengZi.transform.Translate(0, -speed * Time.deltaTime, 0);
        gouZi.transform.Translate(0, -2 * speed * Time.deltaTime, 0);
    }
}
//挂在吊钩的脚本
public class gouzi_ctrl : MonoBehaviour {
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.L))
        {
            transform.GetChild(0).gameObject.GetComponent<Rigidbody>().isKinematic = false;//恢复
            transform.DetachChildren(); //解除自身所有子物体           
        }
    }

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "goods")
        {
            collision.gameObject.GetComponent<Rigidbody>().isKinematic = true;// 让物体仅受代码控制
            collision.gameObject.transform.SetParent(gameObject.transform);
        }
    }
}
2、写一个计时器工具,从整点开始计时,格式为:00:00:00
//挂在随便一个text物体上
//空格暂停与开始;小键盘0清零
public class jishiqi : MonoBehaviour {
    private float timer = 0f;
    private int h = 0;
    private int m = 0;
    private int s = 0;
    private string timeStr = string.Empty;
    Text time_text;
    bool pause = true;

    private void Start()
    {
        time_text = gameObject.GetComponent<Text>();
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            pause = !pause;
        }
        timer += Time.deltaTime;
        if (!pause)
        {
            if (timer >= 1f)
            {
                s++;
                timer = 0;
            }
            if (s >= 60)
            {
                m++;
                s = 0;
            }
            if (m >= 60)
            {
                h++;
                m = 0;
            }
            if (h >= 99)
            {
                h = 0;
            }
        }
        if (Input.GetKeyDown(KeyCode.Keypad0))
        {
            s = 0; m = 0; h = 0;
        }
        timeStr = string.Format("{0:D2}:{1:D2}:{2:D2}", h, m, s);
        time_text.text = timeStr;
    }
}
3、用鼠标实现在场景中拖动物体,用鼠标滚轮实现缩放
//鼠标拖拽,注:被拖拽物体不能与场景内其他物体在default layer上
public class dragobj : MonoBehaviour
{
    private void OnMouseEnter()
    {
        gameObject.transform.localScale += new Vector3(0.1f, 0.1f, 0.1f);
    }
    private void OnMouseExit()
    {
        gameObject.transform.localScale -= new Vector3(0.1f, 0.1f, 0.1f);
    }
    private void OnMouseDrag()
    {
        Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);//从屏幕鼠标点击的位置向场景内发出一条射线
        RaycastHit floorHit;
        if (Physics.Raycast(camRay, out floorHit, 1000f, 1))//1为过滤层
        {
            transform.position = new Vector3(floorHit.point.x, floorHit.point.y + 0.25f, floorHit.point.z);
        }
    }
}

//滚轮缩放
public class view_ctrl : MonoBehaviour
{
    public float wheelspeed;
    void Update()
    {
        transform.Translate(0, 0, Input.GetAxis("Mouse ScrollWheel") * wheelspeed * Time.deltaTime, Space.Self);
    }
}
4、鼠标左键游戏对象使其旋转
public class RollObj : MonoBehaviour
{
    public float roll_speed;
    void Update()
    {
        if (Input.GetMouseButton(0)) 
        {
            gameObject.transform.eulerAngles += new Vector3(0, -Input.GetAxis("Mouse X") * roll_speed * Time.deltaTime, 0);
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读