• Unity笔记(13):Android Movement of Characters[2D]


    目录

    1、搭建一个测试场景

    2、建立画布设置移动按钮

    3、编写脚本绑定按钮

    AxisTouchButton :
    ButtonHandler :

    4、编写脚本绑定角色

    原来的按键移动

    修改为触摸按钮

    5、导出为APK文件,手机下载进行测试


     

    1、搭建一个测试场景

    2、建立画布设置移动按钮

     

     

    3、编写脚本绑定按钮

    脚本资源来自:

     因为资源已经弃用了,资源商店搜是搜不到的。但是我之前下载过,所以有些还能用。

    本次学习只需要以下代码即可:

    左右移动按钮挂载同一个脚本:

    AxisTouchButton :

    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.EventSystems;
    4. namespace UnityStandardAssets.CrossPlatformInput
    5. {
    6. public class AxisTouchButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
    7. {
    8. // designed to work in a pair with another axis touch button
    9. // (typically with one having -1 and one having 1 axisValues)
    10. public string axisName = "Horizontal"; // The name of the axis
    11. public float axisValue = 1; // The axis that the value has
    12. public float responseSpeed = 3; // The speed at which the axis touch button responds
    13. public float returnToCentreSpeed = 3; // The speed at which the button will return to its centre
    14. AxisTouchButton m_PairedWith; // Which button this one is paired with
    15. CrossPlatformInputManager.VirtualAxis m_Axis; // A reference to the virtual axis as it is in the cross platform input
    16. void OnEnable()
    17. {
    18. if (!CrossPlatformInputManager.AxisExists(axisName))
    19. {
    20. // if the axis doesnt exist create a new one in cross platform input
    21. m_Axis = new CrossPlatformInputManager.VirtualAxis(axisName);
    22. CrossPlatformInputManager.RegisterVirtualAxis(m_Axis);
    23. }
    24. else
    25. {
    26. m_Axis = CrossPlatformInputManager.VirtualAxisReference(axisName);
    27. }
    28. FindPairedButton();
    29. }
    30. void FindPairedButton()
    31. {
    32. // find the other button witch which this button should be paired
    33. // (it should have the same axisName)
    34. var otherAxisButtons = FindObjectsOfType(typeof(AxisTouchButton)) as AxisTouchButton[];
    35. if (otherAxisButtons != null)
    36. {
    37. for (int i = 0; i < otherAxisButtons.Length; i++)
    38. {
    39. if (otherAxisButtons[i].axisName == axisName && otherAxisButtons[i] != this)
    40. {
    41. m_PairedWith = otherAxisButtons[i];
    42. }
    43. }
    44. }
    45. }
    46. void OnDisable()
    47. {
    48. // The object is disabled so remove it from the cross platform input system
    49. m_Axis.Remove();
    50. }
    51. public void OnPointerDown(PointerEventData data)
    52. {
    53. if (m_PairedWith == null)
    54. {
    55. FindPairedButton();
    56. }
    57. // update the axis and record that the button has been pressed this frame
    58. m_Axis.Update(Mathf.MoveTowards(m_Axis.GetValue, axisValue, responseSpeed * Time.deltaTime));
    59. }
    60. public void OnPointerUp(PointerEventData data)
    61. {
    62. m_Axis.Update(Mathf.MoveTowards(m_Axis.GetValue, 0, responseSpeed * Time.deltaTime));
    63. }
    64. }
    65. }

    左边设置

    右边设置

     

     

     跳跃按钮挂载这个脚本:

    ButtonHandler :

    1. using System;
    2. using UnityEngine;
    3. namespace UnityStandardAssets.CrossPlatformInput
    4. {
    5. public class ButtonHandler : MonoBehaviour
    6. {
    7. public string Name;
    8. void OnEnable()
    9. {
    10. }
    11. public void SetDownState()
    12. {
    13. CrossPlatformInputManager.SetButtonDown(Name);
    14. }
    15. public void SetUpState()
    16. {
    17. CrossPlatformInputManager.SetButtonUp(Name);
    18. }
    19. public void SetAxisPositiveState()
    20. {
    21. CrossPlatformInputManager.SetAxisPositive(Name);
    22. }
    23. public void SetAxisNeutralState()
    24. {
    25. CrossPlatformInputManager.SetAxisZero(Name);
    26. }
    27. public void SetAxisNegativeState()
    28. {
    29. CrossPlatformInputManager.SetAxisNegative(Name);
    30. }
    31. public void Update()
    32. {
    33. }
    34. }
    35. }

    跳跃脚本设置

     

    4、编写脚本绑定角色

    原来的按键移动

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Collections.Specialized;
    4. using UnityEngine;
    5. public class Char : MonoBehaviour
    6. {
    7. private Rigidbody2D rb;
    8. private float moveSpeed;
    9. private float dirX;
    10. private bool facingRight = true;
    11. private Vector3 localScale;
    12. private void Start()
    13. {
    14. rb = GetComponent();
    15. localScale = transform.localScale;
    16. moveSpeed = 5f;
    17. }
    18. private void Update()
    19. {
    20. dirX = Input.GetAxisRaw("Horizontal") * moveSpeed;
    21. if (Input.GetButtonDown("Jump") && rb.velocity.y == 0)
    22. rb.AddForce(Vector2.up * 700f);
    23. }
    24. private void FixedUpdate()
    25. {
    26. rb.velocity = new Vector2(dirX, rb.velocity.y);
    27. }
    28. private void LateUpdate()
    29. {
    30. if (dirX > 0)
    31. facingRight = true;
    32. else if (dirX < 0)
    33. facingRight = false;
    34. if (((facingRight) && (localScale.x < 0)) || ((!facingRight) && (localScale.x > 0)))
    35. localScale.x *= -1;
    36. transform.localScale = localScale;
    37. }
    38. }

    修改为触摸按钮

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Collections.Specialized;
    4. using UnityEngine;
    5. using UnityStandardAssets.CrossPlatformInput;
    6. public class Character : MonoBehaviour
    7. {
    8. private Rigidbody2D rb;
    9. private float moveSpeed;
    10. private Animator anim;
    11. private float dirX;
    12. private bool facingRight = true;
    13. private Vector3 localScale;
    14. private void Start()
    15. {
    16. rb = GetComponent();
    17. anim = GetComponent();
    18. localScale = transform.localScale;
    19. moveSpeed = 15f;
    20. }
    21. private void Update()
    22. {
    23. dirX = CrossPlatformInputManager.GetAxisRaw("Horizontal") * moveSpeed;
    24. if (CrossPlatformInputManager.GetButton("Jump"))
    25. {
    26. // if ( && rb.velocity.y == 0)
    27. rb.AddForce(Vector2.up * 5f);
    28. anim.SetBool("isjump", true);
    29. }
    30. if (rb.velocity.y == 0)
    31. anim.SetBool("isjump", false);
    32. }
    33. private void FixedUpdate()
    34. {
    35. rb.velocity = new Vector2(dirX, rb.velocity.y);
    36. }
    37. private void LateUpdate()
    38. {
    39. if (dirX > 0)
    40. facingRight = true;
    41. else if (dirX < 0)
    42. facingRight = false;
    43. if (((facingRight) && (localScale.x < 0)) || ((!facingRight) && (localScale.x > 0)))
    44. localScale.x *= -1;
    45. transform.localScale = localScale;
    46. }
    47. }

    5、导出为APK文件,手机下载进行测试

     

  • 相关阅读:
    传感器_三相-双极性-开关型-霍尔传感器 速度+电角度解算理解
    ChatGPT:Spring Boot和Maven——Java应用开发的关键工具和区别
    STM32 HAL库串口使用printf
    Thread线程类基本使用(上)
    Vivado 与 Vitis 2022.1 安装记录
    电子病历编辑器源码(Springboot+原生HTML)
    RocketMQ(3)之事务消息
    Java语言的特点||运算符
    持续进化,快速转录,Faster-Whisper对视频进行双语字幕转录实践(Python3.10)
    使用PyTorch实现简单的AlphaZero的算法(1):背景和介绍
  • 原文地址:https://blog.csdn.net/qq_51701007/article/details/128129601