首页 > 解决方案 > 使用 Unity 启动与 Line Renderer 相对的播放器时出现问题

问题描述

所以我希望我的播放器在我制作的线渲染器的对面启动。线渲染器工作正常,是我想要的样子。但是我无法尝试使物理起作用。我一直在浏览 Unity 文档和在线帖子,但仍然没有运气。

任何帮助将不胜感激。

线路启动脚本

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

public class LineLaunching : MonoBehaviour
{
    public float power;
    public float dragLength;

    public Rigidbody2D rb;
    public LineRenderer lineRend;
    public Camera cam;

    private Vector3 startPoint;
    private Vector3 endPoint;

    private Vector2 force;

    void Awake()
    {
        power = 100f;
    }

    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            startPoint = gameObject.transform.position;
            startPoint.z = 15;
            Vector3 currentPoint = cam.ScreenToWorldPoint(Input.mousePosition);
            currentPoint.z = 15;
            RenderLine(startPoint, currentPoint);

            rb.velocity = new Vector2(0, 0);
        }


        if (Input.GetMouseButtonUp(0))
        {
            endPoint = cam.ScreenToWorldPoint(Input.mousePosition);
            endPoint.z = 15;

            if (startPoint.x > endPoint.x && startPoint.y > endPoint.y)
            {
                rb.velocity = new Vector2(startPoint.x - endPoint.x * power * Time.deltaTime, startPoint.y - endPoint.y * power * Time.deltaTime);
            }

            if (startPoint.x > endPoint.x && startPoint.y < endPoint.y)
            {
                rb.velocity = new Vector2(startPoint.x - endPoint.x * power * Time.deltaTime, endPoint.y - startPoint.y * power * Time.deltaTime);
            }

            if (startPoint.x < endPoint.x && startPoint.y > endPoint.y)
            {
                rb.velocity = new Vector2(endPoint.x - startPoint.x * power * Time.deltaTime, startPoint.y - endPoint.y * power * Time.deltaTime);
            }

            if (startPoint.x < endPoint.x && startPoint.y < endPoint.y)
            {
                rb.velocity = new Vector2(endPoint.x - startPoint.x * power * Time.deltaTime, endPoint.y - startPoint.y * power * Time.deltaTime);
            }

            lineRend.positionCount = 0;
        }
    }

    public void RenderLine(Vector3 startPoint, Vector3 endPoint)
    {
        lineRend.positionCount = 2;
        Vector3[] points = new Vector3[2];
        points[0] = startPoint;
        points[1] = Vector3.MoveTowards(startPoint, endPoint, 6.5f);
        lineRend.SetPositions(points);
        float fade = Vector2.Distance(startPoint, endPoint) / 100 * 15;

        lineRend.startColor = new Color(1.0F, fade + 1 - (fade * 2), 0.0F, 1.0F);
        lineRend.endColor = new Color(1.0F, fade + 1 - (fade * 2), 0.0F, 0.0F);
    }

}

这里还有一个 gif,它显示了它在 Unity 中运行时的样子。

运行时 Unity 的 Gif:

运行时 Unity 的 Gif

标签: c#unity3d

解决方案


我只会从你的startto中获取方向向量end并否定它。它应该为您提供与您瞄准的完全相反的方向。如果您对速度进行检查,它也会减少复杂性。我还建议使用AddForce而不是直接设置速度,因此如果您希望其他外力作用在对象上,则它们不会被此代码覆盖。

if (Input.GetMouseButtonUp(0))
{
    endPoint = cam.ScreenToWorldPoint(Input.mousePosition);
    endPoint.z = 15;

    Vector2 negatedDirection = (startPoint - endPoint).normalized;

    rb.AddForce(negatedDirection * power, ForceMode2D.Impulse);

    lineRend.positionCount = 0;
}

您还可以使用另一种ForceMode2D方法,但是您似乎希望运动立即进行,Impulse这是要走的路。


推荐阅读