FirstComit
This commit is contained in:
20
Assets/Script/BushBehabior.cs
Normal file
20
Assets/Script/BushBehabior.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
|
||||
public class BushBehabior : MonoBehaviour
|
||||
{
|
||||
|
||||
[SerializeField] Transform core;
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
2
Assets/Script/BushBehabior.cs.meta
Normal file
2
Assets/Script/BushBehabior.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ba54f7d439e6c45729b6c98972cb98c2
|
||||
52
Assets/Script/BushBehavior.cs
Normal file
52
Assets/Script/BushBehavior.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class BushBehavior : MonoBehaviour
|
||||
{
|
||||
[SerializeField] Transform core;
|
||||
[SerializeField] float radius = 0.1f;
|
||||
[SerializeField] Animator animator;
|
||||
|
||||
static readonly int TouchHash = Animator.StringToHash("Touch");
|
||||
|
||||
SlimeWalk[] slimes;
|
||||
bool[] wasInside;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (core == null) core = transform;
|
||||
if (animator == null) animator = GetComponent<Animator>();
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
slimes = Object.FindObjectsByType<SlimeWalk>(FindObjectsSortMode.None);
|
||||
wasInside = new bool[slimes.Length];
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (animator == null || core == null || slimes == null) return;
|
||||
|
||||
Vector2 c = core.position;
|
||||
float r2 = radius * radius;
|
||||
|
||||
for (int i = 0; i < slimes.Length; i++)
|
||||
{
|
||||
if (slimes[i] == null) continue;
|
||||
Vector2 p = slimes[i].transform.position;
|
||||
bool inside = (p - c).sqrMagnitude <= r2;
|
||||
if (inside && !wasInside[i])
|
||||
{
|
||||
animator.SetTrigger(TouchHash);
|
||||
}
|
||||
wasInside[i] = inside;
|
||||
}
|
||||
}
|
||||
|
||||
void OnDrawGizmosSelected()
|
||||
{
|
||||
Gizmos.color = Color.yellow;
|
||||
Vector3 c = core != null ? core.position : transform.position;
|
||||
Gizmos.DrawWireSphere(c, radius);
|
||||
}
|
||||
}
|
||||
2
Assets/Script/BushBehavior.cs.meta
Normal file
2
Assets/Script/BushBehavior.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8515b56f7f93a47e0a2c8fa229923a36
|
||||
11
Assets/Script/Score.cs
Normal file
11
Assets/Script/Score.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
public class Score : SingletonMonoBehavior<Score>
|
||||
{
|
||||
public int Cnt = 0;
|
||||
|
||||
public void CntUp()
|
||||
{
|
||||
Cnt++;
|
||||
}
|
||||
|
||||
public string MyName;
|
||||
}
|
||||
2
Assets/Script/Score.cs.meta
Normal file
2
Assets/Script/Score.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 402257d4597fe4c2f92f6bca74b21c83
|
||||
35
Assets/Script/SingletonMonoBehavior.cs
Normal file
35
Assets/Script/SingletonMonoBehavior.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using UnityEngine;
|
||||
|
||||
public abstract class SingletonMonoBehavior<T> : MonoBehaviour where T : SingletonMonoBehavior<T>
|
||||
{
|
||||
private static T _instance;
|
||||
|
||||
public static T Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_instance == null)
|
||||
{
|
||||
_instance = FindFirstObjectByType<T>();
|
||||
if (_instance == null)
|
||||
{
|
||||
var go = new GameObject(typeof(T).Name);
|
||||
_instance = go.AddComponent<T>();
|
||||
}
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void Awake()
|
||||
{
|
||||
if (_instance != null && _instance != this)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
|
||||
_instance = (T)this;
|
||||
DontDestroyOnLoad(gameObject);
|
||||
}
|
||||
}
|
||||
2
Assets/Script/SingletonMonoBehavior.cs.meta
Normal file
2
Assets/Script/SingletonMonoBehavior.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7573d3a9c9d5e437b83b4a712fa1c95b
|
||||
87
Assets/Script/SlimeWalk.cs
Normal file
87
Assets/Script/SlimeWalk.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
public class SlimeWalk : MonoBehaviour
|
||||
{
|
||||
[SerializeField] float moveSpeed = 3f;
|
||||
[SerializeField] float jumpHeight = 1f;
|
||||
[SerializeField] float jumpDuration = 0.4f;
|
||||
[SerializeField] Animator animator;
|
||||
|
||||
static readonly int UpHash = Animator.StringToHash("Up");
|
||||
static readonly int DownHash = Animator.StringToHash("Down");
|
||||
static readonly int LeftHash = Animator.StringToHash("Left");
|
||||
static readonly int RightHash = Animator.StringToHash("Right");
|
||||
static readonly int IdleHash = Animator.StringToHash("Idle");
|
||||
static readonly int JumpHash = Animator.StringToHash("Jump");
|
||||
|
||||
enum Dir { Idle, Up, Down, Left, Right }
|
||||
Dir lastDir = Dir.Idle;
|
||||
float jumpOffset;
|
||||
bool isJumping;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (animator == null) animator = GetComponent<Animator>();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
float x = 0f;
|
||||
float y = 0f;
|
||||
if (Input.GetKey(KeyCode.A)) x -= 1f;
|
||||
if (Input.GetKey(KeyCode.D)) x += 1f;
|
||||
if (Input.GetKey(KeyCode.W)) y += 1f;
|
||||
if (Input.GetKey(KeyCode.S)) y -= 1f;
|
||||
|
||||
Vector3 move = new Vector3(x, y, 0f);
|
||||
if (move.sqrMagnitude > 1f) move.Normalize();
|
||||
transform.position += move * moveSpeed * Time.deltaTime;
|
||||
|
||||
UpdateAnimation(x, y);
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.Space) && !isJumping)
|
||||
{
|
||||
if (animator != null) animator.SetTrigger(JumpHash);
|
||||
StartCoroutine(Jump());
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateAnimation(float x, float y)
|
||||
{
|
||||
if (animator == null) return;
|
||||
Dir newDir;
|
||||
if (x == 0f && y == 0f) newDir = Dir.Idle;
|
||||
else if (Mathf.Abs(x) >= Mathf.Abs(y)) newDir = x > 0f ? Dir.Right : Dir.Left;
|
||||
else newDir = y > 0f ? Dir.Up : Dir.Down;
|
||||
|
||||
if (newDir == lastDir) return;
|
||||
lastDir = newDir;
|
||||
switch (newDir)
|
||||
{
|
||||
case Dir.Up: animator.SetTrigger(UpHash); break;
|
||||
case Dir.Down: animator.SetTrigger(DownHash); break;
|
||||
case Dir.Left: animator.SetTrigger(LeftHash); break;
|
||||
case Dir.Right: animator.SetTrigger(RightHash); break;
|
||||
default: animator.SetTrigger(IdleHash); break;
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator Jump()
|
||||
{
|
||||
isJumping = true;
|
||||
float t = 0f;
|
||||
while (t < jumpDuration)
|
||||
{
|
||||
t += Time.deltaTime;
|
||||
float n = Mathf.Clamp01(t / jumpDuration);
|
||||
float newOffset = 4f * jumpHeight * n * (1f - n);
|
||||
transform.position += new Vector3(0f, newOffset - jumpOffset, 0f);
|
||||
jumpOffset = newOffset;
|
||||
yield return null;
|
||||
}
|
||||
transform.position -= new Vector3(0f, jumpOffset, 0f);
|
||||
jumpOffset = 0f;
|
||||
isJumping = false;
|
||||
}
|
||||
}
|
||||
2
Assets/Script/SlimeWalk.cs.meta
Normal file
2
Assets/Script/SlimeWalk.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e5eb6616d981d495ca496de6db75bcc4
|
||||
24
Assets/Script/SortByY.cs
Normal file
24
Assets/Script/SortByY.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(SpriteRenderer))]
|
||||
[ExecuteAlways]
|
||||
public class SortByY : MonoBehaviour
|
||||
{
|
||||
[SerializeField] Transform anchor;
|
||||
[SerializeField] int factor = 100;
|
||||
|
||||
SpriteRenderer sr;
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
sr = GetComponent<SpriteRenderer>();
|
||||
if (anchor == null) anchor = transform;
|
||||
}
|
||||
|
||||
void LateUpdate()
|
||||
{
|
||||
if (sr == null) sr = GetComponent<SpriteRenderer>();
|
||||
if (anchor == null) anchor = transform;
|
||||
sr.sortingOrder = -Mathf.RoundToInt(anchor.position.y * factor);
|
||||
}
|
||||
}
|
||||
2
Assets/Script/SortByY.cs.meta
Normal file
2
Assets/Script/SortByY.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 39e12bfa589b24515a2e4d70cfb71a85
|
||||
14
Assets/Script/TestMain.cs
Normal file
14
Assets/Script/TestMain.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class TestMain : MonoBehaviour
|
||||
{
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
Score.Instance.CntUp();
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Script/TestMain.cs.meta
Normal file
2
Assets/Script/TestMain.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2ca75d25ccb6e42fc86a6100be8f8137
|
||||
Reference in New Issue
Block a user