Raycast向后进行,但i编码它可以向前

发布于 2025-02-11 08:05:22 字数 1131 浏览 1 评论 0原文

我尽力而为,但我的射线播放仍然每次都向后倒退 - 请我需要帮助

这里是我的代码 - 基本上,代码射击了一个射线播放的前进按钮以检测敌人,但是射线播放的方式相反。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡淡的优雅 2025-02-18 08:05:22

我认为这是两个问题的组合

  1. ,如您所述

      debug.drawRay(point.position,hit.transform.forward,color.red);
     

    hit.transform.forward的方向上绘制射线,这不是您拍摄raycast

    中的原始方向

  2. 作为方向 raycast 的原始方向或raycast您正在传递

      point.point.point.point.forward
     

    ,而不是位置

    您只想通过point.forward作为方向

传递

void Update()
{
    if(Physics.Raycast(Point.position, Point.forward, out var Hit, raydistance)) 
    {
        Debug.DrawLine(Point.position, Hit.transform.position, Color.red);
        // TODO: remove this later! Logging every frame is quite expensive!
        Debug.Log(Hit.transform.name);

        // prefer "CompareTag" over "=="! It is a) slightly faster and b)
        // instead of failing silently throws an error for typos and non-existent tags => better debugging live
        if(Hit.transform.CompareTag("chcolatemilk")) 
        {
            ChasePlayer(Hit.transform);
        }
    }
    Debug.DrawRay(Point.position, Point.forward, Color.green);
}

I think this is a combination of two issues

  1. As mentioned your

    Debug.DrawRay(Point.position, Hit.transform.forward, Color.red);
    

    draws the ray in direction of Hit.transform.forward which is not the original direction you shoot your Raycast in

  2. As a direction or the Raycast you are passing in

    Point.position + Point.forward
    

    which rather is a position.

    You want to pass in only Point.forward as a direction

So it should rather be

void Update()
{
    if(Physics.Raycast(Point.position, Point.forward, out var Hit, raydistance)) 
    {
        Debug.DrawLine(Point.position, Hit.transform.position, Color.red);
        // TODO: remove this later! Logging every frame is quite expensive!
        Debug.Log(Hit.transform.name);

        // prefer "CompareTag" over "=="! It is a) slightly faster and b)
        // instead of failing silently throws an error for typos and non-existent tags => better debugging live
        if(Hit.transform.CompareTag("chcolatemilk")) 
        {
            ChasePlayer(Hit.transform);
        }
    }
    Debug.DrawRay(Point.position, Point.forward, Color.green);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文