Last active
January 25, 2021 14:22
-
-
Save fhfhkiki/266d08fc891d855113c974e1625170cf to your computer and use it in GitHub Desktop.
unity_Practice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class player : MonoBehaviour | |
{ | |
Rigidbody2D rb; | |
Animator anim; | |
SpriteRenderer sr; | |
private int pressKey_cnt_jump; | |
private int pressKey_jump; | |
private int presskey_horizon; | |
[SerializeField] float runSpeed; // 走るスピード | |
[SerializeField] float jumpForce; // ジャンプ力 | |
[SerializeField] int jumpMaxCnt; // ジャンプ最大回数 | |
[SerializeField] int jumpCnt; // ジャンプ残り回数 | |
private bool isGround; // 接地してるか | |
// Start is called before the first frame update | |
void Start() | |
{ | |
rb = GetComponent<Rigidbody2D>(); | |
anim = GetComponent<Animator>(); | |
sr = GetComponent<SpriteRenderer>(); | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
inputCheck(); | |
groundCheck(); | |
animStateCheck(); | |
} | |
void inputCheck() | |
{ | |
if(Input.GetKey(KeyCode.Z)) | |
{ | |
pressKey_cnt_jump++; | |
} | |
else | |
{ | |
pressKey_cnt_jump = 0; | |
} | |
if(pressKey_cnt_jump==1) | |
{ | |
pressKey_jump++; | |
} | |
if(Input.GetKey(KeyCode.LeftArrow)) | |
{ | |
presskey_horizon = -1; | |
} | |
else if(Input.GetKey(KeyCode.RightArrow)) | |
{ | |
presskey_horizon = 1; | |
} | |
else | |
{ | |
presskey_horizon = 0; | |
} | |
} | |
void groundCheck() | |
{ | |
RaycastHit2D hit = Physics2D.Linecast(this.transform.position+ (Vector3.down * 15.1f) - (Vector3.right * 5f),this.transform.position + (Vector3.down * 15.1f) + (Vector3.right * 5f)); | |
Debug.DrawLine(this.transform.position+ (Vector3.down * 15.1f) - (Vector3.right * 5f),this.transform.position + (Vector3.down * 15.1f) + (Vector3.right * 5f)); | |
if(hit){ | |
isGround = true; | |
jumpCnt = jumpMaxCnt; | |
} | |
else | |
{ | |
//Debug.Log("iGf"); | |
isGround = false; | |
} | |
} | |
void animStateCheck() | |
{ | |
anim.SetBool("isSkip",false); | |
anim.SetBool("isIdle",false); | |
if(isGround){ | |
if(rb.velocity.x <0) | |
{ | |
anim.SetBool("isSkip",true); | |
sr.flipX = true; | |
} | |
else if(0 < rb.velocity.x) | |
{ | |
anim.SetBool("isSkip",true); | |
sr.flipX = false; | |
} | |
else { | |
anim.SetBool("isIdle",true); | |
} | |
} | |
} | |
void FixedUpdate() | |
{ | |
if(pressKey_jump>0) | |
{ | |
if(isGround) | |
{ | |
isGround = false; | |
rb.AddForce(new Vector2(0,jumpForce)); | |
jumpCnt--; | |
} | |
else | |
{ | |
if(jumpCnt>0) | |
{ | |
rb.velocity = new Vector2(rb.velocity.x, 0); | |
rb.AddForce(new Vector2(0,jumpForce)); | |
jumpCnt--; | |
} | |
} | |
pressKey_jump--; | |
} | |
if(presskey_horizon!=0){ | |
rb.velocity = new Vector2(presskey_horizon*runSpeed, rb.velocity.y); | |
} | |
else | |
{ | |
rb.velocity = new Vector2(0, rb.velocity.y); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment