Unity does not update frames by rotating the character with the movement of the mouse

2

My Unity is not giving error, or anything. Everything is OK, but the character does not move.
I already looked at the code and no mistake, I move the mouse and nothing happens to the character. The Unity version is 4.6 and the code is this:

public class PlayerMovement : MonoBehaviour
{
   public float speed = 6f;
   Vector3 movement;
   Animator anim;
   Rigidbody playerRigidbody;
   int floorMask;
   float camRayLenght = 100f;

   void Awake(){
      floorMask = LayerMask.GetMask ("Floor");
      anim = GetComponent<Animator> ();
      playerRigidbody = GetComponent<Rigidbody>();
   }

   void FixUpdate(){
      float h = Input.GetAxisRaw ("Horizontal");
      float v = Input.GetAxisRaw ("Vertical");
      Move (h, v);
      Turning ();
      Animating (h, v);
   }

   void Move(float h,float v){
      //tertemina o movimento
      movement.Set(h,0f,v);
      //normaliza o movimento
      movement = movement.normalized * speed * Time.deltaTime;
      // efetua o movimento
      playerRigidbody.MovePosition (transform.position + movement);
   }

   //girar o jogador
   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;
          // rotaçao do personagem
          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 01.03.2015 / 07:03

1 answer

4

The error is in:

void FixUpdate(){
  float h = Input.GetAxisRaw ("Horizontal");
  float v = Input.GetAxisRaw ("Vertical");
  Move (h, v);
  Turning ();
  Animating (h, v);
}

Unity uses an internal function called FixedUpdate

Everything within it will be called at every frame update. If you use another name, it will think it's another function or method.

Just change the

FixUpdate()

by

 FixedUpdate ()

The code looks like this:

void FixedUpdate(){
  float h = Input.GetAxisRaw ("Horizontal");
  float v = Input.GetAxisRaw ("Vertical");
  Move (h, v);
  Turning ();
  Animating (h, v);
}

* Do not forget to say more details of the tutorial you are doing to make it easier for people who are trying to get your questions answered Unity training day 2014: Survival Shooter

    
01.03.2015 / 10:09