unity简单实现塔吊效果

前几天无意中看到一篇面试题 其中有个问题就是做一个简单的塔吊
然后我就根据自己现有的知识 做了一个简单的塔吊效果 在建筑游戏中可以加入这个demo
之后先给大家看一下效果 我做的比较粗糙 一方面没有合适的素材 第二就是没有吧数值弄的很精确
在这里插入图片描述

我实现了简单的左右旋转 上下旋转 力臂的延展和收缩
其中我用到了之前在博客介绍到的规则物体的简单的单向缩放地址

然后其中比较难理解的地方应该就是物体的勾取 和绳子的实现 还有就是物理效果
这些我分别使用了射线 划线组件 铰链关节组件来实现
在代码中也使用了很多关于获取组件的语句 希望大家可以看懂

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

public class DiaoJI : MonoBehaviour
{
    public Transform RotatePart;//可左右旋转的部分

    public Transform LengthPart;//改变长度的部分

    float scale;
    float speed = 0;
    bool islengthmove = true;

    public Transform UprotatePart;//上下旋转的部分

    public GameObject GouPart;//钩子
    public LineRenderer linerenderer;
    public GameObject TargetPosiObject;//以物体代替位置


    public GameObject Cameras;


    public void Start()
    {
        scale = LengthPart.localScale.z;
        linerenderer.enabled = false;
        
    }
    private void RightAndLeftRotate()
    {
        if (Input.GetKey(KeyCode.A))
        {
            RotatePart.Rotate(-Vector3.up * Time.deltaTime * 10f);
        }
        if (Input.GetKey(KeyCode.D))
        {
            RotatePart.Rotate(Vector3.up * Time.deltaTime * 10f);
        }

    }//旋转和上下高度的调整
    private void LengthUpdate()
    {
        if (Input.GetKey(KeyCode.W))
        {
          
            scale += 0.05f;
            speed = 2f;
        }
        else if (Input.GetKey(KeyCode.S))
        {
            
            scale -= 0.05f;
            speed = -2f;
        }
        else
        {
            speed = 0;
        }

        if (islengthmove)
        {
            LengthPart.Translate(transform.forward * Time.deltaTime * speed);
        }

        LengthPart.localScale = new Vector3(LengthPart.localScale.x, LengthPart.localScale.y, scale);
        if (LengthPart.localScale.z >= 11.3f)
        {
            islengthmove = false;
            LengthPart.localScale = new Vector3(LengthPart.localScale.x, LengthPart.localScale.y, 11.3f);
        }
        else if (LengthPart.localScale.z <= 1.7f)
        {
            islengthmove = false;
            LengthPart.localScale = new Vector3(LengthPart.localScale.x, LengthPart.localScale.y, 1.7f);
        }
        else
        {
            islengthmove = true;
        }
    }//长度的延伸

    private void UpandDownRotate()//还没有做区域限制
    {
        if (Input.GetKey(KeyCode.Q))
        {
            UprotatePart.Rotate(-Vector3.right * Time.deltaTime*2);
        }
        if (Input.GetKey(KeyCode.E))
        {
            UprotatePart.Rotate(Vector3.right * Time.deltaTime * 2);
        }
        if (UprotatePart.localRotation.x > 1)
        {
            Debug.LogError("过来");
            UprotatePart.localEulerAngles = new Vector3(5.58f,0,0);
        }
        if (UprotatePart.localRotation.x < -14.5f)
        {
            UprotatePart.localEulerAngles = new Vector3(-14.5f, 0, 0);
        }
    }

   private void ConGou()//控制钩子的方法
   {
        linerenderer.SetPosition(0, GouPart.transform.position);
        linerenderer.SetPosition(1, TargetPosiObject.transform.position);
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Ray ray = new Ray(GouPart.transform.position, Vector3.down);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit,1000))
            {
                if (hit.collider.tag == "Bg")
                {
                    TargetPosiObject = hit.collider.gameObject;

                    TargetPosiObject.transform.position = hit.point;

                    HingeJoint hin= GouPart.AddComponent<HingeJoint>();
                    hin.connectedBody = hit.collider.gameObject.GetComponent<Rigidbody>();


                    linerenderer.enabled = true;

                   
                }
            }
         }
        ///放手吧
        if (Input.GetKey(KeyCode.X ))
        {
            Destroy(GouPart.GetComponent<HingeJoint>());
            linerenderer.enabled = false;
        }

    }


    private void camerarotate() //摄像机围绕目标旋转操作
    {
        if (Input.GetKey(KeyCode.A))
        {
            Cameras.transform.RotateAround(transform.position, -Vector3.up, Time.deltaTime * 10f);
        }
        if (Input.GetKey(KeyCode.D))
        {
            Cameras.transform.RotateAround(transform.position, Vector3.up, Time.deltaTime * 10f);
        }
    }

    private void Update()
    {
        RightAndLeftRotate();//左右旋转力臂

        LengthUpdate();//力臂的长度的延伸

        UpandDownRotate();//力臂的上下的伸展

        ConGou();//钩子的控制方法

        camerarotate(); //摄像机围绕目标旋转操作
    }



}

代码其实不是很难 希望我所写的对大家有帮助
如果有问题可以联系我 主页有我的联系方式

猜你喜欢

转载自blog.csdn.net/weixin_44302602/article/details/108473039
今日推荐