Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question I have a movement problem

Discussion in 'Testing & Automation' started by CodeD306, Mar 26, 2023.

  1. CodeD306

    CodeD306

    Joined:
    Dec 22, 2022
    Posts:
    20
    im new in unity and i tried to make a 2D character movement, but the character cant jump
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.Collections;
    4. using UnityEngine;
    5.  
    6. public class NewBehaviourScript : MonoBehaviour
    7. {
    8.     public float speed;
    9.     Rigidbody2D rb;
    10.     float Xdir;
    11.  
    12.  
    13.     // Start is called before the first frame update
    14.     private void Awake()
    15.     {
    16.         rb= GetComponent<Rigidbody2D>();
    17.     }
    18.  
    19.     void Start()
    20.     {
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.         Xdir = Input.GetAxis("Horizontal");
    27.         if (Xdir > 0)
    28.         {
    29.             transform.eulerAngles = new Vector3(0, 0, 0);
    30.         }
    31.         else if (Xdir < 0)
    32.         {
    33.             transform.eulerAngles = new Vector3(0, 180, 0);
    34.         }
    35.        
    36.        
    37.         if(Input.GetKeyDown(KeyCode.Space))
    38.         {
    39.             float JumpSpeed = 65f;
    40.             rb.velocity = Vector2.up * JumpSpeed;
    41.         }
    42.     }
    43.  
    44.  
    45.     void Velocity(float dir)
    46.     {
    47.         rb.velocity = Vector2.right * dir * speed * Time.deltaTime;
    48.     }
    49.  
    50.     private void FixedUpdate()
    51.     {
    52.         Velocity(Xdir);
    53.  
    54.        
    55.     }
    56.  
    57.  
    58.  
    59.  
    60.  
    61. }
    62.  
    63.  
     
  2. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    660
    It looks to me like a simple case of you are overwriting your jump velocity every fixed update, which ofc, is where all the physics magic happens.
     
  3. CodeD306

    CodeD306

    Joined:
    Dec 22, 2022
    Posts:
    20
    so what should i do?
     
  4. CodeD306

    CodeD306

    Joined:
    Dec 22, 2022
    Posts:
    20
    bc when i delete the player movement the jump works
     
  5. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    660
    you should probably change the XDir float to a vector 3... use the Vector3.x value where you are using XDir now, and use the Vector3.y value in your jump detection check, assign the jump value to the vector3.y.

    If that still doesnt make sense, i will rearrange your code a bit for you. But its really rather simple.
     
  6. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    660
    Yea, because you overwriting it every fixed update.
     
  7. CodeD306

    CodeD306

    Joined:
    Dec 22, 2022
    Posts:
    20
    and in the function Velocity i change it too?
    bc i wrote Velocity(Xdir)
     
  8. CodeD306

    CodeD306

    Joined:
    Dec 22, 2022
    Posts:
    20
  9. CodeD306

    CodeD306

    Joined:
    Dec 22, 2022
    Posts:
    20
    i started using unity recently so i dont know a lot about coding
     
  10. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    660
    NP - be warned, that code will let you eternally jump. At no point are you checking to see if you are on a ground surface.
     
  11. CodeD306

    CodeD306

    Joined:
    Dec 22, 2022
    Posts:
    20
    i know that was the next thing that i was gonna put
     
  12. CodeD306

    CodeD306

    Joined:
    Dec 22, 2022
    Posts:
    20
    lemme try the code
     
  13. CodeD306

    CodeD306

    Joined:
    Dec 22, 2022
    Posts:
    20
    it says that i cant turn unityenginevector3 into a float
     
  14. CodeD306

    CodeD306

    Joined:
    Dec 22, 2022
    Posts:
    20
    did i mis something?
     
  15. CodeD306

    CodeD306

    Joined:
    Dec 22, 2022
    Posts:
    20
    theres in total 5 errors
     
  16. CodeD306

    CodeD306

    Joined:
    Dec 22, 2022
    Posts:
    20
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Numerics;
    4. using Unity.Collections;
    5. using UnityEngine;
    6.  
    7. public class NewBehaviourScript : MonoBehaviour
    8. {
    9.     public float speed;
    10.     Rigidbody2D rb;
    11.     Vector3 Xdir;
    12.  
    13.  
    14.     // Start is called before the first frame update
    15.     private void Awake()
    16.     {
    17.         rb= GetComponent<Rigidbody2D>();
    18.     }
    19.  
    20.     void Start()
    21.     {
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.  
    28.         Xdir = Vector3.zero;
    29.         Xdir.y = rb.velocity.y;
    30.         Xdir.x = Input.GetAxis("Horizontal");
    31.         if (Xdir.x > 0)
    32.         {
    33.             transform.eulerAngles = new Vector3(0, 0, 0);
    34.         }
    35.         else if (Xdir.x < 0)
    36.         {
    37.             transform.eulerAngles = new Vector3(0, 180, 0);
    38.         }
    39.        
    40.        
    41.         if(Input.GetKeyDown(KeyCode.Space))
    42.         {
    43.             float JumpSpeed = 65f;
    44.             Xdir.y = JumpSpeed;
    45.         }
    46.     }
    47.  
    48.  
    49.     void Velocity(float dir)
    50.     {
    51.         rb.velocity =  dir * speed * Time.deltaTime;
    52.     }
    53.  
    54.     private void FixedUpdate()
    55.     {
    56.         Velocity(Xdir);
    57.  
    58.        
    59.     }
    60.  
    61.  
    62.  
    63.  
    64.  
    65. }
    66.  
     
  17. CodeD306

    CodeD306

    Joined:
    Dec 22, 2022
    Posts:
    20
    did i write anything wrong?
     
  18. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    660
    Sorry , i missed that. Change your Velocity() method to take in a Vector3.

    Code (CSharp):
    1. void Velocity(Vector3 dir)
    2. {
    3.      ///etc...
    4. }
    JD#1539 Discord Me if you still having issues. Its just something simple and overlooked im sure.
     
  19. CodeD306

    CodeD306

    Joined:
    Dec 22, 2022
    Posts:
    20
    im sorry if im being anoying butit still sshow a couple errors
     
  20. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    660
    At a quick glance, there was a few typos.


    Code (CSharp):
    1. public class NewBehaviourScript : MonoBehaviour
    2.     {
    3.         public float speed;
    4.         Rigidbody2D rb;
    5.         Vector3 Xdir;
    6.         //float Xdir;
    7.  
    8.  
    9.         // Start is called before the first frame update
    10.         private void Awake()
    11.         {
    12.             rb = GetComponent<Rigidbody2D>();
    13.         }
    14.  
    15.         void Start()
    16.         {
    17.  
    18.         }
    19.  
    20.         // Update is called once per frame
    21.         void Update()
    22.         {
    23.             Xdir.y = rb.velocity.y;
    24.             Xdir.x = Input.GetAxis("Horizontal");
    25.             if (Xdir.x > 0)
    26.             {
    27.                 transform.eulerAngles = new Vector3(0, 0, 0);
    28.             }
    29.             else if (Xdir.x < 0)
    30.             {
    31.                 transform.eulerAngles = new Vector3(0, 180, 0);
    32.             }
    33.  
    34.  
    35.             if (Input.GetKeyDown(KeyCode.Space))
    36.             {
    37.                 float JumpSpeed = 65f;
    38.                 Xdir.y = JumpSpeed;
    39.             }
    40.         }
    41.         void Velocity(Vector3 dir)
    42.         {
    43.             rb.velocity = dir * speed * Time.deltaTime;
    44.         }
    45.         private void FixedUpdate()
    46.         {
    47.             Velocity(Xdir);
    48.         }
    49.     }

    Post the errors, if there is any.
     
  21. CodeD306

    CodeD306

    Joined:
    Dec 22, 2022
    Posts:
    20
    wht did u change?
     
  22. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    660
    the language is Case Sensitive. I just fixed the capitolization.
     
  23. CodeD306

    CodeD306

    Joined:
    Dec 22, 2022
    Posts:
    20
    srry but what doest that mean XD?
     
  24. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    660
    Well, i gave my Discord info in case you need a quick little one on one help. but it sounds as if you really should spend a little time going through some beginner c# lessons. Alot of this stuff is just elementary and core to anything you are going to do if you plan on writing any code what so ever.

    If you need further assistance, feel free to add me on Discord, but i truly do advise, dig into the language a little but before trying to dig into the engine to ,cuh. At least... if you plan on coding yourself.
     
  25. CodeD306

    CodeD306

    Joined:
    Dec 22, 2022
    Posts:
    20
  26. CodeD306

    CodeD306

    Joined:
    Dec 22, 2022
    Posts:
    20
    so wheres ur disc info?