Skip to content
Snippets Groups Projects
PlayerBehavior.cs 1.32 KiB
Newer Older
  • Learn to ignore specific revisions
  • using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    
    public class PlayerBehavior : MonoBehaviour {
    
        /* Movement to apply this frame */
    
        private Vector3 frameMovement;
        
        [SerializeField]
        private float acceleration;
        [SerializeField]
        private float brakeFraction;
    
        [SerializeField]
        private float maxSpeed;
    
    
        public GameObject player;
        private Rigidbody2D playerBody;
    
    
    
        public void MoveLeft()
        {
    
            frameMovement.x -= acceleration;
    
            frameMovement.x += acceleration;
    
            frameMovement.y += acceleration;
    
            frameMovement.y -= acceleration;
        }
    
        public void Brake()
        {
            playerBody.velocity *= brakeFraction;
    
        void Start () {
            playerBody = this.GetComponent<Rigidbody2D>();
    	}
    	
    	// Update is called once per frame
    
            playerBody.AddForce(Vector3.ClampMagnitude(frameMovement, acceleration));
    
            if(playerBody.velocity.magnitude > maxSpeed)
            {
                playerBody.velocity = (playerBody.velocity.normalized * maxSpeed);
            }
    
            frameMovement = Vector3.zero;
    	}
    }