(改良)ブロックの端からでもジャンプ可能にする

接地判定の改良
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class PlayerMovement : MonoBehaviour
{
public float speed;
public float moveH { get; set; }
private Rigidbody2D rb2d;
private Animator animator;
private SpriteRenderer spriteRenderer;
public float jumpSpeed;
public AudioClip jumpSound;
public LayerMask floor;
private bool isGard = false;
public BoxCollider2D guardCollider2D;
public TextMeshProUGUI sonicCountLabel;
private int sonicCount = 10;
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
spriteRenderer = GetComponent<SpriteRenderer>();
guardCollider2D.enabled = false;
sonicCountLabel.text = "" + sonicCount;
}
void Update()
{
if (isGard == false)
{
moveH = Input.GetAxisRaw("Horizontal");
rb2d.velocity = new Vector2(moveH * speed, rb2d.velocity.y);
animator.SetFloat("Speed", moveH);
}
if (moveH > 0.5f)
{
spriteRenderer.flipX = false;
}
else if (moveH < -0.5f)
{
spriteRenderer.flipX = true;
}
if (Input.GetKeyDown(KeyCode.Space) && IsGrounded())
{
rb2d.velocity = Vector2.up * jumpSpeed;
AudioSource.PlayClipAtPoint(jumpSound, Camera.main.transform.position);
animator.SetTrigger("Jump");
}
else if (Input.GetKeyDown(KeyCode.Z))
{
if (moveH != 0)
{
return;
}
animator.SetTrigger("Attack1");
}
else if (Input.GetKeyDown(KeyCode.X) && sonicCount > 0)
{
if (moveH != 0)
{
return;
}
sonicCount -= 1;
sonicCountLabel.text = "" + sonicCount;
animator.SetTrigger("Attack2");
}
if (Input.GetKeyDown(KeyCode.C))
{
if (moveH != 0)
{
return;
}
animator.SetBool("Guard", true);
isGard = true;
guardCollider2D.enabled = true;
}
else if (Input.GetKeyUp(KeyCode.C))
{
animator.SetBool("Guard", false);
isGard = false;
guardCollider2D.enabled = false;
}
}
private bool IsGrounded()
{
// ★改良(接地判定)
// BoxCastの活用
RaycastHit2D hit2d = Physics2D.BoxCast(transform.position, new Vector2(1f, 1f), 0f, Vector2.down, 0.1f, floor);
return hit2d.collider != null;
//RaycastHit2D hit2d = Physics2D.Raycast(transform.position, Vector2.down, 0.6f, floor);
//return hit2d.collider != null;
}
public void AddSonicCount(int amount)
{
sonicCount += amount;
sonicCountLabel.text = "" + sonicCount;
}
}
【2022版】DarkCastle(全39回)
他のコースを見る
接地判定の改良
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class PlayerMovement : MonoBehaviour
{
public float speed;
public float moveH { get; set; }
private Rigidbody2D rb2d;
private Animator animator;
private SpriteRenderer spriteRenderer;
public float jumpSpeed;
public AudioClip jumpSound;
public LayerMask floor;
private bool isGard = false;
public BoxCollider2D guardCollider2D;
public TextMeshProUGUI sonicCountLabel;
private int sonicCount = 10;
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
spriteRenderer = GetComponent<SpriteRenderer>();
guardCollider2D.enabled = false;
sonicCountLabel.text = "" + sonicCount;
}
void Update()
{
if (isGard == false)
{
moveH = Input.GetAxisRaw("Horizontal");
rb2d.velocity = new Vector2(moveH * speed, rb2d.velocity.y);
animator.SetFloat("Speed", moveH);
}
if (moveH > 0.5f)
{
spriteRenderer.flipX = false;
}
else if (moveH < -0.5f)
{
spriteRenderer.flipX = true;
}
if (Input.GetKeyDown(KeyCode.Space) && IsGrounded())
{
rb2d.velocity = Vector2.up * jumpSpeed;
AudioSource.PlayClipAtPoint(jumpSound, Camera.main.transform.position);
animator.SetTrigger("Jump");
}
else if (Input.GetKeyDown(KeyCode.Z))
{
if (moveH != 0)
{
return;
}
animator.SetTrigger("Attack1");
}
else if (Input.GetKeyDown(KeyCode.X) && sonicCount > 0)
{
if (moveH != 0)
{
return;
}
sonicCount -= 1;
sonicCountLabel.text = "" + sonicCount;
animator.SetTrigger("Attack2");
}
if (Input.GetKeyDown(KeyCode.C))
{
if (moveH != 0)
{
return;
}
animator.SetBool("Guard", true);
isGard = true;
guardCollider2D.enabled = true;
}
else if (Input.GetKeyUp(KeyCode.C))
{
animator.SetBool("Guard", false);
isGard = false;
guardCollider2D.enabled = false;
}
}
private bool IsGrounded()
{
// ★改良(接地判定)
// BoxCastの活用
RaycastHit2D hit2d = Physics2D.BoxCast(transform.position, new Vector2(1f, 1f), 0f, Vector2.down, 0.1f, floor);
return hit2d.collider != null;
//RaycastHit2D hit2d = Physics2D.Raycast(transform.position, Vector2.down, 0.6f, floor);
//return hit2d.collider != null;
}
public void AddSonicCount(int amount)
{
sonicCount += amount;
sonicCountLabel.text = "" + sonicCount;
}
}
(改良)ブロックの端からでもジャンプ可能にする