- Home /
I'm making a character controller but can't figure out why jumping wont work.
The script (c# in visual studio) using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour {
public float speed = 1; public float jumpForce = 1; private Rigidbody2D _rb; void Start() { _rb = GetComponent(); }
void Update()
{
var movement = Input.GetAxis("Horizontal");
transform.position += new Vector3(movement, 0, 0) * Time.deltaTime * speed;
if(Input.GetKeyDown)KeyCode.Space) && Mathf.Abs(_rb.velocity.y) < 0.001f)
{
_rb.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
}
}
}
Answer by rage_co · Jul 06, 2021 at 03:43 AM
It's a very simple mistake....it's not Input.GetKeyDown)KeyCode.Space).....it's Input.GetKeyDown(KeyCode.Space).....as you can see...you have used a flipped bracket there.....hope this helps
Yeah this is most likely why. just replace if(Input.GetKeyDown)KeyCode.Space) && Mathf.Abs(_rb.velocity.y) < 0.001f) { _rb.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse); }
to if(Input.GetKeyDown(KeyCode.Space) && Mathf.Abs(_rb.velocity.y) < 0.001f) { _rb.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse); }
Your answer
Follow this Question
Related Questions
How to move a gameobject towards a UI element onscreen. 0 Answers
2d shooting problem 1 Answer
2D Detect collisions of a 2D block only on left/right (not top/bottom) 0 Answers
How to set axes for mobile controlling? 0 Answers
What are the formulas for adding an "X" amount of enemies, at each "n" point or distance? 0 Answers