调整方法不起作用

时间:2016-10-24 11:43:07

标签: c# unity3d

我正在学习Unity游戏开发..我正在关注https://unity3d.com/learn/tutorials/projects/survival-shooter/player-character?playlist=17144

一切都一样,但

我希望播放器对象朝向鼠标指针旋转。但它不会转向鼠标指针..

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    Vector3 movement;
    Animator anim;
    Rigidbody playerRigidbody;
    int floormask;
    float camraylength = 100f;
    float speed = 10f;



    void Awake()
    {
        anim = GetComponent<Animator>();
        floormask = LayerMask.GetMask("floor");
        playerRigidbody = GetComponent<Rigidbody>();
    }
    void FixedUpdate()
    {
        float h = Input.GetAxisRaw("Horizontal");
        float v = Input.GetAxisRaw("Vertical");
        moving(h, v);
        tunning();
        animationz(h, v);
    }
    void moving(float h, float v)
    {
        movement = new Vector3(h, 0, v);
        movement = movement.normalized * speed * Time.deltaTime;
        playerRigidbody.MovePosition(transform.position + movement);
    }
    void tunning()
    {
        Ray camray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit floorhit;
        if(Physics.Raycast(camray,out floorhit, camraylength, floormask))
        {
            Vector3 playertomouse = floorhit.point - transform.position;
            playertomouse.y = 0f;
            Quaternion newrotate = Quaternion.LookRotation(playertomouse);
            playerRigidbody.MoveRotation(newrotate); 
        }

    }
    void animationz(float h, float v)
    {
        bool walking = h != 0f || v != 0f;
        anim.SetBool("IsWalking", walking);
    }
}

tunning()方法在上面的代码中

如果你想要更多,那么请评论。谢谢你的帮助...

1 个答案:

答案 0 :(得分:0)

我建议您检查几个问题以找到问题的根源:

  • 您需要确保使用的是Quad而不是平面并且正确的面朝上
  • 确保四边形的图层是“地板”(区分大小写)
  • 确保相机不超过正在投射的光线的100f
  • 最后在if条件中添加一个Debug.Log()来检查光线是否完全击中了地板。