Raycast(Circlecast)和对象方向不一样

时间:2018-03-26 23:53:21

标签: c# unity3d

我试图制作一条虚线,显示球从哪里着陆并从那里反射。我使用的是物理系统。

我认为圆形投射和线条渲染器出了问题。

无论我用鼠标点击哪里,球都会产卵。所以我没有看到BallCreate()函数有任何问题。

这是问题所在。

The Problem

正如你在图片中看到的那样,球在transform.position上实例化并通过hit.point。线条渲染器和球的方向不一样。总是有一点点差异(有时更多)。

代码如下:

我试图解决这个问题一个星期,任何帮助都意味着这么多。

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

public Transform BallPrefab = null;
bool ShootDirected = true;
Vector3 mousePos;
private LineRenderer linerender;
RaycastHit2D hitx;
private Vector3 dir;
private Vector3 origin;
void Start()
{
    linerender = GetComponent<LineRenderer>();  
}
void Update()
{

    mousePos = new Vector3(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y, 0);
    //Direction according to mouse position.
    dir = mousePos - transform.position;

    origin = transform.position;

    //First hit point 0.3307159f is the radius of the ball.
    RaycastHit2D hit = Physics2D.CircleCast(origin, 0.3307159f, dir, 100f, 1 << 9 | 1<<10);

    if (hit.collider != null)
    {
        //Stored reflected hit point.
        Vector2 reflectDir = Vector2.Reflect(dir, hit.normal);

        //Then start second ray from hit point thru reflected hit point.
        RaycastHit2D SecondHit = Physics2D.CircleCast(hit.point, 0.3307159f, reflectDir, 100, 1 << 9 | 1<<10);

        //Draw lines beetween origin, hit.point and secondhit point.
        linerender.SetPosition(0, origin);
        linerender.SetPosition(1, hit.point);
        linerender.SetPosition(2, SecondHit.point);

    }
 //Create 50 balls when mouse clicked.
    if (Input.GetMouseButtonUp(0))
    {
        ShootDirected = true;
        StartCoroutine("BallCreate");
    }
 }

 IEnumerator BallCreate()
    {  
    Vector3 shootDirection = new Vector3(0,0,0);

    //Create 50 balls
    for (int i = 0; i < 50; i++)
    {

        Transform ball = Instantiate(BallPrefab, transform.position, Quaternion.identity) as Transform;
        Rigidbody2D rb = ball.GetComponent<Rigidbody2D>();

        if (ShootDirected)
        {
            //Set shootDirection to mouse position
            shootDirection = Input.mousePosition;
            shootDirection.z = 0.0f;
            shootDirection = Camera.main.ScreenToWorldPoint(shootDirection);

            //Get direction.
            shootDirection = (shootDirection - transform.position);
                ShootDirected = false;
        }
        //Apply force to each ball thru mouse direction.
        rb.velocity = new Vector2(shootDirection.x, shootDirection.y);
        rb.velocity = 7f * (rb.velocity.normalized);

        yield return new WaitForSeconds(0.08f);

    }
}
}

1 个答案:

答案 0 :(得分:2)

在Unity Technologies的MelvMay的帮助下发现了这个问题。

只需更改

`RaycastHit2D SecondHit = Physics2D.CircleCast(hit.point, 0.3307159f, reflectDir, 100, 1 << 9 | 1<<10);`

RaycastHit2D SecondHit = Physics2D.CircleCast(hit.centroid, 0.3307159f, reflectDir, 100, 1 << 9 | 1<<10);

MelvMay的解释;

如果您查看RaycastHit2D文档,您将看到两个“点”属性(您正在使用),这是形状相交的实际位置。对于射线而言,这是显而易见的,因为它没有大小。对于圆形等形状,这不是圆相交时的位置,而是圆的外观点。对于所有演员表,您可以使用“centroid”属性。这将返回形状接触时的位置。对于线/光线,“点”和“质心”属性都是相同的,但对于(例如)圆,盒,胶囊或多边形,这些将是不同的。