微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

是否要相对于线的长度向LineRenderer施加力?

如何解决是否要相对于线的长度向LineRenderer施加力?

在这里闲逛了很长时间,并通过其他人的问题找到了很多帮助。但是经过大量搜索之后,我找不到帖子来帮助我解决所遇到的问题,因此我决定在此处发布自己的问题,以便如果得到回答,可能会帮助其他类似的人时尚!

我在向划线机加力时遇到一些问题。基本思想是,我有一个物体可以通过向反方向拖动一条线来向前射击。现在,所缺少的只是使镜头具有的力相对于“拖动线”的长度,如将对象拉动的“强度”如何。我对此有些麻烦,所以我想问一个比我更聪明,更有经验的人!

这是我绘制拉线的脚本:

public class DragIndicatorScript : MonoBehavIoUr

{

    Vector3 startPos;
    Vector3 endPos;
    Camera camera;
    LineRenderer lineRenderer;

    Vector3 camOffset = new Vector3(0,10);

    [Serializefield] AnimationCurve animCurve;

    // Start is called before the first frame update
    void Start()
    {
        camera = Camera.main;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            if (lineRenderer == null)
            {
                lineRenderer = gameObject.AddComponent<LineRenderer>();
            }
            lineRenderer.enabled = true;
            lineRenderer.positionCount = 2;
            startPos = camera.ScreenToWorldPoint(Input.mousePosition) + camOffset;
            lineRenderer.SetPosition(0,startPos);
            lineRenderer.useWorldspace = true;
            lineRenderer.widthCurve = animCurve;
            lineRenderer.numCapVertices = 10;
            lineRenderer.material = new Material(Shader.Find("Hidden/Internal-Colored"));
            lineRenderer.startColor = Color.magenta;
            lineRenderer.endColor = Color.cyan;
        }
        if (Input.GetMouseButton(0))
        {
            endPos = camera.ScreenToWorldPoint(Input.mousePosition) + camOffset;
            lineRenderer.SetPosition(1,endPos);
        }
        if (Input.GetMouseButtonUp(0))
        {
            lineRenderer.enabled = false;
        }
    }
}

这是我用来控制物体射击的脚本:

public class Shoot : MonoBehavIoUr
{
    Rigidbody2D rigidigidy;
    Vector2 startpos;
    Vector2 endpos;
    [Serializefield]  float power = 10f;

    void Start()
    {
        rigidigidy = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        if (Input.GetMouseButtonUp(0))
        {
            endpos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Launch();
        }
    }

    void OnMouseDown()
    {
        startpos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    }

    void Launch()
    {
        Vector2 direction = (startpos - endpos).normalized; 
        rigidigidy.AddForce(direction * power,ForceMode2D.Impulse);
    }
}

提前谢谢!我已经连续使用Google数小时,并编写了几行代码,但感觉好像找不到我想要的东西。

其他问题1) 我如何将划线机固定在始终处于可拉物体的位置?我希望这样做,以便玩家可以在可玩区域上的任何位置拖动,但是线性渲染器总是实例化有问题的对象(它始终是同一对象,因为它是“玩家模型”)。

其他问题2) 我知道如何在脚本中更改内衬渲染器的材质,但是我还没有真正找到从资产中调用材质的正确方法(我制作了要用作拉铲的材质),因此我正在使用的占位符:

Material(Shader.Find("Hidden/Internal-Colored"));
            lineRenderer.startColor = Color.magenta;
            lineRenderer.endColor = Color.cyan;

因为我想在那儿买些比认的粉红色的东西还要令人愉悦的东西。

解决方法

  1. 主要问题对我来说还不清楚。您已经有startpos-endpos。难道这已经不是包含您要寻找的幅度的向量了吗?

    我可能宁愿合并您的两个脚本,而不是让它们独立处理输入和位置。

    我将让您的Shoot处理输入并将其仅转发到DragIndicatorScript,因为顾名思义:这仅是一个指标,而不是决定最终射击强度的组件。

    因此Shoot宁愿计算输入向量,对其进行钳位,然后仅将其传递给指标,然后指标可以根据锚点(参见下文)和向量来更新其最终位置。

  2. 其他问题1:

    我只记得锚点和startPos之间的偏移量。然后,您稍后可以通过简单地考虑此偏移量来计算端点。

    为了能够在屏幕上拖动到任何地方,您不应该使用OnMouseDown,它仅在鼠标实际上位于对撞机上时才起作用,而应像在DragIndicatorScript中那样使用{{1 }}。

    听起来您还需要该行的最大长度,并且将此最大长度也用作最大拍摄功率。如前所述,为什么我要让拍摄控制一切呢?

  3. 其他问题2:

    您可以简单地在检查器的插槽中引用您的材料。但是总的来说,我建议宁愿预先添加GetMouseButtonDown(0)并进行完整设置。然后,您甚至不需要通过代码进行操作!

所以它看起来有点像

LineRenderer

public class Shoot : MonoBehaviour { [SerializeField] private DragIndicatorScript _dragIndicator; [SerializeField] private Rigidbody2D _rigidBody; [SerializeField] private float power = 10f; [SerializeField] private Camera _camera; // This script should clamp the input [SerializeField] private float maxLength; private Vector2 inputStartPosition; private void Awake() { if(!_rigidBody) _rigidBody = GetComponent<Rigidbody2D>(); if(!_camera) _camera = Camera.main; } private void Update() { if (Input.GetMouseButtonDown(0)) { // store the initial input position inputStartPosition = camera.ScreenToWorldPoint(Input.mousePosition); // Enable the indicator and for now set the endpoint also on the anchor since there wasn't any input yet _dragIndicator.SetIndicator(Vector2.zero); } if (Input.GetMouseButton(0)) { // Get the current position Vector2 inputCurrentPosition = camera.ScreenToWorldPoint(Input.mousePosition); // Get the input delta between the current and initial position var input = inputCurrentPosition - inputStartPosition; // Now clamp this vector to the max length input = Vector2.ClampMagnitude(input,maxLength); _dragIndicator.SetIndicator(input); } if (Input.GetMouseButtonUp(0)) { // Just to be sure recalculate from the current position Vector2 inputCurrentPosition = camera.ScreenToWorldPoint(Input.mousePosition); var input = inputCurrentPosition - inputStartPosition; // Now clamp this to the max length input = Vector2.ClampMagnitude(input,maxLength); _dragIndicator.HideIndicator(); // Directly pass in the final input vector,this way you don't need to store it in a field Launch(input); } } private void Launch(Vector2 input) { // Input already contains the direction and magnitude of the user input _rigidBody.AddForce(-input * power,ForceMode2D.Impulse); } } 仅接收以下命令:

DragIndicatorScript

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。