ads

Moving a 2D Game Character in Unity



 Here is some example C# code for a simple player character movement in a 2D platformer game:

using UnityEngine;

public class PlayerMovement : MonoBehaviour { public float speed = 6f; private Vector3 movement; private Rigidbody playerRigidbody; private int floorMask; private float camRayLength = 100f; void Awake () { floorMask = LayerMask.GetMask ("Floor"); playerRigidbody = GetComponent <Rigidbody> (); } void FixedUpdate () { float h = Input.GetAxisRaw("Horizontal"); float v = Input.GetAxisRaw("Vertical"); Move (h, v); } void Move (float h, float v) { movement.Set (h, 0f, v); movement = movement.normalized * speed * Time.deltaTime; playerRigidbody.MovePosition (transform.position + movement); } }

About Admin

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 yorum :

Yorum Gönder