Skip to content

Commit e41e8f7

Browse files
authored
Chapter 20 Assets and Code
1 parent 1768e93 commit e41e8f7

8 files changed

+130
-0
lines changed

Chapter 20/Assets/Cat.png

58.3 KB
Loading
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using UnityEngine;
5+
using UnityEngine.InputSystem;
6+
7+
public class ActionReferenceBasicCharacterController : MonoBehaviour
8+
{
9+
private Rigidbody2D catRigidbody;
10+
[SerializeField] private float speed;
11+
[SerializeField] private float jumpHeight;
12+
private bool grounded;
13+
private Vector2 moveVector = new Vector2();
14+
15+
[SerializeField] private InputActionAsset actions;
16+
private InputAction playerMoveAction;
17+
18+
void Awake()
19+
{
20+
catRigidbody = GetComponent<Rigidbody2D>();
21+
}
22+
23+
private void OnEnable() {
24+
actions.FindActionMap("Player").Enable();
25+
actions.FindActionMap("Player").FindAction("Jump").performed += OnJump; // subscribe to the "Jump" action
26+
playerMoveAction = actions.FindActionMap("Player").FindAction("Move"); // find the "Move" action
27+
}
28+
29+
void Update() {
30+
moveVector = playerMoveAction.ReadValue<Vector2>();
31+
catRigidbody.velocity = new Vector2(speed * moveVector.x, catRigidbody.velocity.y);
32+
}
33+
34+
// called from Jump Event on Cat's Player Input component
35+
public void OnJump(InputAction.CallbackContext context) {
36+
if(grounded){
37+
catRigidbody.AddForce(new Vector2(catRigidbody.velocity.x, jumpHeight));
38+
}
39+
}
40+
41+
private void OnCollisionEnter2D(Collision2D other) {
42+
grounded = true;
43+
}
44+
45+
private void OnCollisionExit2D(Collision2D other) {
46+
grounded = false;
47+
}
48+
49+
private void OnDisable() {
50+
actions.FindActionMap("Player").FindAction("Jump").performed -= OnJump;
51+
actions.FindActionMap("Player").Disable();
52+
}
53+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using UnityEngine;
5+
6+
public class InputManagerBasicCharacterController : MonoBehaviour
7+
{
8+
private Rigidbody2D catRigidbody;
9+
[SerializeField] private float speed;
10+
[SerializeField] private float jumpHeight;
11+
private float movement;
12+
private bool grounded;
13+
14+
void Awake()
15+
{
16+
catRigidbody = GetComponent<Rigidbody2D>();
17+
}
18+
19+
void Update() {
20+
movement = Input.GetAxis("Horizontal");
21+
catRigidbody.velocity = new Vector2(speed * movement, catRigidbody.velocity.y);
22+
23+
if(grounded && Input.GetButtonDown("Jump")){
24+
catRigidbody.AddForce(new Vector2(catRigidbody.velocity.x, jumpHeight));
25+
}
26+
}
27+
28+
private void OnCollisionEnter2D(Collision2D other) {
29+
grounded = true;
30+
}
31+
32+
private void OnCollisionExit2D(Collision2D other) {
33+
grounded = false;
34+
}
35+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using UnityEngine;
5+
using UnityEngine.InputSystem;
6+
7+
public class PlayerInputBasicCharacterController : MonoBehaviour
8+
{
9+
private Rigidbody2D catRigidbody;
10+
[SerializeField] private float speed;
11+
[SerializeField] private float jumpHeight;
12+
private bool grounded;
13+
private Vector2 moveVector = new Vector2();
14+
15+
void Awake()
16+
{
17+
catRigidbody = GetComponent<Rigidbody2D>();
18+
}
19+
20+
void Update() {
21+
catRigidbody.velocity = new Vector2(speed * moveVector.x, catRigidbody.velocity.y);
22+
}
23+
24+
// called from Jump Event on Cat's Player Input component
25+
public void OnJump(InputAction.CallbackContext context) {
26+
if(grounded){
27+
catRigidbody.AddForce(new Vector2(catRigidbody.velocity.x, jumpHeight));
28+
}
29+
}
30+
31+
// called from Move Event on Cat's Player Input component
32+
public void OnMove(InputAction.CallbackContext context) {
33+
moveVector = context.ReadValue<Vector2>();
34+
}
35+
private void OnCollisionEnter2D(Collision2D other) {
36+
grounded = true;
37+
}
38+
39+
private void OnCollisionExit2D(Collision2D other) {
40+
grounded = false;
41+
}
42+
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)