Stranar/Assets/Scripts/Character_ctrl.cs

163 lines
6.0 KiB
C#
Raw Normal View History

2022-06-25 15:42:49 +02:00
using UnityEngine;
using System.Collections;
//load textmeshpro
using TMPro;
// This script moves the character controller forward
// and sideways based on the arrow keys.
// It also jumps when pressing space.
// Make sure to attach a character controller to the same game object.
// It is recommended that you make only one call to Move or SimpleMove per frame.
public class Character_ctrl : MonoBehaviour
{
CharacterController characterController;
Camera camera_obj;
//win sound
public HuDcko hudcko;
public AudioClip win_sound;
public AudioClip die_sound;
public AudioClip walk_sound;
public AudioClip jump_sound;
public AudioClip teleport_sound;
public GameObject spawnpoint;
public float speed = 6.0f;
//textmeshpro
public TextMeshProUGUI textmeshpro;
public TextMeshProUGUI hudko;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
public float turnSpeed = 1.0f;
2022-06-27 11:54:53 +02:00
public bombspawner bs;
public Pukvbrane pukvbrane;
2022-06-25 15:42:49 +02:00
private float timeks = 0;
private bool passed = false;
Vector2 camerarot = new Vector2(0, 0);
private Vector3 moveDirection = Vector3.zero;
private Vector3 moveKeyb = Vector3.zero;
private bool ontop = false;
void Start()
{
characterController = GetComponent<CharacterController>();
camera_obj = GetComponentInChildren<Camera>();
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
characterController.enabled = false;
transform.position = spawnpoint.transform.position;
transform.rotation = spawnpoint.transform.rotation;
characterController.enabled = true;
}
void FixedUpdate()
{
if(timeks >= 0.2f){
timeks = 0;
passed = true;
}
else{
timeks += Time.fixedDeltaTime;
passed = false;
}
// We are grounded, so recalculate
// move direction directly from axes
//add mouse x and y to camera rotation
camerarot.x += Input.GetAxis("Mouse X");
camerarot.y += -Input.GetAxis("Mouse Y");
//use turn speed to rotate camera
camerarot.x *= turnSpeed;
camerarot.y *= turnSpeed;
//then modulo with 360 to keep it in range
camerarot.x %= 360;
camerarot.y %= 360;
//then apply to camera
//clip camerarot y to -90 to 90
if (camerarot.y > 90)
{
camerarot.y = 90;
}
if (camerarot.y < -90)
{
camerarot.y = -90;
}
camera_obj.transform.localRotation = Quaternion.Euler(camerarot.y, 0, 0);
transform.localRotation = Quaternion.Euler(0, camerarot.x, 0);
//then apply to character controller
//moveDirection = camera.transform.TransformDirection(moveDirection);
moveKeyb = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
//if moving and walk sound is not playing, play it
//add movekeyb to moveDirection while accounting for rotation of camera
moveDirection = transform.TransformDirection(moveKeyb);
//multiply moveDirection by speed
moveDirection *= speed;
2022-06-27 11:54:53 +02:00
if (Input.GetButtonDown("Jump"))
{
//if enabled textmeshpro disable
if (textmeshpro.enabled)
{
textmeshpro.enabled = false;
hudko.enabled = true;
bs.StartBombs();
pukvbrane.StartCubes();
}
}
2022-06-25 15:42:49 +02:00
if (characterController.isGrounded)
{
if (moveKeyb != Vector3.zero && !GetComponent<AudioSource>().isPlaying)
{
GetComponent<AudioSource>().clip = walk_sound;
GetComponent<AudioSource>().volume = 0.1f;
GetComponent<AudioSource>().Play();
}
if (Input.GetButtonDown("Jump"))
{
moveDirection.y = jumpSpeed;
//set location of sound to be the same as the character and play sound
AudioSource.PlayClipAtPoint(jump_sound, transform.position);
}
}
if (Input.GetButton("Fire1"))
{
if(passed){
AudioSource.PlayClipAtPoint(teleport_sound, transform.position);
if(ontop){
Debug.Log("moved to lower floor");
characterController.enabled = false;
transform.position = new Vector3(transform.position.x, transform.position.y - 920, transform.position.z);
characterController.enabled = true;
ontop = false;
}
else{
characterController.enabled = false;
transform.position = new Vector3(transform.position.x, transform.position.y + 920, transform.position.z);
characterController.enabled = true;
ontop = true;
Debug.Log("moved to upper floor");
}
AudioSource.PlayClipAtPoint(teleport_sound, transform.position);
}
}
if(transform.position.y <= -500){
AudioSource.PlayClipAtPoint(die_sound, transform.position);
characterController.enabled = false;
transform.position = spawnpoint.transform.position;
transform.rotation = spawnpoint.transform.rotation;
characterController.enabled = true;
AudioSource.PlayClipAtPoint(die_sound, transform.position);
}
// Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
// when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
// as an acceleration (ms^-2)
moveDirection.y -= gravity * Time.fixedDeltaTime;
// Move the controller
characterController.Move(moveDirection * Time.fixedDeltaTime);
hudcko.ontop = ontop;
}
}