Mini-Games-Game/Assets/Movement.cs
Santi d68f3bd970 Unity: Throwable Objects
Able to throw objects that have been picked up
2024-04-28 17:14:08 +01:00

66 lines
1.9 KiB
C#

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;
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
if (controller.isGrounded)
{
// Change the direction the player is facing based on the direction they are moving
if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
{
Quaternion ToRotation = Quaternion.LookRotation(new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")));
throwForce.throwForce = 7.5f;
transform.rotation = Quaternion.RotateTowards(transform.rotation, ToRotation, turnSpeed * Time.deltaTime);
}
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
if (Input.GetKey(KeyCode.LeftShift))
{
moveDirection *= sprintSpeed;
jumpForce = 4.0f;
if(Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
{
throwForce.throwForce = 20.0f;
}
else
{
throwForce.throwForce = 5.0f;
}
}
else
{
moveDirection *= speed;
jumpForce = 2.5f;
}
if (Input.GetButton("Jump"))
{
moveDirection.y = jumpForce;
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}