• 【开发日志】2022.11.09 Unity自制小游戏HappyRabbit


    GitHub - Endlessdaydream/Endless_Unity_Projects: Unity Projects of EndlessdaydramUnity Projects of Endlessdaydram. Contribute to Endlessdaydream/Endless_Unity_Projects development by creating an account on GitHub.https://github.com/Endlessdaydream/Endless_Unity_Projects

    (37) How to make a 2D Bow and Arrow with UNITY & C#! - YouTubeicon-default.png?t=M85Bhttps://www.youtube.com/watch?app=desktop&v=tNwLaGUJTK4

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class PlayerController : MonoBehaviour
    5. {
    6. PlayerCharacter player;
    7. private void Start()
    8. {
    9. player = GetComponent();
    10. }
    11. private void Update()
    12. {
    13. if (Input.GetKeyDown(KeyCode.Tab))
    14. player.TrySwitch();
    15. if (Input.GetMouseButton(1))
    16. player.TryBreak();
    17. if (Input.GetMouseButton(0))
    18. player.PrepareShoot();
    19. if (Input.GetMouseButtonUp(0))
    20. player.Shoot();
    21. player.Move(Input.GetAxisRaw("Horizontal"));
    22. if (Input.GetButtonDown("Jump"))
    23. player.Jump();
    24. player.updateAnim();
    25. }
    26. }
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class PlayerCharacter : MonoBehaviour
    5. {
    6. public int hp = 1;
    7. public float speed = 4;
    8. public float jumpSpeed = 14;
    9. public float minShootSpeed = 5f, maxShootSpeed = 24f;
    10. public Transform shootPoint;
    11. public Transform flagPoint;
    12. public List arrowsPrefab;
    13. public float maxStoreTime = 2f;
    14. int arrowId = 0;
    15. int curArrowId = -1;
    16. Transform curArrow;
    17. float shootSpeed;
    18. float curStoreTime = 0;
    19. Transform[] points;
    20. Rigidbody2D r;
    21. Animator anim;
    22. bool isGround = false;
    23. bool isAim = false;
    24. bool isStopShoot = false;
    25. Transform foot1;
    26. Transform foot2;
    27. Transform center;
    28. LineRenderer line;
    29. SpriteRenderer render;
    30. bool cuphit = false;
    31. Transform gameOverPanel;
    32. Transform completePanel;
    33. public static PlayerCharacter Instance { get; private set; }
    34. private void Awake()
    35. {
    36. Instance = this;
    37. }
    38. private void Start()
    39. {
    40. render = GetComponent();
    41. r = GetComponent();
    42. anim = GetComponent();
    43. points = new Transform[30];
    44. for(int i=0;i
    45. {
    46. points[i] = Instantiate(flagPoint, transform);
    47. var render = points[i].GetComponent();
    48. var temp = render.color;
    49. temp.a *= (float)(points.Length - i) / points.Length;
    50. render.color = temp;
    51. }
    52. ShowPoints(false);
    53. foot1 = transform.Find("foot1");
    54. foot2 = transform.Find("foot2");
    55. center = transform.Find("center");
    56. line = GetComponent();
    57. GameObject canvas = GameObject.Find("Canvas");
    58. gameOverPanel = canvas.transform.Find("GameOverPanel");
    59. gameOverPanel.gameObject.SetActive(false);
    60. completePanel = canvas.transform.Find("CompletePanel");
    61. completePanel.gameObject.SetActive(false);
    62. }
    63. private void FixedUpdate()
    64. {
    65. isAim = false;
    66. isGround = false;
    67. LayerMask layerMask = ~(1 << 7);
    68. if (Physics2D.Raycast(foot1.position, Vector2.down, 0.25f, layerMask) || Physics2D.Raycast(foot2.position, Vector2.down, 0.25f, layerMask))
    69. {
    70. isGround = true;
    71. }
    72. line.enabled = false;
    73. if (curArrowId == 1 && curArrow)
    74. {
    75. isStopShoot = true;
    76. line.SetPosition(0, center.position);
    77. line.SetPosition(1, curArrow.position);
    78. line.enabled = true;
    79. var dir = (curArrow.position - center.position).normalized;
    80. Transform linkObj = curArrow.parent;
    81. if (linkObj != null)
    82. {
    83. if(linkObj.GetComponent())
    84. {
    85. Rigidbody2D otherR = linkObj.GetComponent();
    86. otherR.AddForce(-dir * 20f);
    87. }
    88. else
    89. {
    90. r.AddForce(dir * 65f);
    91. }
    92. }
    93. }
    94. }
    95. public void PrepareShoot()
    96. {
    97. if (isStopShoot) return;
    98. curArrowId = arrowId;
    99. curArrow = null;
    100. curStoreTime += Time.deltaTime;
    101. shootSpeed = minShootSpeed + (maxShootSpeed - minShootSpeed) * (Mathf.Min(curStoreTime, maxStoreTime) / maxStoreTime);
    102. isAim = true;
    103. if (isGround) r.velocity = new Vector2(0, r.velocity.y);
    104. DrawDir();
    105. }
    106. public void Shoot()
    107. {
    108. if (isStopShoot) return;
    109. curStoreTime = 0;
    110. ShowPoints(false);
    111. curArrow = Instantiate(arrowsPrefab[curArrowId], shootPoint.position, shootPoint.rotation);
    112. curArrow.GetComponent().velocity = shootSpeed * curArrow.right;
    113. }
    114. public void TryBreak()
    115. {
    116. if (isStopShoot)
    117. {
    118. isStopShoot = false;
    119. Destroy(curArrow.gameObject);
    120. curArrow = null;
    121. }
    122. }
    123. public void TrySwitch()
    124. {
    125. if (isStopShoot) return;
    126. arrowId ^= 1;
    127. }
    128. void DrawDir()
    129. {
    130. ShowPoints(true);
    131. float time = 0.5f;
    132. float deltaTime = time / points.Length;
    133. float t = 0;
    134. Vector2 v0 = shootPoint.right * shootSpeed;
    135. Vector2 start = shootPoint.position;
    136. for (int i = 0; i < points.Length; i++)
    137. {
    138. t += deltaTime;
    139. points[i].position = start + v0 * t + 0.5f * Physics2D.gravity * arrowsPrefab[curArrowId].GetComponent().gravityScale * t * t;
    140. }
    141. }
    142. void ShowPoints(bool isShow)
    143. {
    144. for (int i = 0; i < points.Length; i++)
    145. points[i].gameObject.SetActive(isShow);
    146. }
    147. public void Move(float h)
    148. {
    149. if (Mathf.Abs(h) > 0.1f)
    150. transform.right = h < 0 ? Vector2.left : Vector2.right;
    151. if (isAim) return;
    152. if(isGround)
    153. {
    154. r.velocity = new Vector2(h * speed, r.velocity.y);
    155. }
    156. else
    157. {
    158. r.AddForce(new Vector2(h * 30 * Time.deltaTime / Time.fixedDeltaTime, 0));
    159. r.velocity = new Vector2(Mathf.Clamp(r.velocity.x, -speed, speed), r.velocity.y);
    160. }
    161. }
    162. public void Jump()
    163. {
    164. if (!isGround||isAim) return;
    165. r.velocity = new Vector2(r.velocity.x, jumpSpeed);
    166. }
    167. public void updateAnim()
    168. {
    169. anim.SetBool("isWalk", Mathf.Abs(r.velocity.x) > 0.01f);
    170. anim.SetBool("isGround", isGround);
    171. anim.SetBool("isFall", r.velocity.y < 0.01f);
    172. anim.SetBool("isAim", isAim);
    173. }
    174. public void beHurt()
    175. {
    176. if (--hp <= 0)
    177. {
    178. Destroy(gameObject);
    179. gameOverPanel.gameObject.SetActive(true);
    180. }
    181. else
    182. StartCoroutine(TempTurnRed());
    183. }
    184. public void Win()
    185. {
    186. completePanel.gameObject.SetActive(true);
    187. }
    188. IEnumerator TempTurnRed()
    189. {
    190. render.color = Color.red;
    191. yield return new WaitForSeconds(0.1f);
    192. render.color = Color.white;
    193. }
    194. }
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class Bow : MonoBehaviour
    5. {
    6. private void LateUpdate()
    7. {
    8. Vector2 dir = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    9. transform.right = dir;
    10. }
    11. }
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class ArrowBase : MonoBehaviour
    5. {
    6. protected Rigidbody2D r;
    7. public Transform hitObj = null;
    8. private void Start()
    9. {
    10. r = GetComponent();
    11. }
    12. private void OnTriggerEnter2D(Collider2D collision)
    13. {
    14. if (transform.parent) return;
    15. r.velocity = Vector2.zero;
    16. r.isKinematic = true;
    17. transform.parent = collision.transform;
    18. }
    19. private void Update()
    20. {
    21. if (transform.parent) return;
    22. float angle = Mathf.Atan2(r.velocity.y, r.velocity.x) * Mathf.Rad2Deg;
    23. transform.rotation = Quaternion.Euler(0, 0, angle);
    24. }
    25. }
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class Bee : MonsterBase
    5. {
    6. public Rigidbody2D bulletPrefab;
    7. Transform shootPoint;
    8. Rect range;
    9. bool isOpenAttack = false;
    10. private new void Start()
    11. {
    12. base.Start();
    13. var bound = transform.parent.Find("Bound");
    14. shootPoint = transform.Find("ShootPoint");
    15. range.size = bound.localScale;
    16. range.center = bound.position;
    17. StartCoroutine(RandomMove(RandomTarget()));
    18. }
    19. private new void Update()
    20. {
    21. base.Update();
    22. if(isVigilant&&!isOpenAttack&&!isDead)
    23. {
    24. isOpenAttack = true;
    25. StartCoroutine(AutoAttack());
    26. }
    27. }
    28. IEnumerator RandomMove(Vector2 target)
    29. {
    30. while(this)
    31. {
    32. while (Vector2.Distance(transform.position, target) > 0.1f && !isDead)
    33. {
    34. if (isVigilant && player)
    35. {
    36. transform.right = player.position.x < transform.position.x ? Vector2.right : Vector2.left;
    37. }
    38. else
    39. {
    40. transform.right = target.x < transform.position.x ? Vector2.right : Vector2.left;
    41. }
    42. transform.position = Vector2.MoveTowards(transform.position, target, 3f * Time.deltaTime);
    43. yield return null;
    44. }
    45. yield return new WaitForSeconds(1f);
    46. target = RandomTarget();
    47. }
    48. }
    49. IEnumerator AutoAttack()
    50. {
    51. while(this&&player&&!isDead)
    52. {
    53. var r = Instantiate(bulletPrefab, shootPoint.position, Quaternion.identity);
    54. r.velocity = (player.position - r.transform.position).normalized * 2f;
    55. yield return new WaitForSeconds(3f);
    56. }
    57. }
    58. Vector2 RandomTarget()
    59. {
    60. return new Vector2(Random.Range(range.xMin, range.xMax), Random.Range(range.yMin, range.yMax));
    61. }
    62. }
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class Flower : MonsterBase
    5. {
    6. bool stopMove = false;
    7. Rigidbody2D r;
    8. private void FixedUpdate()
    9. {
    10. LayerMask layerMask = 1 << 7;
    11. if (Physics2D.Raycast(transform.position-new Vector3(0,0.3f), transform.right, 0.7f, layerMask))
    12. {
    13. anim.SetTrigger("attack");
    14. stopMove = true;
    15. r.velocity = Vector2.zero;
    16. }
    17. }
    18. public void Attack()
    19. {
    20. LayerMask layerMask = 1 << 7;
    21. var hitInfo = Physics2D.Raycast(transform.position - new Vector3(0, 0.3f), transform.right, 0.8f, layerMask);
    22. if (hitInfo)
    23. {
    24. hitInfo.transform.GetComponent().beHurt();
    25. }
    26. stopMove = false;
    27. }
    28. private new void Start()
    29. {
    30. base.Start();
    31. r = GetComponent();
    32. }
    33. private new void Update()
    34. {
    35. base.Update();
    36. if (!isDead && !stopMove && isVigilant && player)
    37. {
    38. bool isLeft = player.position.x < transform.position.x;
    39. transform.right = isLeft ? Vector2.left : Vector2.right;
    40. r.velocity = new Vector2(isLeft ? -2 : 2, r.velocity.y);
    41. }
    42. }
    43. }
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class MonsterBase : MonoBehaviour
    5. {
    6. public int hp = 1;
    7. SpriteRenderer render;
    8. protected Animator anim;
    9. protected Transform player;
    10. protected bool isDead = false, isVigilant = false;
    11. public void Start()
    12. {
    13. render = GetComponent();
    14. anim = GetComponent();
    15. player = GameObject.FindWithTag("Player").transform;
    16. }
    17. public void Update()
    18. {
    19. if (player&&Vector2.Distance(player.position, transform.position) < 5f)
    20. isVigilant = true;
    21. }
    22. private void OnTriggerEnter2D(Collider2D collision)
    23. {
    24. if (isDead) return;
    25. if (collision.gameObject.layer == 8)
    26. beHurt();
    27. }
    28. public void beHurt()
    29. {
    30. isVigilant = true;
    31. if(--hp<=0)
    32. {
    33. isDead = true;
    34. var r = GetComponent();
    35. r.constraints = RigidbodyConstraints2D.None;
    36. r.isKinematic = false;
    37. render.color = new Color(0.5f, 0.5f, 0.5f, 1);
    38. bool isLeft = transform.position.x - player.position.x >= 0;
    39. r.AddForce(new Vector2(isLeft ? 1 : -1, 1) * 200f);
    40. //r.angularVelocity = (isLeft ? -1 : 1) * 100f;
    41. anim.SetTrigger("die");
    42. if(transform.parent)
    43. {
    44. Destroy(transform.root.gameObject, 2f);
    45. }
    46. else
    47. {
    48. Destroy(gameObject, 2f);
    49. }
    50. }
    51. if(!isDead)
    52. StartCoroutine(TempTurnRed());
    53. }
    54. IEnumerator TempTurnRed()
    55. {
    56. render.color = Color.red;
    57. yield return new WaitForSeconds(0.1f);
    58. render.color = Color.white;
    59. }
    60. }
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class Cup : MonoBehaviour
    5. {
    6. void Start()
    7. {
    8. }
    9. private void OnTriggerEnter2D(Collider2D collision)
    10. {
    11. if (collision.gameObject.layer == 8)
    12. {
    13. //Destroy(collision.gameObject);
    14. PlayerCharacter.Instance.Win();
    15. }
    16. }
    17. // Update is called once per frame
    18. void Update()
    19. {
    20. }
    21. }

    (37) How to make an AWESOME fully INTERACTIVE Game Menu in UNITY - Everything you need to know. - YouTubehttps://www.youtube.com/watch?v=pGxI8_GYfUY

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class uisettingscript : MonoBehaviour
    5. {
    6. public GameObject Panel;
    7. public void Setting()
    8. {
    9. Panel.GetComponent().SetTrigger("Pop");
    10. }
    11. public void OpenSite()
    12. {
    13. Application.OpenURL("https://assetstore.unity.com/");
    14. }
    15. }
    1. //======================================================================================
    2. //==description:音频管理器
    3. //==state:播放音频、暂停、暂停继续播放、停止播放、切换音频、音频回调播放器、延时音频播放器、生成2D音效、3D音效、指定音效播放
    4. //======================================================================================
    5. using System;
    6. using System.Collections;
    7. using System.Collections.Generic;
    8. using UnityEngine;
    9. public delegate void AudioCallBack();
    10. [RequireComponent(typeof(AudioSource))]
    11. public class Audio : MonoBehaviour
    12. {
    13. public class ClipData
    14. {
    15. public AudioClip audiodata(string name)
    16. {
    17. return Resources.Load("Audios/" + name);
    18. }
    19. }
    20. public static Audio Instance;
    21. ClipData clipdata = new ClipData();
    22. public AudioSource _audioSource;
    23. void Awake()
    24. {
    25. Instance = this;
    26. }
    27. void Start()
    28. {
    29. _audioSource = GetComponent();
    30. }
    31. ///
    32. /// 播放音频 Resources/Audios/name
    33. ///
    34. ///
    35. public void AudioPlay(string name)
    36. {
    37. _audioSource.clip = clipdata.audiodata(name);
    38. _audioSource.Play();
    39. }
    40. ///
    41. /// 暂停播放
    42. ///
    43. public void AudioPause()
    44. {
    45. _audioSource.Pause();
    46. }
    47. ///
    48. /// 暂停播放后继续播放
    49. ///
    50. public void AudioUnPause()
    51. {
    52. _audioSource.UnPause();
    53. }
    54. ///
    55. /// 停止播放
    56. ///
    57. public void AudioStop()
    58. {
    59. _audioSource.Stop();
    60. }
    61. ///
    62. /// 切换音频 Resources/Audios/name
    63. ///
    64. ///
    65. public void AudioSwitch(string name)
    66. {
    67. AudioClip _clip = clipdata.audiodata(name);
    68. if (_audioSource.isPlaying)
    69. {
    70. _audioSource.Stop();
    71. }
    72. _audioSource.clip = _clip;
    73. _audioSource.Play();
    74. }
    75. ///
    76. /// 音频回调播放器 Resources/Audios/name callback=>音频播完后执行的方法。
    77. ///
    78. ///
    79. ///
    80. public void AudioPlayer(string name, AudioCallBack callback)
    81. {
    82. _audioSource.clip = clipdata.audiodata(name);
    83. _audioSource.Play();
    84. StartCoroutine(AudioDelayedCallBack(_audioSource.clip.length, callback));
    85. }
    86. ///
    87. /// 音频回调播放器 Resources/Audios/name callback=>音频播完后执行的方法。
    88. ///
    89. ///
    90. ///
    91. public void AudioPlayer(string name, AudioCallBack callback, float time)
    92. {
    93. _audioSource.clip = clipdata.audiodata(name);
    94. _audioSource.Play();
    95. StartCoroutine(AudioDelayedCallBack(_audioSource.clip.length + time, callback));
    96. }
    97. //音频延迟回调
    98. IEnumerator AudioDelayedCallBack(float time, AudioCallBack callback)
    99. {
    100. yield return new WaitForSeconds(time);
    101. callback();
    102. }
    103. ///
    104. /// 延时播放音频 Resources/Audios/name time=>延时时间
    105. ///
    106. ///
    107. ///
    108. public void AudioDelayPlay(string name, float time)
    109. {
    110. _audioSource.clip = clipdata.audiodata(name);
    111. Invoke("AudioDelayTime", time);
    112. }
    113. public void AudioDelayTime() { _audioSource.Play(); }
    114. ///
    115. /// 生成2D音效 Resources/Audios/name 播放完毕消失
    116. ///
    117. ///
    118. public void AudioInstantiate(string name)
    119. {
    120. GameObject obj = new GameObject();
    121. AudioSource _audio = obj.AddComponent();
    122. _audio.name = "AudioSource";
    123. _audio.playOnAwake = true;
    124. _audio.clip = clipdata.audiodata(name);
    125. _audio.Play();
    126. StartCoroutine(AudioFinish(_audio.clip.length, obj));
    127. }
    128. //音效结束销毁AudioGameObject
    129. IEnumerator AudioFinish(float time, GameObject obj)
    130. {
    131. yield return new WaitForSeconds(time);
    132. DestroyImmediate(obj);
    133. }
    134. ///
    135. /// 3D音效 (只播放一次) Resources/Audios/name CameraPos:摄像机位置
    136. ///
    137. ///
    138. ///
    139. public void AudioAtPoint(string name, Vector3 CameraPos)
    140. {
    141. AudioSource.PlayClipAtPoint(clipdata.audiodata(name), CameraPos, 1.0f);
    142. }
    143. ///
    144. /// 指定AudioSource播放音频
    145. ///
    146. ///
    147. ///
    148. public void AudioSourceOther(GameObject obj, string name)
    149. {
    150. AudioSource audioSource = obj.GetComponent();
    151. audioSource.clip = clipdata.audiodata(name);
    152. audioSource.Play();
    153. }
    154. public void CloseAudio()
    155. {
    156. StopAllCoroutines();
    157. _audioSource.clip = null;
    158. }
    159. }

    场景切换

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. public class TOBOSS : MonoBehaviour
    6. {
    7. public void OnBtnStart()
    8. {
    9. SceneManager.LoadScene("BOSS");
    10. }
    11. }

     DontDestroyOnLoad

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class BackgroundMusic : MonoBehaviour
    5. {
    6. static BackgroundMusic S;
    7. private void Awake()
    8. {
    9. if (S == null)
    10. {
    11. S = this;
    12. }
    13. else if(S != this)
    14. {
    15. Destroy(gameObject);
    16. }
    17. DontDestroyOnLoad(gameObject);
    18. }
    19. }

  • 相关阅读:
    KDD'22 | 对比学习+知识蒸馏,Bing搜索广告最新利器!
    ROS 工作空间
    详解Redis三大集群模式,轻松实现高可用!
    12.函数
    【vue3源码】三、effectScope源码解析
    R语言caTools包进行数据划分、scale函数进行数据缩放、e1071包的naiveBayes函数构建朴素贝叶斯模型
    【老生谈算法】matlab实现图像阈值分割算法——图像阈值分割
    Apache Hudi 元数据字段揭秘
    Java常见API---split()
    Docker 学习总结(78)—— Docker Rootless 让你的容器更安全
  • 原文地址:https://blog.csdn.net/Angelloveyatou/article/details/127786960