Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Player goes at same speed, despite different number

Discussion in 'Scripting' started by UnityForever777, Feb 4, 2020.

  1. UnityForever777

    UnityForever777

    Joined:
    Jan 15, 2018
    Posts:
    6
    I have tried to change my player's speed both in the code, and through Unity, but My player still goes the smae speed

    I know I probably look stupid right now, but I can't seem to find a solution to this problem.

    Here is code:

    move.cs

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class move : MonoBehaviour
    6. {
    7.     public float moveSpeed = 20f;
    8.     public bool isGrounded = false;
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.        
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         Jump();
    19.         Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
    20.         transform.position += movement * Time.deltaTime;
    21.     }
    22.  
    23.     void Jump()
    24.     {
    25.         if (Input.GetButtonDown("Jump") && isGrounded == true) {
    26.             gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 10f), ForceMode2D.Impulse);
    27.         }
    28.     }
    29. }
    30.  
    grounded.cs

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class grounded : MonoBehaviour
    6. {
    7.     GameObject large;
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         large = gameObject.transform.parent.gameObject;
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.        
    18.     }
    19.  
    20.     private void OnCollisionEnter2D(Collision2D collision)
    21.     {
    22.         if (collision.collider.tag == "Ground")
    23.         {
    24.             large.GetComponent<move>().isGrounded = true;
    25.         }
    26.     }
    27.  
    28.     private void OnCollisionExit2D(Collision2D collision)
    29.     {
    30.         if (collision.collider.tag == "Ground")
    31.         {
    32.             large.GetComponent<move>().isGrounded = false;
    33.         }
    34.     }
    35. }
    36.  
     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    You're not actually using the moveSpeed anywhere.
     
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
    You defined a variable named "moveSpeed", but you don't actually use that variable in the calculation of how far the player should move.

    The computer doesn't understand English and doesn't try to guess what you want a variable to do based on its name. It only uses the variable when you explicitly say so.

    Try multiplying movement by moveSpeed.
     
    Madgvox likes this.
  4. UnityForever777

    UnityForever777

    Joined:
    Jan 15, 2018
    Posts:
    6
    Well, I feel stupid. Thank you, but im not sure why you wasted your time answering me. I should've realized this simple solution.
     
  5. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    We all start somewhere! Don't be too hard on yourself.
     
  6. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,772
    To be fair on you, the way unity works removes some of the warnings that would usually catch errors like this. we're encourages to make public fields to adjust things in the inspector but because they are public we don't get the warning that says a variable is declared but never used, which in this case would have saved you a fair bit of time.

    you get use to it.