C# 一旦它击中统一体中的某个对象,就让CircleCast停止?

C# 一旦它击中统一体中的某个对象,就让CircleCast停止?,c#,unity3d,2d,raycasting,projectile,C#,Unity3d,2d,Raycasting,Projectile,我正在做一个游戏,2D投射物可以从墙上弹下来。我现在有对象本身反弹刚刚好,但我想一个(减去重力)显示对象将击中墙壁的位置,并显示它将反弹的角度:(请原谅我的绘画技巧) 我的系统在技术方面运行良好,除了一个事实,因为光线投射是无止境的,所以实际结果如下: 所以说得很清楚,最后,我希望第一个光线投射无限远,直到它击中某物,然后当它生成第二个光线投射,第一个击中某物时,让第二个光线投射只发出预先指定的距离(可能是3f或类似的距离),或者直到它击中某物,在这种情况下,它应该停止 我的脚本: Tran

我正在做一个游戏,2D投射物可以从墙上弹下来。我现在有对象本身反弹刚刚好,但我想一个(减去重力)显示对象将击中墙壁的位置,并显示它将反弹的角度:(请原谅我的绘画技巧)

我的系统在技术方面运行良好,除了一个事实,因为光线投射是无止境的,所以实际结果如下:

所以说得很清楚,最后,我希望第一个光线投射无限远,直到它击中某物,然后当它生成第二个光线投射,第一个击中某物时,让第二个光线投射只发出预先指定的距离(可能是3f或类似的距离),或者直到它击中某物,在这种情况下,它应该停止

我的脚本:

Transform firePoint;

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
        direction = firePoint.transform.up;
        float radius = 0.4f;
        RaycastHit2D hit = Physics2D.CircleCast(origin, radius, direction);

        // Draw black line from firepoint to hit point 1
        Debug.DrawLine(origin, direction * 10000, UnityEngine.Color.black);

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

            // Create second raycast
            RaycastHit2D hit2 = Physics2D.CircleCast(origin, radius, secondDirection);

            if (hit2)
            {
                if(hit.collider.gameObject.tag != "Destroy")
                {
                    // Enable prediction points
                    for (int i = 0; i < numOfPoints; i++)
                    {
                        predictionPoints2[i].SetActive(true);
                    }

                    // Calculate reflect direction
                    Vector2 origin2 = hit2.point + (hit2.normal * radius);
                    // Draw blue line from hit point 1 to predicted reflect direction
                    Debug.DrawLine(origin, secondDirection * 10000, UnityEngine.Color.blue);
                }      
            }
            
        }
    }

    Vector2 predictionPointPosition(float time)
    {
        Vector2 position = (Vector2)firePoint.position + direction.normalized * 10f * time;
        return position;
    }

    Vector2 predictionPointPosition2(float time, Vector2 origin, Vector2 direction)
    {
        Vector2 position = origin + direction.normalized * 10f * time;
        return position;
    }
