98 lines
3.3 KiB
C#
98 lines
3.3 KiB
C#
|
using System;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using UnityEngine;
|
||
|
|
||
|
//* General Comments or Finished Tasks
|
||
|
//TODO Tasks left to be done for game
|
||
|
//! Bugs or Issues
|
||
|
//? Questions or Suggestions
|
||
|
|
||
|
//! Aftering hitting down with hammer, turning stops working in what direction the player tries to move first in
|
||
|
//! As with throwing objects, the rock sometimes goes in random directions and random speeds
|
||
|
//*TODO Playing StickBeating Game, player should be able to throw rocks every few seconds
|
||
|
//? Soft auto lock on enemies when throwing rocks
|
||
|
public class MovementStickBeating : MonoBehaviour
|
||
|
{
|
||
|
public float speed = 7.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;
|
||
|
public KeyCode rockKey = KeyCode.Mouse1;
|
||
|
public bool hammer = false;
|
||
|
public bool rock = false;
|
||
|
public GameObject rockObject;
|
||
|
public GameObject spawnPoint;
|
||
|
|
||
|
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 && !hammer)
|
||
|
{
|
||
|
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);
|
||
|
transform.rotation = Quaternion.RotateTowards(transform.rotation, ToRotation, turnSpeed * Time.deltaTime);
|
||
|
}
|
||
|
|
||
|
moveDirection *= speed;
|
||
|
jumpForce = 2.5f;
|
||
|
|
||
|
if (Input.GetKeyDown(jumpKey))
|
||
|
{
|
||
|
moveDirection.y = jumpForce;
|
||
|
}
|
||
|
//If player is moving diagonally, reduce speed
|
||
|
if (forwards != 0 && right != 0 || forwards != 0 && left != 0 || backwards != 0 && right != 0 || backwards != 0 && left != 0)
|
||
|
{
|
||
|
moveDirection *= 0.7f;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
moveDirection *= 1.0f;
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
if(hammer)
|
||
|
{
|
||
|
moveDirection = Vector3.zero;
|
||
|
}
|
||
|
|
||
|
if(rock)
|
||
|
{
|
||
|
if(Input.GetKeyDown(rockKey))
|
||
|
{
|
||
|
GameObject throwing = Instantiate(rockObject, spawnPoint.transform.position, spawnPoint.transform.rotation);
|
||
|
throwing.GetComponent<Rigidbody>().AddForce(transform.forward * 8.5f, ForceMode.Impulse);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
moveDirection.y -= gravity * Time.deltaTime;
|
||
|
controller.Move(moveDirection * Time.deltaTime);
|
||
|
}
|
||
|
}
|