【问题标题】:Raycast 2D ricochet in Unity?Unity 中的 Raycast 2D 弹跳?
【发布时间】:2020-09-30 17:56:04
【问题描述】:

我是 Raycasting 的新手,所以我可能会以不好的方式处理这个问题,但我会向外发送光线投射到游戏对象所面对的方向,从它击中的第一个对象反弹并在消失前走一小段距离.

据我所知,Unity 中没有用于反射光线投射的内置函数,所以我一直在尝试生成另一个光线投射,但我的运气并不好。到目前为止,这是我所拥有的:

public Gameobject firePoint; // I have an object attached to my main object that I use as a point of origin

void DrawLazer()
{
    Vector2 origin = new Vector2(firePoint.transform.position.x, firePoint.transform.position.y);
 
        Vector2 direction = transform.TransformDirection(Vector2.up);
 
        RaycastHit2D hit = Physics2D.Raycast(origin, direction, 10f);
        Debug.DrawLine(origin, direction *10000, Color.black);
        if (hit)
        {
            Debug.Log("Hit: " + hit.collider.name);
            var whatWeHit = new Vector2(hit.transform.position.x, hit.transform.position.y);
            var offset = whatWeHit + hit.point;
            offset.y = 0;
 
            RaycastHit2D hit2 = Physics2D.Raycast(offset, Vector3.Reflect(direction, hit.normal) * -10000);
 
            if (hit2)
            {              
                Debug.DrawLine(offset, -Vector3.Reflect(direction, hit.normal) * -10000);
            }
        }
}

我调用 DrawLaxer();正在更新中。

当前的脚本能够生成第二次光线投射,但是正如您所见,第一次光线投射在击中某物时仍然不会停止,更重要的是,虽然该解决方案在击中平面物体时效果很好水平面。但是,如果它在垂直或对角平面上撞击物体,则会对错误的轴应用多个计算:

任何帮助将不胜感激。

【问题讨论】:

  • 你用一段代码发布了一些文本,这里的问题是什么?代码有什么问题?
  • @sommmen 我已经用一些屏幕截图和更多细节更新了我的 OP,这有帮助吗?

标签: unity3d 2d game-physics raycasting


【解决方案1】:

这是一个使用 Vector2.Reflect 和 SphereCast 的完整示例单体行为。

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

public class ReflectionExample : MonoBehaviour
{

    public GameObject firePoint;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        DrawPredictionDisplay();
    }


    private void DrawPredictionDisplay()
    {

        Vector2 origin = firePoint.transform.position; //unity has a built in type converter that converts vector3 to vector2 by dropping the z component

        Vector2 direction = firePoint.transform.up;

        float radius = 1.0f;

        RaycastHit2D distanceCheck = Physics2D.Raycast(origin, direction);

        RaycastHit2D hit = Physics2D.CircleCast(origin, radius, direction);


        Debug.DrawLine(origin, direction * 10000, UnityEngine.Color.black);
        DrawCircle(origin, 1.0f, UnityEngine.Color.black);

        if (hit)
        {

            origin = hit.point + (hit.normal * radius);
            direction = Vector2.Reflect(direction, hit.normal);

            hit = Physics2D.CircleCast(origin, radius, direction);

            Debug.DrawLine(origin, direction * 10000, UnityEngine.Color.blue);
            DrawCircle(origin, 1.0f, UnityEngine.Color.blue);
        }
    }


    private void DrawCircle(Vector2 center, float radius, UnityEngine.Color color)
    {
        Vector2 prevPoint = new Vector2(Mathf.Sin(0f), Mathf.Cos(0f));

        for (float t = 0.1f; t < 2 * Mathf.PI; t = t + 0.1f)
        {
            var nextPoint = new Vector2(Mathf.Sin(t), Mathf.Cos(t));

            Debug.DrawLine(center + prevPoint, center + nextPoint, color);

            prevPoint = nextPoint;
        }


    }
}

【讨论】:

  • 在第 10 行它说RaycastHit2D 不包含contacts 的定义。我是否缺少可能的依赖项?
  • 哦,我认为它只是 hit.normal
  • 现在它说 Reflect 需要 3 个参数。
  • 好的,我花了几个小时从你的代码中成功地做到了这一点:pastebin.com/pG0GgfBY(它如何为我工作的示例:imgur.com/a/lUDuUh4)我创建了一个 Raycast,然后正确地创建使用第二次 Raycast 的反射错觉,但是此代码仅在您击中的对象在水平面上完全平坦时才有效。我们怎样才能让它动态化,以便它考虑到我们击中的物体的旋转?
  • hit.point 已经在世界空间中,hit.normal 也是如此,因此您不需要通过变换进行偏移。只需使用 hit.point。我会根据你的新代码更新我的答案。
猜你喜欢
  • 2016-01-30
  • 1970-01-01
  • 2021-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多