Mini-Games-Game/Assets/Scripts/Movement.cs

80 lines
2.4 KiB
C#
Raw Normal View History

using System;
2024-04-28 14:13:37 +01:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public float speed = 5.0f;
public float turnSpeed;
2024-04-28 14:13:37 +01:00
public float sprintSpeed = 10.0f;
public float jumpForce;
2024-04-28 14:13:37 +01:00
public float gravity = 9.8f;
public PickUpObject throwForce;
2024-04-28 14:13:37 +01:00
public CharacterController controller;
public Vector3 moveDirection = Vector3.zero;
[Header("Keybinds")]
public KeyCode forwardsKey = KeyCode.W;
public KeyCode backwardsKey = KeyCode.S;
public KeyCode leftKey = KeyCode.A;
public KeyCode rightKey = KeyCode.D;
public KeyCode sprintKey = KeyCode.LeftShift;
public KeyCode jumpKey = KeyCode.Space;
2024-04-28 14:13:37 +01:00
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
float forwards = Convert.ToSingle(Input.GetKey(forwardsKey));
float backwards = Convert.ToSingle(Input.GetKey(backwardsKey));
float left = Convert.ToSingle(Input.GetKey(leftKey));
float right = Convert.ToSingle(Input.GetKey(rightKey));
moveDirection = new Vector3(right - left, 0, forwards - backwards);
if (!controller.isGrounded)
{
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
return;
}
2024-04-28 14:13:37 +01:00
throwForce.throwForce = 4.0f;
// Change the direction the player is facing based on the direction they are moving
if (moveDirection != Vector3.zero)
{
Quaternion ToRotation = Quaternion.LookRotation(new Vector3(right - left, 0, forwards - backwards));
throwForce.throwForce = 8.5f;
transform.rotation = Quaternion.RotateTowards(transform.rotation, ToRotation, turnSpeed * Time.deltaTime);
}
if (Input.GetKey(sprintKey))
{
moveDirection *= sprintSpeed;
jumpForce = 4.0f;
if(moveDirection != Vector3.zero)
2024-04-28 14:13:37 +01:00
{
throwForce.throwForce = 20.0f;
2024-04-28 14:13:37 +01:00
}
}
else
{
moveDirection *= speed;
jumpForce = 2.5f;
}
2024-04-28 14:13:37 +01:00
if (Input.GetKeyDown(jumpKey))
{
moveDirection.y = jumpForce;
2024-04-28 14:13:37 +01:00
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}