Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Character movement speed doesn´t adjust right

Discussion in 'Documentation' started by cirazgames, Dec 12, 2021.

  1. cirazgames

    cirazgames

    Joined:
    Apr 13, 2021
    Posts:
    11
    The movement speed of my character doesn´t adjust right. When I upgrade it to 100 it´s way too fast and I don´t know how else to calculate it.
    The IEnumerator "PlayerMove", moves the Player and the float moveSpeed, is the value that indicates how fast the character is being moved (the movement speed).
    Does anyone have any idea, how I can fix it, so the character doesn´t move so fast, when upgrading the moveSpeed?


    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using UnityEngine.EventSystems;
    7. using System;
    8.  
    9. public class JoystickController : MonoBehaviour, IDragHandler, IPointerDownHandler, IPointerUpHandler
    10. {
    11.     public SpawnPlayer spawnPlayer;
    12.  
    13.     //Rotation
    14.     public Transform meshPlayer;
    15.     private Transform cameraTransform;
    16.  
    17.     //Joystick Things
    18.     private RectTransform JoystickBackground;
    19.     private Image Joystick;
    20.  
    21.     //Input Axes (Used for Mobile and Computer)
    22.     private Vector2 positionInput;
    23.     private float inputX;
    24.     private float inputZ;
    25.  
    26.     //Player Movement
    27.     public float moveSpeed;
    28.     private Vector3 movement;
    29.     public float sprintSpeed;
    30.     public float walkSpeed;
    31.  
    32.     private PlayerStatsManager psm;
    33.  
    34.  
    35.     void Start()
    36.     {
    37.         StartCoroutine("GetComponents");
    38.         Invoke("SetSpeeds", 1f);
    39.     }
    40.  
    41.     private void GetNeededComponents()
    42.     {
    43.         //Automatically get SpawnPlayer-Script
    44.         GameObject temporarySP = GameObject.FindGameObjectWithTag("PlayerAvatar");
    45.         spawnPlayer = temporarySP.GetComponentInChildren<SpawnPlayer>();
    46.  
    47.         //Automatically find Camera
    48.         GameObject temporaryCamera = GameObject.FindGameObjectWithTag("Camera");
    49.         cameraTransform = temporaryCamera.transform.GetChild(0);
    50.  
    51.         //Auto get Joystick Components
    52.         Joystick = GetComponent<Image>();
    53.         GameObject temporaryJoystickBackground = GameObject.FindGameObjectWithTag("Joystick");
    54.         JoystickBackground = (RectTransform)temporaryJoystickBackground.transform.GetChild(0);
    55.  
    56.         //Automatically find the Character
    57.         //GameObject temporaryPlayer = GameObject.FindGameObjectWithTag("Player");
    58.         meshPlayer = GameObject.FindGameObjectWithTag("MeshPlayer").transform;
    59.     }
    60.  
    61.     public void OnDrag(PointerEventData eventData)
    62.     {
    63.         if (RectTransformUtility.ScreenPointToLocalPointInRectangle(JoystickBackground, eventData.position, eventData.pressEventCamera, out positionInput))
    64.         {
    65.             positionInput.x = positionInput.x / (JoystickBackground.sizeDelta.x);
    66.             positionInput.y = positionInput.y / (JoystickBackground.sizeDelta.y);
    67.             //print(positionInput.x.ToString() + "/" + positionInput.y.ToString());
    68.  
    69.             //normalize
    70.             if (positionInput.magnitude > 1.0f)
    71.             {
    72.                 positionInput = positionInput.normalized;
    73.             }
    74.  
    75.             //Joystick movement ability
    76.             Joystick.rectTransform.anchoredPosition = new Vector2
    77.                 (positionInput.x * (JoystickBackground.sizeDelta.x / 1.8f),
    78.                 positionInput.y * (JoystickBackground.sizeDelta.y / 1.8f));
    79.         }
    80.  
    81.         movement = new Vector3(transform.localPosition.x, 0, transform.localPosition.y).normalized;
    82.  
    83.         movement = movement.x * cameraTransform.right.normalized + movement.z * cameraTransform.forward.normalized;
    84.         movement.y = 0f;
    85.     }
    86.  
    87.  
    88.     public void OnPointerDown(PointerEventData eventData)
    89.     {
    90.         //OnDrag(eventData);
    91.         StartCoroutine("PlayerMove");
    92.     }
    93.  
    94.     public void OnPointerUp(PointerEventData eventData)
    95.     {
    96.         positionInput.x = 0;
    97.         positionInput.y = 0;
    98.  
    99.         transform.localPosition = Vector3.zero;
    100.         movement = Vector3.zero;
    101.  
    102.         StopCoroutine("PlayerMove");
    103.     }
    104.  
    105.     //Movement through joystick with Animations
    106.     private void Update()
    107.     {
    108.         //print(inputX + inputZ);
    109.  
    110.         //Inputs for Computer and Mobile in one
    111.         inputX = inputHorizontal();
    112.         inputZ = inputVertical();
    113.  
    114.         //Transition between Animations
    115.         //idle
    116.         if (inputX == 0f && inputZ == 0f)
    117.         {
    118.             //animator.SetFloat("Speed", 0.0f, 0.01f, Time.deltaTime);
    119.             spawnPlayer.animator.SetBool("Walk", false);
    120.             spawnPlayer.animator.SetBool("Run", false);
    121.         }
    122.         //walk
    123.         else if (inputX > 0f && inputX <= 0.5f || inputZ > 0.1f && inputZ <= 0.5f
    124.             || inputX > -0.5f && inputX < 0f || inputZ > -0.5f && inputZ < 0f)
    125.         {
    126.             //animator.SetFloat("Speed", 0.5f, 0.01f, Time.deltaTime);
    127.             spawnPlayer.animator.SetBool("Walk", true);
    128.             spawnPlayer.animator.SetBool("Run", false);
    129.  
    130.             moveSpeed = walkSpeed;
    131.         }
    132.  
    133.         //run
    134.         if (inputX > 0.6f && inputX <= 2.1f || inputZ > 0.6f && inputZ <= 2.1f
    135.             || inputX >= -2.1f && inputX < -0.6f || inputZ >= -2.1f && inputZ < -0.6f)
    136.         {
    137.             //animator.SetFloat("Speed", 2f, 0.01f, Time.deltaTime);
    138.             spawnPlayer.animator.SetBool("Run", true);
    139.             moveSpeed = sprintSpeed;
    140.         }
    141.     }
    142.  
    143.     private IEnumerator PlayerMove()
    144.     {
    145.         while (true)
    146.         {
    147.             //Start Player Movement
    148.             meshPlayer.Translate(movement * moveSpeed * Time.deltaTime, Space.World);
    149.  
    150.             if (movement != Vector3.zero)
    151.                 meshPlayer.rotation = Quaternion.Slerp(meshPlayer.rotation, Quaternion.LookRotation(movement), 5 * Time.deltaTime);
    152.  
    153.  
    154.             yield return null;
    155.         }
    156.     }
    157.  
    158.     private float inputHorizontal()
    159.     {
    160.         if (positionInput.x != 0)
    161.             return positionInput.x;
    162.         else
    163.             return Input.GetAxis("Horizontal");
    164.     }
    165.  
    166.     private float inputVertical()
    167.     {
    168.         if (positionInput.y != 0)
    169.             return positionInput.y;
    170.         else
    171.             return Input.GetAxis("Vertical");
    172.     }
    173.  
    174.     private IEnumerator GetComponents()
    175.     {
    176.         yield return new WaitForFixedUpdate();
    177.         GetNeededComponents();
    178.         StopCoroutine("GetComponents");
    179.     }
    180.  
    181.     public void SetSpeeds()
    182.     {
    183.         psm = meshPlayer.GetComponent<PlayerStatsManager>();
    184.  
    185.         moveSpeed = psm.speedLevel;
    186.         sprintSpeed = moveSpeed;
    187.         walkSpeed = moveSpeed * 0.25f;
    188.     }
    189. }
    190.  
     
  2. emirkivrak-dev

    emirkivrak-dev

    Joined:
    Jan 14, 2020
    Posts:
    10
    if you don't get it wrong, can i ask why you are moving your player inside an IEnumerator?
     
  3. cirazgames

    cirazgames

    Joined:
    Apr 13, 2021
    Posts:
    11

    I saw it in this tutorial:


    And with an IEnumerator, it works like an Update function, but I can start and stop it whenever I want.
     
  4. cirazgames

    cirazgames

    Joined:
    Apr 13, 2021
    Posts:
    11
    Never mind! I fixed it.