Raycast向后进行,但i编码它可以向前
我尽力而为,但我的射线播放仍然每次都向后倒退 - 请我需要帮助
这里是我的代码 - 基本上,代码射击了一个射线播放的前进按钮以检测敌人,但是射线播放的方式相反。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class milkscript: MonoBehaviour
{
NavMeshAgent Agent;
public Transform Point;
public float raydistance = 40f;
public float enemyview = 5f;
// Start is called before the first frame update
void Start()
{
Agent = GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update()
{
RaycastHit Hit;
if(Physics.Raycast(Point.position,Point.position + Point.forward, out Hit, raydistance))
{
Debug.DrawRay(Point.position, Hit.transform.forward, Color.red);
Debug.Log(Hit.transform.name);
if(Hit.transform.tag == "chcolatemilk")
{
ChasePlayer(Hit.transform);
}
}
}
public void ChasePlayer(Transform target)
{
Agent.SetDestination(target.position);
transform.LookAt(target.position);
}
}
I try my best but my raycast still goes backwards every time - please I need help
Here's my code - basically the code shoots a raycast forward button to detected an enemy but instead the raycast goes the opposite way.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class milkscript: MonoBehaviour
{
NavMeshAgent Agent;
public Transform Point;
public float raydistance = 40f;
public float enemyview = 5f;
// Start is called before the first frame update
void Start()
{
Agent = GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update()
{
RaycastHit Hit;
if(Physics.Raycast(Point.position,Point.position + Point.forward, out Hit, raydistance))
{
Debug.DrawRay(Point.position, Hit.transform.forward, Color.red);
Debug.Log(Hit.transform.name);
if(Hit.transform.tag == "chcolatemilk")
{
ChasePlayer(Hit.transform);
}
}
}
public void ChasePlayer(Transform target)
{
Agent.SetDestination(target.position);
transform.LookAt(target.position);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(1)
我认为这是两个问题的组合
,如您所述
在
hit.transform.forward
的方向上绘制射线,这不是您拍摄raycast
中的原始方向
raycast
您正在传递,而不是位置。
您只想通过
point.forward
作为方向传递
I think this is a combination of two issues
As mentioned your
draws the ray in direction of
Hit.transform.forward
which is not the original direction you shoot yourRaycast
inAs a direction or the
Raycast
you are passing inwhich rather is a position.
You want to pass in only
Point.forward
as a directionSo it should rather be