【Unity】实现愤怒的小鸟的弹射起步效果-SpringJoint2D弹簧组件

关键组件:Spring Joint 2D弹簧组件

其中,Connected Right Body:弹簧根节点(带Rigidbody2D物体) Connected Anchor属性:设定连接弹簧锚点位置。其他属性可自行查阅官网说明。

将该组件挂载到小鸟身上,小鸟是一个Sprite Renderer图片,身上有Rigidbody2D和一个碰撞器,最后使用一个脚本实现弹射效果,如下所示,将该C#脚本挂到小鸟身上。

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

public class Test : MonoBehaviour
{

    public GameObject right;
    private SpringJoint2D sp;
    private Rigidbody2D rig2D;
    private bool isSelect = false;
    public GameObject rightPos;
    public GameObject leftPos;
    private LineRenderer rightLine;
    private LineRenderer leftLine;
    private GameObject mainCamera;
    private Vector3 velocity = Vector3.zero;
    private bool canMove = true;
    private void Awake()
    {
        //this.GetComponent<TestMyTrail>().StartTrails();//开启拖尾效果(注意是有时间限制的)
        rightLine = rightPos.GetComponent<LineRenderer>();
        leftLine = leftPos.GetComponent<LineRenderer>();
        this.sp = this.GetComponent<SpringJoint2D>();
        this.rig2D = this.GetComponent<Rigidbody2D>();
        this.sp.enabled = true;
        this.rig2D.bodyType = RigidbodyType2D.Static;//默认让小鸟停留不受任何力影响
        mainCamera = Camera.main.gameObject;
    }

    private void OnMouseDown()
    {
        if (canMove)
        {
            isSelect = true;
            sp.enabled = true;
            rig2D.bodyType = RigidbodyType2D.Dynamic;//小鸟受力影响,其中sp就是弹簧 收到弹簧影响的小鸟会弹到相应的位置
        }
    }
    private void OnMouseUp()
    {
        if (canMove)
        {
            isSelect = false;
            sp.enabled = false;//小鸟不受弹簧约束 而弹出去
            rightLine.enabled = false;
            leftLine.enabled = false;
            canMove = false;
        }
    }
    private void Update()
    {
        if (isSelect)
        {
            //约束小鸟的拖动范围
            Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition) + new Vector3(0, 0, 10);
            transform.position = mousePos;
            if (Vector3.Distance(mousePos, rightPos.transform.position) > 3)
            {
                Vector3 dt = this.transform.position - rightPos.transform.position;
                this.transform.position = rightPos.transform.position + dt.normalized * 3;
            }            
            //弹簧线模拟
            rightLine.SetPosition(0, rightPos.transform.position);
            rightLine.SetPosition(1, transform.position);
            leftLine.SetPosition(0, leftPos.transform.position);
            leftLine.SetPosition(1, transform.position);
        }
        if (!isSelect)
        {
            Vector3 targetPos = new Vector3(Mathf.Clamp(transform.position.x, 0, 100000), transform.position.y, mainCamera.transform.position.z);
            mainCamera.transform.position = Vector3.SmoothDamp(mainCamera.transform.position, targetPos, ref velocity, Time.deltaTime * 2);
        }
    }
}

其中leftPos和rightPos是二叉树枝弹簧杆的2个点,均带有Line Renderer组件渲染弹簧线的。

小鸟首先将2D刚体的bodyType设置为Static,即不受任何力影响,当鼠标在小鸟身上按下后会小鸟2D刚体的bodyType改为Dynamic,因此受到弹簧组件影响而回弹到一定程度 这个时候还是受到弹簧约束小鸟不会掉下去。

最后再松开鼠标后,将弹簧组件禁用,禁用后小鸟会因为不受弹簧约束而飞出去。

其中,小鸟身上的弹簧组件属性如下:

小鸟身上挂载的C#脚本需要指定的东西如下:

注意:Line Renderer组件的Materials要设置好材质球,没有设置材质球会显示不出弹簧线条颜色。

使用默认的Sprites-Default。

猜你喜欢

转载自blog.csdn.net/qq_39574690/article/details/89046443