Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. public class PlayerController : MonoBehaviour {
  2.  
  3. public float speed = 1;
  4. private Rigidbody2D rigidbody;
  5. private Animator animator;
  6. private int key;
  7.  
  8.  
  9. public float jumpForce = 240.0f;
  10. private float walkForce = 20.0f;
  11. private float MaxWalkSpeed = 10.0f;
  12.  
  13. // Use this for initialization
  14. void Start () {
  15. rigidbody = GetComponent<Rigidbody2D>();
  16. animator = GetComponent<Animator>();
  17. }
  18.  
  19. // Update is called once per frame
  20. void Update () {
  21. key = 0;
  22.  
  23. //ジャンプ
  24. if (Input.GetKeyDown(KeyCode.Space))
  25. {
  26. animator.SetTrigger("JumpTrigger");
  27. rigidbody.AddForce(transform.up * jumpForce);
  28. //jump_ = true;
  29. }
  30.  
  31. if(Input.GetKey(KeyCode.RightArrow))
  32. {
  33. key = 1;
  34. }
  35.  
  36. if (Input.GetKey(KeyCode.LeftArrow))
  37. {
  38. key = -1;
  39. }
  40.  
  41.  
  42. if ( key != 0)
  43. {
  44. transform.localScale = new Vector3(key,1,1);
  45. }
  46.  
  47. float speedx = Mathf.Abs(rigidbody.velocity.x);//ここです。
  48. //Debug.Log(speedx);
  49.  
  50. if(speedx < MaxWalkSpeed)
  51. {
  52. rigidbody.AddForce(transform.right * key * walkForce);
  53. }
  54.  
  55.  
  56. if(rigidbody.velocity.y == 0)
  57. {
  58. animator.speed = speedx / 2.0f;
  59. }
  60. else
  61. {
  62. animator.speed = 1.0f;
  63. }
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement