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

Help me

Discussion in '2D' started by josiah777, Jul 12, 2022.

  1. josiah777

    josiah777

    Joined:
    Jul 12, 2022
    Posts:
    2
    I've been working on this game for a week now and I still have the same problem in the movement script.
    Whenever I boost the jump force my character just instantly fly's in the air


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class character : MonoBehaviour
    5. {
    6. public float MovementSpeed = 1;
    7. public float JumpForce = 1;
    8. private Rigidbody2D _rigidbody;
    9. private void Start()
    10. {
    11. _rigidbody = GetComponent<Rigidbody2D>();
    12. }
    13. private void Update()
    14. {
    15. var movement = Input.GetAxis("Horizontal");
    16. transform.position += new Vector3(movement, 0, 0) * Time.deltaTime * MovementSpeed;
    17. if (Input.GetButtonDown("Jump") && Mathf.Abs(_rigidbody.velocity.y) < 0.001f)
    18. {
    19. }
    20. _rigidbody.AddForce(new Vector2(1, JumpForce), ForceMode2D.Impulse);
    21. }
    22. }
     
    Last edited: Jul 12, 2022
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.

    Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.

    https://forum.unity.com/threads/col...-unity-physic-rigidbody.1216875/#post-7763061

    https://forum.unity.com/threads/oncollisionenter2d-not-being-called.1266563/#post-8044121

    Are you trying to learn how to write a player controller or do you just want one? There's plenty on Youtube and here's two more:

    CharacterController CharMover broken:

    I wrote about this before: the Unity example code in the API no longer jumps reliably.

    I reported it to Unity via their docs feedback in October 2020. Apparently it is still broken:

    https://docs.unity3d.com/ScriptReference/CharacterController.Move.html

    Here is a work-around:

    https://forum.unity.com/threads/how...racter-movement-in-unity.981939/#post-6379746

    I recommend you also go to that same documentation page and ALSO report that the code is broken.

    When you report it, you are welcome to link the above workaround. One day the docs might get fixed.

    If you would prefer something more full-featured here is a super-basic starter prototype FPS based on Character Controller (BasicFPCC):

    https://forum.unity.com/threads/a-basic-first-person-character-controller-for-prototyping.1169491/

    That one has run, walk, jump, slide, crouch... it's crazy-nutty!!
     
  3. josiah777

    josiah777

    Joined:
    Jul 12, 2022
    Posts:
    2
    Sorry im new to coding is there any easier way?
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,621
    Yes, following tutorials on how to use these systems. TBH It's impossible to answer such a question on a forum; this is where docs and tutorials come in.
     
    Kurt-Dekker likes this.
  5. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,353
    MelvMay likes this.