Unity does not update frames when rotating the character with mouse movement

Asked

Viewed 314 times

2

My Unity is not giving error, or anything. Everything is OK, but the character does not move.
I’ve looked at the code and no mistakes, I move the mouse and nothing happens to the character. 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);
   }
}
  • http://answall.com/questions/49919/como-girar-o-personagem-conforme-o-movimento-do-mouse

  • It is not duplicated, the problem is different from the one that sent the link. Just see the code, the "Floor" is correct in this.

  • @Nils Well, I didn’t score as a duplicate, I just posted a link of a question of similar title.

  • @I just wanted to warn the moderator, I didn’t know about the signal button. A lot of people saw this link and how the question name was the same they signaled as duplicate.

1 answer

4

The mistake 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 inside it will be called every frame update. If you use another name, it will think it is another function or method.

Just change the

FixUpdate()

for

 FixedUpdate ()

The code will look 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 facilitate the staff who will try to answer your questions Unity training day 2014: Survival Shooter

Browser other questions tagged

You are not signed in. Login or sign up in order to post.