A
A
Akly2020-07-19 13:54:10
C++ / C#
Akly, 2020-07-19 13:54:10

What is the correct way to code jump for a character in Unity?

Unity started throwing an error after adding the code for the player to jump. I can't find any errors.
-------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehavior
{

public float speed;
private RigidBody2D rb;

void Start()
{
rb = GetComponent();
}

void FixedUpdate()
{
transform.Translate(direction.normalized * speed);
}

void Update()
{
if (Input.GetButtonDown(KeyCode.Space))
rb.AddForce(Vector2.up * 1000);
}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zZaKko, 2020-07-19
@zZaKko

uh... rb = GetComponent... what is GetComponent? What are you taking? You should take Rigidbody/Rigidboyd2D.
rb = GetComponent<Rigidbody2D>();
ps first check the code, and then go to the forum)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question