• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

unity 3d 控制 SpringJoint2D 弹力过大的方法

武飞扬头像
taojiaheng
帮助1

Rigidbody2D 依然采用 Dynamic,在鼠标松开的一刻, 速率 乘以 一个 0-1 之间的浮点数 用来控制 弹力的大小

学新通

面板设置 

以下是代码

  1.  
    using System.Collections;
  2.  
    using System.Collections.Generic;
  3.  
    using UnityEngine;
  4.  
     
  5.  
    public class BirdsController : MonoBehaviour
  6.  
    {
  7.  
    [Header("旋转原点对象")]
  8.  
    public Transform trunOriginPoint;
  9.  
     
  10.  
    [Header("限定的最大距离")]
  11.  
    public float maxDistance;
  12.  
     
  13.  
    [Header("弹簧力")]
  14.  
    [Range(0f, 1f)]
  15.  
    public float springForce;
  16.  
     
  17.  
    /// <summary>
  18.  
    /// 鼠标是否按住小鸟
  19.  
    /// </summary>
  20.  
    private bool isEnter = false;
  21.  
     
  22.  
     
  23.  
    private SpringJoint2D spj;
  24.  
     
  25.  
    private Rigidbody2D rig;
  26.  
     
  27.  
    private void Awake()
  28.  
    {
  29.  
    spj = GetComponent<SpringJoint2D>();
  30.  
    rig = GetComponent<Rigidbody2D>();
  31.  
    }
  32.  
     
  33.  
    // Start is called before the first frame update
  34.  
    void Start()
  35.  
    {
  36.  
    }
  37.  
     
  38.  
    // Update is called once per frame
  39.  
    void Update()
  40.  
    {
  41.  
    if (isEnter)
  42.  
    {
  43.  
    transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  44.  
    transform.position = new Vector3(0,0, -Camera.main.transform.position.z);
  45.  
     
  46.  
    if(Vector3.Distance(transform.position, trunOriginPoint.position) > maxDistance)
  47.  
    {
  48.  
    Vector3 dir = (transform.position - trunOriginPoint.position).normalized;
  49.  
    transform.position = dir * maxDistance trunOriginPoint.position;
  50.  
    }
  51.  
     
  52.  
     
  53.  
     
  54.  
     
  55.  
    }
  56.  
    }
  57.  
     
  58.  
     
  59.  
    private void OnMouseDown()
  60.  
    {
  61.  
    isEnter = true;
  62.  
    spj.enabled = true;
  63.  
    }
  64.  
     
  65.  
    private void OnMouseUp()
  66.  
    {
  67.  
    isEnter = false;
  68.  
    spj.enabled = false;
  69.  
    rig.velocity *= springForce;
  70.  
    }
  71.  
    }
学新通

------------------------------ 2022-10-11 月更新

类似愤怒小鸟发射 不要使用 SpringJoint2D  有些教学视频简直就是误人子弟 我们学习者要懂得思考 擦亮眼睛

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgafakg
系列文章
更多 icon
同类精品
更多 icon
继续加载