UnityScripterAI
Discord ↗
Link Copied to Clipboard
×
"2d platformer character controller"
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CharacterController2D : MonoBehaviour { public float moveSpeed = 5f; public float jumpForce = 10f; public LayerMask groundLayer; private Rigidbody2D rb; private bool isGrounded; private float horizontalMove; // Start is called before the first frame update void Start() { rb = GetComponent<Rigidbody2D>(); } // Update is called once per frame void Update() { horizontalMove = Input.GetAxisRaw("Horizontal") * moveSpeed; isGrounded = Physics2D.OverlapCircle(transform.position, 0.1f, groundLayer); if (Input.GetButtonDown("Jump") && isGrounded) { rb.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse); } } void FixedUpdate() { rb.velocity = new Vector2(horizontalMove, rb.velocity.y); } }