0

I am new to coding and c#, but I have been trying to make my player without gravity affecting it. But my player keeps moving up and doing nothing else. I have started by trying to make it move to the left and right. I am not sure where I went wrong or if I should start over as I am not to far in or if I should fix a small mistake.

Here is my code:

     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;

     public class PlayerMovement1 : MonoBehaviour
     {
     private float speed;
     // Start is called before the first frame update
     void Start()
     {
        
     }

     // Update is called once per frame
     void Update()
     {
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");

        Vector2 direction = new Vector2(horizontalInput, verticalInput);

        transform.Translate(direction * speed * Time.deltaTime);
     }
     }
1
  • You should initialize the speed parameter with a real number, otherwise the transform.translate function will not work. Did you scale the gravity down to 0 for the rigid body? Commented Jul 7 at 22:09

2 Answers 2

2

you are transferring the object that PlayerMovement1 script is attached. how is a gravity affecting your object? with rigidbody? if you have a gravity with rigidbody, the "translate" doesn't care it and move anyway but it doesn't recommended for most cases because in this way the object can't detect the collisions and if you have rigidbody at the same time, you get weird movements and shakings. you should move your object with the rigidbody instead of transferring your object with transform. To do this; you should get the rigidbody from the inspector menu. write that code and assign the rigidbody:

[SerialiseField] Rigidbody2D rigidbody;

now you can assign the rigidbody. Just change your rigidbody's velocity and move the object in the update method (FixedUpdate for better movement but be careful because it can't detect the "input.getkey" methods everytime):

float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");

Vector2 direction = new Vector2(horizontalInput, verticalInput);
rigidbody.velocity = direction * speed;

Lastly, don't forget to disable the gravity at the start method(as I understand from your message you want to do it):

rigidbody.gravityScale = 0;

Also, is your game 2d or 3d?

14
  • Thank you for your feedback! I am about to try it out. I will let you know if it fixes the problem! Also my game is 2D since I have heard that will be easier! Commented Jul 7 at 23:04
  • your wellcome ^^ btw, as unity developer for 4 years, i think there is no difference starting at 3d or 2d about hardness
    – mizi
    Commented Jul 7 at 23:07
  • Edit: As I was trying this out for myself I got the error, "Assets\PlayerMovement1.cs(24,14): error CS1061: 'Rigidbody2D' does not contain a definition for 'useGravity' and no accessible extension method 'useGravity' accepting a first argument of type 'Rigidbody2D' could be found (are you missing a using directive or an assembly reference?)" Could you help me understand this? If not I can try another way! Edit 2; It also was saying that the [SerialisedField] wasn't a thing, I am not sure if you know anything about that? Also thats cool that you were a developer for 4 years! Commented Jul 7 at 23:18
  • oh i am sorry. since i am working mostly on 3d, i didn't know that the rigidbody2d has gravityScale instead. i just found it by searching "rigidbody2d gravity" in google. i suggest you to search this kind of things and take a look at the "unity scripting api" for your future problems ^^ also you should write "SerialiseField", not the "SerialisedField". I updated my answer with the right code. I hope this helps ^^
    – mizi
    Commented Jul 7 at 23:28
  • This has been so helpful! The only problem I have run into is that even the "SerialiseField" will not work. I am thinking I might be placing it in the wrong area, should it be placed after the first bracket? Edit: I was able to figure it out! But when I try to play the game, to see how it works, it gives me the error, NullReferenceException: Object reference not set to an instance of an object PlayerMovement1.Update () (at Assets/PlayerMovement1.cs:22) Commented Jul 8 at 0:15
0
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement1 : MonoBehaviour
{
    public float speed = 5f; // Initialize the speed variable and make it public
    private Rigidbody2D rb; // If it is a 2D platformer game. 

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        if (rb != null)
        {
            rb.gravityScale = 0; // Disable gravity effect on the player
        }
    }


    void Update()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        Vector2 direction = new Vector2(horizontalInput, 0); // Only use horizontal input for now. 

        transform.Translate(direction * speed * Time.deltaTime);
    }
}

Please let me know in the comments if it works. Because I am not sure how does the speed parameter helps here when it is not initialized.

Not the answer you're looking for? Browse other questions tagged or ask your own question.