Search Unity

Resolved Unity keeps changing my move speed when I click play

Discussion in 'Getting Started' started by DavidJohnWilson, Apr 9, 2023.

  1. DavidJohnWilson

    DavidJohnWilson

    Joined:
    Apr 4, 2023
    Posts:
    2
    So I'm a beginner who is learning Unity because it seems fun, which it has been and I've been enjoying going though the tutorials which has honestly been excellent so far and I can't praise this community enough for how much effort seems to have been put into making it easy for new people to dive into unity.

    but enough brown nosing, here my problem that I have run into:

    I have been going though the tutorial by Chris' tutorials
    2D Top Down Pixel Art RPG Game Dev in Unity 2022 ~ Crash Course Tutorial for Beginners - YouTube
    and I have gotten half way though to where I am moving the player sprite around which works but is incredible slow.
    what I have noticed is the "Move Speed" starts off with the value of 1 but when you click play and then press a move key it changes to 0.04.
    I have double checked the code (See below) and I can't see anything wrong with it but if I remove the Time.fixedDeltaTime from
    Code (CSharp):
    1. rb.MovePosition(rb.position + (direction * moveSpeed * Time.fixedDeltaTime));
    The sprite moves at a normal speed but the "Move Speed" still shows as 0.04, so I don't think that this is the cause.

    Does anyone have any ideas?

    Before
    https://ibb.co/T26gBjs

    After

    https://ibb.co/ZXYT2Qc

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class PlayerController : MonoBehaviour
    7. {
    8.     public float moveSpeed = 1f;
    9.     public float collisionOffset = 0.05f;
    10.     public ContactFilter2D movementFilter;
    11.  
    12.     Vector2 movementInput;
    13.     Rigidbody2D rb;
    14.     List<RaycastHit2D> castCollisions = new List<RaycastHit2D>();
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         rb =   GetComponent<Rigidbody2D>();
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     private void FixedUpdate()
    24.     {
    25.         // If movement input is not 0, try to move
    26.         if (movementInput != Vector2.zero)
    27.         {
    28.             bool success = TryMove(movementInput);
    29.          
    30.             if (!success)
    31.             {
    32.                 success = TryMove(new Vector2(movementInput.x, 0));
    33.  
    34.                 if (!success)
    35.                 {
    36.                     success = TryMove(new Vector2(0, movementInput.y));
    37.                 }
    38.             }
    39.         }
    40.     }
    41.  
    42.     private bool TryMove(Vector2 direction)
    43.         {
    44.         //Check for potential collisions
    45.         int count = rb.Cast(
    46.             movementInput, // X and Y values between -1 and 1 that represent the direction from the body to look for collisions
    47.             movementFilter, // The settings that determine where a collision can occur on such as layers to collide with
    48.             castCollisions, // List of collisions to store the found collisions into after the Cast is finished
    49.             moveSpeed = Time.fixedDeltaTime + collisionOffset); // the amount to cast equal to the moment plus an offset
    50.  
    51.         if (count == 0)
    52.         {
    53.             rb.MovePosition(rb.position + (direction * moveSpeed * Time.fixedDeltaTime));
    54.             return true;
    55.         }
    56.         else
    57.         {
    58.             return false;
    59.         }
    60.     }
    61.  
    62.  
    63.     void OnMove(InputValue movementValue)
    64.     {
    65.         movementInput = movementValue.Get<Vector2>();
    66.     }
    67.      
    68. }
    69.  
     
  2. DavidJohnWilson

    DavidJohnWilson

    Joined:
    Apr 4, 2023
    Posts:
    2
    This works fine when I tried a different guide so i am just going to put it down to something unknown this I clicked that I should not of.
     
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,172
    You're modifying the value of moveSpeed on line 49.