Hi, I’m an absolute beginner and I want to get a grasp of Unity as a person with no background whatsoever in computing language.
I’m following the 2D tutorial on
but I am not concerned with the animations therefore I have omitted the lines related to the animation however with the followingcode I have created, I cannot make a character jump.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float jumpforce = 10f;
public float maxSpeed = 10f;
private Rigidbody2D rb;
bool grounded = false;
public Transform groundcheck;
public float groundRadius = 0.2f;
public LayerMask whatIsGround;
// Use this for initialization
void Start()
{
rb = GetComponent<Rigidbody2D>();
CharacterController controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
if (grounded && Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(new Vector2(0, jumpforce));
}
}
void FixedUpdate()
{
grounded = Physics2D.OverlapCircle(groundcheck.position, groundRadius, whatIsGround);
float move = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(move * maxSpeed, rb.velocity.y);
}
}
Here is a reference screenshot of my work,