B
B
bolnovnoge2019-06-21 09:26:05
C++ / C#
bolnovnoge, 2019-06-21 09:26:05

Why doesn't moving an object work in Unity?

There is an object (player) with Rigidbody2d, BoxCollider2d on it, there is another object (platform) with BoxCollider2d on it. The following code hangs on the player: Why doesn't the player jump when pressing the space bar? Is there a bug in the code or something in the editor is off? help me please

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
 
public class Move : MonoBehaviour 
{ 
 Rigidbody2D rb; 
 
 void Start() 
 { 
  rb = GetComponent<Rigidbody2D> (); 
 } 
 
 void Update() 
 { 
 if (Input.GetKeyDown (KeyCode.Space)) 
  { 
   jump(); 
  } 
 } 
 
 void jump() 
 { 
  rb.AddForce(transform.up * 14f, ForceMode2D.Impulse); 
 } 
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DIEMONIC, 2019-06-30
@DIEMONIC

Try to poke the Rigidbody2d settings - quite by accident, physics can be turned off there altogether or specifically gravity physics. If it doesn’t help, find a standard unity asset with a 2d robot (inside the unit, right-click in the assets - package import - characters (characters)), the movement is fully implemented there (but unfortunately, it’s made too abstruse and it can be hard to disassemble it, but it works )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question