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

Can you help me with this it is not working for my game I don't know what I happening I am new to c#

Discussion in '2D' started by Creator10124, Nov 14, 2022.

  1. Creator10124

    Creator10124

    Joined:
    Dec 2, 2021
    Posts:
    6
    using UnityEngine;

    public class move : MonoBehaviour
    {
    public float movespeed = 10f;
    Rigidbody2D rb;

    // Start is called before the first frame update
    void Start()
    {
    rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
    var movement = Input.GetAxisRaw("Horizontal");
    transform.position = new Vector2(movement, 0) * Time.fixedDeltaTime * movespeed;
    }
    }
     
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
  3. Creator10124

    Creator10124

    Joined:
    Dec 2, 2021
    Posts:
    6
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class move : MonoBehaviour
    4. {  
    5.     public float movespeed = 10f;
    6.     Rigidbody2D rb;
    7.  
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         rb = GetComponent<Rigidbody2D>();
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void FixedUpdate()
    16.     {
    17.         var movement = Input.GetAxisRaw("Horizontal");
    18.         transform.position = new Vector2(movement, 0) * Time.fixedDeltaTime * movespeed;
    19.     }
    20. }
    21.  
     
  4. Creator10124

    Creator10124

    Joined:
    Dec 2, 2021
    Posts:
    6
    IT WORKED
     
  5. Creator10124

    Creator10124

    Joined:
    Dec 2, 2021
    Posts:
    6
    Well the problem is it keeps floating slowly to the ground and I don't know why, and when it collides with the ground it teleports back to the position that is started with.
     
  6. DavidTeo

    DavidTeo

    Unity Technologies

    Joined:
    Apr 1, 2021
    Posts:
    69
  7. Creator10124

    Creator10124

    Joined:
    Dec 2, 2021
    Posts:
    6
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class move : MonoBehaviour
    4. {  
    5.     public float movespeed = 10f;
    6.     Rigidbody2D rb;
    7.  
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         rb = GetComponent<Rigidbody2D>();
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void FixedUpdate()
    16.     {
    17.         var movement = Input.GetAxisRaw("Horizontal");
    18.         rb.MovePosition();
    19.     }
    20. }
     
  8. Creator10124

    Creator10124

    Joined:
    Dec 2, 2021
    Posts:
    6
    how exactly would I use rb.MovePosition
     
  9. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    491
    The link that DavidTeo provided gives everything you need to use it. There is even an example on that page.
     
    Kurt-Dekker likes this.