【问题标题】:C# Unity Raycast problems, camera not respondingC# Unity Raycast 问题,相机没有响应
【发布时间】:2016-05-15 00:55:39
【问题描述】:

我在我的播放器和相机之间创建了一个光线投射,当它检测到命中时,将相机移动到对象前面,这样对象就不会阻碍视野。

调试测试表明光线投射确实可以正常工作。然而,我从控制台得到的关于我的代码的唯一消息是变量 targetMoveUse 已分配,但它的值从未使用过。

我从 youtube 上的教程中学习了这段代码,并按照它进行了操作,但尽管有与教程相同的代码,但它对我不起作用。教程在这里供参考:https://www.youtube.com/watch?v=0nI0rX5fEY0

这是我的代码。任何帮助表示赞赏。

Transform player;
Quaternion targetLook;
Vector3 targetMove;
public float rayHitMoveInFront = 0.1f;
Vector3 targetMoveUse;
public float smoothLook = 0.5f;
public float smoothMove = 0.5f;
Vector3 smoothMoveV;
public float distFromPlayer = 5;
public float heightFromPlayer = 3;

void Start () {
    player = GameObject.FindWithTag ("Player").transform;
}

void Update () {
    targetMove = player.position + (player.rotation * new Vector3 (0, heightFromPlayer, -distFromPlayer));

    RaycastHit hit;
    if (Physics.Raycast (player.position, targetMove - player.position, out hit, Vector3.Distance (player.position, targetMove)))
        targetMoveUse = Vector3.Lerp (hit.point, player.position, rayHitMoveInFront);
    else 
        targetMoveUse = targetMove;

    //chapter 4 episode 3 part 1
    //transform.position = player.position + (player.rotation * new Vector3 (0, heightFromPlayer, -distFromPlayer));


    //transform.position = Vector3.Lerp (transform.position, targetMove, smoothMove * Time.deltaTime);

    transform.position = Vector3.SmoothDamp (transform.position, targetMove, ref smoothMoveV, smoothMove);

    targetLook = Quaternion.LookRotation (player.position - transform.position);
    transform.rotation = Quaternion.Lerp (transform.rotation, targetLook, smoothLook * Time.deltaTime);
    //chapter 4 episode 1
    //transform.LookAt (player);
}

}

【问题讨论】:

  • 那么什么不起作用?你没有提到那部分......

标签: c# unity3d camera


【解决方案1】:

检查这一行,

  transform.position = Vector3.SmoothDamp (transform.position, targetMove, ref smoothMoveV, smoothMove);

用 targetMoveUse 替换 targetMove 变量

transform.position = Vector3.SmoothDamp (transform.position, targetMoveUse, ref smoothMoveV, smoothMove);

【讨论】:

  • 做到了!谢谢!
  • 欢迎您,请勾选此帖为正确答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多