Chapter 4.3 done

This commit is contained in:
BeatriceAl
2026-04-05 00:21:35 +02:00
parent 29e85cfd20
commit d7e461212a
2 changed files with 557 additions and 10 deletions
@@ -3,12 +3,29 @@ using UnityEngine.InputSystem;
public class PlayerControl : MonoBehaviour
{
private CharacterController Figur;
private CharacterController Figure;
private Vector3 MoveVector = Vector3.zero;
private InputAction moveAction; // for new Input handling
private InputAction jumpAction; // for new Input handling
private SpriteRenderer renderer;
private float current = 0.0f;
[Header("Movement")]
public float Speed = 5.0f;
public float Jump = 5.0f;
public float Gravity = 15.0f;
[Header("Sprites")]
public Sprite front, back;
public Sprite right1, right2;
public Sprite left1, left2;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
Figur = GetComponent<CharacterController>();
Figure = GetComponent<CharacterController>();
renderer = GetComponent<SpriteRenderer>();
}
// Update is called once per frame
@@ -17,19 +34,89 @@ public class PlayerControl : MonoBehaviour
// old Input handling - used in book
/*
if (Input.GetKey(KeyCode.LeftArrow))
Figur.Move(Vector3.left * Time.deltaTime * Speed);
Figure.Move(Vector3.left * Time.deltaTime * Speed);
if (Input.GetKey(KeyCode.RightArrow))
Figur.Move(Vector3.right * Time.deltaTime * Speed);
Figure.Move(Vector3.right * Time.deltaTime * Speed);
*/
/*
if (Figure.isGrounded){
MoveVector.x = Input.GetAxis("Horizontal");
if(Input.GetButton("Jump")) MoveVector.y = Jump;
}
MoveVector.y -= Gravity * Time.deltaTime;
Figure.Move(MoveVector * Time.deltaTime * Speed);
*/
/*
if (MoveVector.x < 0) renderer.sprite = left1;
else if (MoveVector.x > 0) renderer.sprite = right1;
else renderer.sprite = front;
*/
//new Input handling - arrows and wasd
/*
if (Keyboard.current.leftArrowKey.isPressed || Keyboard.current.aKey.isPressed)
Figur.Move(Vector3.left * Time.deltaTime * Speed);
Figure.Move(Vector3.left * Time.deltaTime * Speed);
if (Keyboard.current.rightArrowKey.isPressed || Keyboard.current.dKey.isPressed)
Figur.Move(Vector3.right * Time.deltaTime * Speed);
Figure.Move(Vector3.right * Time.deltaTime * Speed);
if (Keyboard.current.upArrowKey.isPressed || Keyboard.current.wKey.isPressed)
Figur.Move(Vector3.up * Time.deltaTime * Speed);
Figure.Move(Vector3.up * Time.deltaTime * Speed);
if (Keyboard.current.downArrowKey.isPressed || Keyboard.current.sKey.isPressed)
Figur.Move(Vector3.down * Time.deltaTime * Speed);
Figure.Move(Vector3.down * Time.deltaTime * Speed);
*/
Vector2 input = moveAction.ReadValue<Vector2>();
if (Figure.isGrounded){
MoveVector.x = input.x;
if (jumpAction.WasPressedThisFrame()) MoveVector.y = Jump;
}
MoveVector.y -= Gravity * Time.deltaTime;
Figure.Move(MoveVector * Time.deltaTime * Speed);
if (MoveVector.x < 0) goLeft();
else if (MoveVector.x > 0) goRight();
else renderer.sprite = front;
}
void OnEnable() // for new Input handling
{
moveAction = new InputAction(type: InputActionType.Value);
moveAction.AddCompositeBinding("2DVector")
.With("Left", "<Keyboard>/a")
.With("Right", "<Keyboard>/d")
.With("Up", "<Keyboard>/w")
.With("Down", "<Keyboard>/s");
moveAction.AddCompositeBinding("2DVector")
.With("Left", "<Keyboard>/leftArrow")
.With("Right", "<Keyboard>/rightArrow")
.With("Up", "<Keyboard>/upArrow")
.With("Down", "<Keyboard>/downArrow");
jumpAction = new InputAction("Jump", InputActionType.Button);
jumpAction.AddBinding("<Keyboard>/space");
jumpAction.AddBinding("<Keyboard>/w");
jumpAction.AddBinding("<Keyboard>/upArrow");
moveAction.Enable();
jumpAction.Enable();
}
void OnDisable() // for new Input handling
{
moveAction.Disable();
jumpAction.Disable();
}
private void goLeft(){
current += 0.1f;
if (current < 10.0f) return;
renderer.sprite = renderer.sprite == left1 ? left2 : left1;
current = 0.0f;
}
private void goRight(){
current += 0.1f;
if (current < 10.0f) return;
renderer.sprite = renderer.sprite == right1 ? right2 : right1;
current = 0.0f;
}
}