Nightmares character does not move

2

I have a problem with the Nightmares game. My character does not move just does the Idle animation.

The script is there and seems to have no error.

publicclassPlayerMovement:MonoBehaviour{publicfloatspeed=6f;Vector3movement;Animatoranim;RigidbodyplayerRigidbody;intfloorMask;floatcamRayLenght=100f;voidAwake(){floorMask=LayerMask.GetMask("Floor");
        anim = GetComponent <Animator> ();
        playerRigidbody = GetComponent <Rigidbody> ();

    }


    void FixedUpdate ()
    {
        float h =Input.GetAxisRaw("Horizontal");
        float v =Input.GetAxisRaw("Vertical");

        Move (h,v);
        Turning ();
        Animating (h, v);

    }


    void Move (float h, float v)
    {

        movement.Set (h, 0f, v);

        movement = movement.normalized * speed * Time.deltaTime ;

        playerRigidbody.MovePosition (transform.position + movement);

    }

    void Turning ()
    {

        Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);

        RaycastHit floorHit;

        if(Physics.Raycast (camRay, out floorHit, camRayLenght, floorMask))
        {

            Vector3 playerToMouse = floorHit.point - transform.position;

            playerToMouse.y = 0f;

            Quaternion newRotation = Quaternion.LookRotation (playerToMouse);

            playerRigidbody.MoveRotation (newRotation);
        }




    }


    void Animating (float h,float v)
    {

        bool walking = h != 0f || v !=0f;

        anim.SetBool ("IsWalking", walking);
    }


}
    
asked by anonymous 23.07.2015 / 18:11

1 answer

3
  

Just to ask for an answer (and maybe to help other people in the   future), I will post here the conclusion of the comments. It is worth noting that the merit of the "solution" to the AP problem is also from @Nils. :)

At startup, that is, when calling the Awake method, your code does the following:

playerRigidbody = GetComponent <Rigidbody> ();

This code snippet gets the Rigidbody component attached to the current (player) object and stores it in the playerRigidbody variable.

Then, during the move, your code does the following:

playerRigidbody.MovePosition (transform.position + movement);

This call of method MovePosition of class Rigidbody will only work if this variable is not null.

As the error itself already indicates, there is no rigid body attached to the player object (in its example, the object Player ):

  

" There is no 'Rigidbody' attached to the 'Player' game object, but a   script is trying to access it. "

     

Free translation:

     

"There is a 'Rigidbody' component attached to the 'Player' game object,   but a script is trying to access it. "

Precisely because in your call of MovePosition the variable playerRigidbody is null, since you forgot to attach the 'Rigidbody' component to the player object.

    
24.07.2015 / 21:24