变换火力点;
无效更新
{
DrawPredictionDisplay();
}
私有void DrawPredictionDisplay()
{
Vector2 origin=firePoint.transform.position;//unity有一个内置类型转换器,通过删除z组件将vector3转换为Vector2
方向=firePoint.transform.up;
浮动半径=0.4f;
RaycastHit2D hit=Physics2D.CircleCast(原点、半径、方向);
//从火力点到生命点1画一条黑线
调试。绘制线(原点,方向*10000,单位发动机。颜色。黑色);
如果(命中)
{          
原点=击中点+(击中法向*半径);
Vector2 secondDirection=Vector2.Reflect(方向、命中、法线);
//创建第二个光线投射
RaycastHit2D hit2=Physics2D.CircleCast(原点、半径、第二个方向);
如果(hit2)
{
if(hit.collider.gameObject.tag!=“销毁”)
{
//启用预测点
对于(int i=0;i
注意事项:

Transform firePoint;

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
        direction = firePoint.transform.up;
        float radius = 0.4f;
        RaycastHit2D hit = Physics2D.CircleCast(origin, radius, direction);

        // Draw black line from firepoint to hit point 1
        Debug.DrawLine(origin, direction * 10000, UnityEngine.Color.black);

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

            // Create second raycast
            RaycastHit2D hit2 = Physics2D.CircleCast(origin, radius, secondDirection);

            if (hit2)
            {
                if(hit.collider.gameObject.tag != "Destroy")
                {
                    // Enable prediction points
                    for (int i = 0; i < numOfPoints; i++)
                    {
                        predictionPoints2[i].SetActive(true);
                    }

                    // Calculate reflect direction
                    Vector2 origin2 = hit2.point + (hit2.normal * radius);
                    // Draw blue line from hit point 1 to predicted reflect direction
                    Debug.DrawLine(origin, secondDirection * 10000, UnityEngine.Color.blue);
                }      
            }
            
        }
    }

    Vector2 predictionPointPosition(float time)
    {
        Vector2 position = (Vector2)firePoint.position + direction.normalized * 10f * time;
        return position;
    }

    Vector2 predictionPointPosition2(float time, Vector2 origin, Vector2 direction)
    {
        Vector2 position = origin + direction.normalized * 10f * time;
        return position;
    }
虽然我会使用常规光线投射,但我发现普通光线投射不会剪切它,因为光线投射只有1像素宽,并且对象(大约)512px x x 512px,这意味着对象会在光线投射之前物理接触到墙,从而导致不准确

我已经创建了一个系统,在光线投射路径上以类似于《愤怒的小鸟》的方式生成点,这样玩家就可以在游戏视图中看到光线投射在做什么,但由于这与问题无关,我从上面的脚本中删除了这部分代码。这意味着我需要做的只是限制光线投射的距离,而不是让玩家看到发生了什么。(我将此添加到注释中,以避免出现玩家是否能看到发生了什么的对话。)


火力点
是发射炮弹的武器的枪管/尖端。(武器根据/跟随鼠标旋转)

你可以在知道它是否击中物体后绘制线/路径:

Vector2 origin = firePoint.transform.position; //unity has a built in type converter that converts vector3 to vector2 by dropping the z component
direction = firePoint.transform.up;
float radius = 0.4f;
RaycastHit2D hit = Physics2D.CircleCast(origin, radius, direction);

var firstPathStart = origin;
var firstPathEnd = origin + direction * 10000;    

if (hit)
{          
    origin = hit.point + (hit.normal * radius);
    firstPathEnd = origin;
    
    Vector2 secondDirection = Vector2.Reflect(direction, hit.normal); 
    var secondPathStart = firstPathEnd;
    var secondPathEnd = origin + secondDirection * 3f;           

    // Create second raycast
    RaycastHit2D hit2 = Physics2D.CircleCast(origin, radius, secondDirection);

    if (hit2)
    {
        if(hit.collider.gameObject.tag != "Destroy")
        {
            // Enable prediction points
            for (int i = 0; i < numOfPoints; i++)
            {
                predictionPoints2[i].SetActive(true);
            }

            // Calculate reflect direction
            Vector2 origin2 = hit2.point + (hit2.normal * radius);
            secondPathEnd = origin2;
        }      
    }
    // Draw blue line from hit point 1 to predicted reflect direction
    Debug.DrawLine(secondPathStart, secondPathEnd, UnityEngine.Color.blue);                  
} 

// Draw black line from firepoint to hit point 1
Debug.DrawLine(firstPathStart, firstPathEnd, UnityEngine.Color.black);  
Vector2原点=firePoint.transform.position//unity有一个内置类型转换器,通过删除z组件将vector3转换为vector2
方向=firePoint.transform.up;
浮动半径=0.4f;
RaycastHit2D hit=Physics2D.CircleCast(原点、半径、方向);
var firstPathStart=原点;
var firstPathEnd=原点+方向*10000;
如果(命中)
{          
原点=击中点+(击中法向*半径);
firstPathEnd=原点;
Vector2 secondDirection=Vector2.Reflect(方向、命中、法线);
var secondPathStart=firstPathEnd;
var secondPathEnd=原点+第二方向*3f;
//创建第二个光线投射
RaycastHit2D hit2=Physics2D.CircleCast(原点、半径、第二个方向);
如果(hit2)
{
if(hit.collider.gameObject.tag!=“销毁”)
{
//启用预测点
对于(int i=0;i

您可以创建一个类来存储路径信息(路径有多少段,每个段的起点和终点),在检查碰撞时填充并在末尾绘制。

Debug.DrawLine
将两个点作为参数,而不是一个点和一个方向,您的意思是使用
Debug.DrawRay
?您可以等待绘制路径,直到您知道是否击中了某个物体,并且您有了可以绘制路径的碰撞点/信息。@Pluto我以前从未使用过Debug.DrawRay,您能告诉我在这个上下文中它是什么样子吗?
Debug.DrawLine(origin,origin+direction*10000,UnityEngine.Co