Mini-Games-Game/Assets/Scripts/Movement.cs
Santi 3111305a83 WackAMole: Moles only give one point per, a timer has been added, comments
Timer: Created a universal timer

Colours: Improved Comments, cubes now randomly appear on start. When a cube is in a container it gets randomly teleported back
2024-06-07 14:37:52 +01:00

64 lines
2.0 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public float speed = 5.0f;
public float turnSpeed;
public float sprintSpeed = 10.0f;
public float jumpForce;
public float gravity = 9.8f;
public PickUpObject throwForce;
private CharacterController controller;
private 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;
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));
if (controller.isGrounded)
{
moveDirection = new Vector3(right - left, 0, forwards - backwards);
//* Change the direction the player is facing based on the direction they are moving
if (right - left != 0 || forwards - backwards != 0)
{
Quaternion ToRotation = Quaternion.LookRotation(moveDirection);
throwForce.throwForce = 8.5f;
transform.rotation = Quaternion.RotateTowards(transform.rotation, ToRotation, turnSpeed * Time.deltaTime);
}
else
{
throwForce.throwForce = 4.0f;
}
moveDirection *= speed;
jumpForce = 2.5f;
if (Input.GetKeyDown(jumpKey))
{
moveDirection.y = jumpForce;
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}