Unity 2D-光线没有朝着正确的方向前进
Unity 2D- Rays not going in the right direction
我有一个游戏对象,它应该向多个方向发射光束。我想让它尽可能灵活,所以我添加了几个目标,脚本将在其中绘制一条射线(从游戏对象到目标)。我已经使用了目标变换的位置变量,但这只会导致所有光线都向一个奇怪的方向发射,但当我四处移动时,它确实发生了轻微的变化。然后我尝试了目标的局部位置,但这导致所有光线都沿其原始方向行进,而不考虑旋转的变化。
这是激光脚本的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Laser : MonoBehaviour {
private LineRenderer Linerenderer;
public List<Transform> rays;
void Start()
{
Physics2D.queriesStartInColliders = false;
}
void Update () {
foreach (Transform tran in rays)
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, tran.position,20);
Debug.DrawLine(transform.position, hit.point);
}
}
}
截图:
-Using world positions:
-Using local positions:
Physics2D.Raycast() method's second parameter you are specifying is wrong: it should not be a position in space, but a direction. The second parameter for the Debug.DrawRay()方法也是一个方向,而不是space中的点。
尝试以下操作:
void Update () {
foreach (Transform tran in rays)
{
Vector2 direction = (tran.position - transform.position).normalized;
RaycastHit2D hit = Physics2D.Raycast(transform.position, direction, 20);
Vector2 rayDirection = hit.point - transform.position;
Debug.DrawRay(transform.position, rayDirection);
}
}
谢谢,经过一番折腾,我终于能够让它工作了。
如果其他人需要类似的帮助(动态光线投射)。
这是最终代码(您可能需要稍微编辑一下):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Laser : MonoBehaviour {
private LineRenderer Linerenderer;
public List<Transform> rays;
void Start()
{
Physics2D.queriesStartInColliders = false;
}
void Update()
{
foreach (Transform tran in rays)
{
Debug.DrawRay(new Vector3(1, 1, 0), new Vector3(-1, -1, 0));
Vector2 direction = (tran.position - transform.position).normalized;
RaycastHit2D hit = Physics2D.Raycast(transform.position, direction);
Vector2 rayDirection = (hit.point - new Vector2( transform.position.x, transform.position.y));
Debug.DrawRay(transform.position, rayDirection);
}
}
}
我有一个游戏对象,它应该向多个方向发射光束。我想让它尽可能灵活,所以我添加了几个目标,脚本将在其中绘制一条射线(从游戏对象到目标)。我已经使用了目标变换的位置变量,但这只会导致所有光线都向一个奇怪的方向发射,但当我四处移动时,它确实发生了轻微的变化。然后我尝试了目标的局部位置,但这导致所有光线都沿其原始方向行进,而不考虑旋转的变化。
这是激光脚本的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Laser : MonoBehaviour {
private LineRenderer Linerenderer;
public List<Transform> rays;
void Start()
{
Physics2D.queriesStartInColliders = false;
}
void Update () {
foreach (Transform tran in rays)
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, tran.position,20);
Debug.DrawLine(transform.position, hit.point);
}
}
}
截图:
-Using world positions:
-Using local positions:
Physics2D.Raycast() method's second parameter you are specifying is wrong: it should not be a position in space, but a direction. The second parameter for the Debug.DrawRay()方法也是一个方向,而不是space中的点。
尝试以下操作:
void Update () {
foreach (Transform tran in rays)
{
Vector2 direction = (tran.position - transform.position).normalized;
RaycastHit2D hit = Physics2D.Raycast(transform.position, direction, 20);
Vector2 rayDirection = hit.point - transform.position;
Debug.DrawRay(transform.position, rayDirection);
}
}
谢谢,经过一番折腾,我终于能够让它工作了。 如果其他人需要类似的帮助(动态光线投射)。 这是最终代码(您可能需要稍微编辑一下):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Laser : MonoBehaviour {
private LineRenderer Linerenderer;
public List<Transform> rays;
void Start()
{
Physics2D.queriesStartInColliders = false;
}
void Update()
{
foreach (Transform tran in rays)
{
Debug.DrawRay(new Vector3(1, 1, 0), new Vector3(-1, -1, 0));
Vector2 direction = (tran.position - transform.position).normalized;
RaycastHit2D hit = Physics2D.Raycast(transform.position, direction);
Vector2 rayDirection = (hit.point - new Vector2( transform.position.x, transform.position.y));
Debug.DrawRay(transform.position, rayDirection);
}
}
}