using System.Collections;using System.Collections.Generic;using UnityEngine;
public class Player2DController : MonoBehaviour{ public float jumpForce = 200, moveForce = 10, moveLimit = 2; public GameObject aimer; Rigidbody rb; bool onGround = false; float jumpCD = 0;
// Start is called before the first frame update void Start() { rb = this.GetComponent<Rigidbody>(); }
// Update is called once per frame void LateUpdate() { if(jumpCD > 0) { jumpCD -= Time.deltaTime; } else { jumpCD = 0; } rb.WakeUp();
var pvel = rb.velocity; pvel.x = Mathf.Abs(pvel.x); pvel.y = Mathf.Abs(pvel.y); if (Input.GetKey(KeyCode.A)) { if(pvel.x < moveLimit) { rb.AddForce(-Vector3.right * moveForce, ForceMode.Force); } } if (Input.GetKey(KeyCode.D)) { if (pvel.x < moveLimit) { rb.AddForce(Vector3.right * moveForce, ForceMode.Force); } } if (Input.GetKey(KeyCode.W) && onGround && jumpCD == 0) { rb.AddForce(Vector3.up * jumpForce, ForceMode.Force); jumpCD += 1; }
var mousepos = Input.mousePosition; Vector3 v3 = new Vector3(mousepos.x - Screen.width / 2, mousepos.y - Screen.height / 2, 0); aimer.transform.rotation = Quaternion.identity; aimer.transform.LookAt(this.transform.position + v3);
onGround = false; }
private void OnCollisionStay(Collision collision) { if(collision.gameObject.transform.position.y < this.transform.position.y && collision.contacts[0].point.y < this.transform.position.y - 0.1f) { onGround = true; } }}