Search Unity

Resolved Player Moving Very Slow

Discussion in 'Scripting' started by Chimaira, Jul 8, 2020.

  1. Chimaira

    Chimaira

    Joined:
    Jul 8, 2020
    Posts:
    12
    I'm new to this an following some tutorials online. The problem is my player moves very slow if using Update instead of FixedUpdate but then my player attack has a massive delay between attacks if using FixedUpdate. Any help is greatly appreciated.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public enum PlayerState
    6. {
    7.     walk,
    8.     attack,
    9.     interact
    10. }
    11.  
    12. public class PlayerMovement : MonoBehaviour
    13. {
    14.     public PlayerState currentState;
    15.     public float speed;
    16.     private Rigidbody2D myRigidbody;
    17.     private Vector3 change;
    18.     private Animator animator;
    19.  
    20.     // Start is called before the first frame update
    21.     void Start()
    22.     {
    23.         currentState = PlayerState.walk;
    24.         animator = GetComponent<Animator>();
    25.         myRigidbody = GetComponent<Rigidbody2D>();
    26.         animator.SetFloat("moveX", 0);
    27.         animator.SetFloat("moveY", -1);
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void FixedUpdate()
    32.     {
    33.         change = Vector3.zero;
    34.         change.x = Input.GetAxisRaw("Horizontal");
    35.         change.y = Input.GetAxisRaw("Vertical");
    36.         if (Input.GetButtonDown("attack") && currentState != PlayerState.attack)
    37.         {
    38.             StartCoroutine(AttackCo());
    39.         }
    40.         else if (currentState == PlayerState.walk)
    41.         {
    42.             UpdateAnimatonAndMove();
    43.         }
    44.     }
    45.  
    46.     private IEnumerator AttackCo()
    47.     {
    48.         animator.SetBool("attacking", true);
    49.         currentState = PlayerState.attack;
    50.         yield return null;
    51.         animator.SetBool("attacking", false);
    52.         yield return new WaitForSeconds(.3f);
    53.         currentState = PlayerState.walk;
    54.     }
    55.  
    56.     void UpdateAnimatonAndMove()
    57.     {
    58.         if (change != Vector3.zero)
    59.         {
    60.             MoveCharacter();
    61.             animator.SetFloat("moveX", change.x);
    62.             animator.SetFloat("moveY", change.y);
    63.             animator.SetBool("moving", true);
    64.         }
    65.         else
    66.         {
    67.             animator.SetBool("moving", false);
    68.         }
    69.     }
    70.  
    71.     void MoveCharacter()
    72.     {
    73.         change.Normalize();
    74.         myRigidbody.MovePosition(
    75.             transform.position + change * speed * Time.deltaTime
    76.             );
    77.     }
    78. }
    79.  
     
  2. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    can you not just increase to speed value? If it is linked to the attacks also, then just break it up into two variables.
     
  3. Chimaira

    Chimaira

    Joined:
    Jul 8, 2020
    Posts:
    12
    I noticed more issues with just increasing the value. I ended up separating the speed calculation into a FixedUpdate and movement control in update. Thanks

    Code (CSharp):
    1.     void Update() {
    2.         change = Vector2.zero;
    3.         change.x = Input.GetAxisRaw("Horizontal");
    4.         change.y = Input.GetAxisRaw("Vertical");
    5.         if (Input.GetButtonDown("attack") && currentState != PlayerState.attack)
    6.         {
    7.             StartCoroutine(AttackCo());
    8.         }
    9.         else if (currentState == PlayerState.walk)
    10.         {
    11.             UpdateAnimatonAndMove();
    12.         }
    13.     }
    14.  
    15.     void FixedUpdate()
    16.     {
    17.         myRigidbody.MovePosition(myRigidbody.position + change * playerSpeed * Time.fixedDeltaTime);
    18.     }
     
    Cyber-Dog likes this.