C#Unity Raycast问题,相机无响应

时间:2016-05-15 00:55:39

标签: c# unity3d camera

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

调试测试显示光线投射肯定有效。但是,我从控制台获得的关于我的代码的唯一消息是分配了变量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);
}

}

1 个答案:

答案 0 :(得分:1)

检查此行,

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

用targetMoveUse替换targetMove变量

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