You are on page 1of 14

Movement Controller Script

using Photon.Pun;

using System.Collections;

using UnityEngine;

public class MovementController : MonoBehaviour

Animator anim;

private PhotonView PV;

public GameObject playerCamera;

Transform cameraTransform;

float cameraRotation = 0f;

float currentCameraRotation = 0f;

bool iS_crouch = false;

float fixed_rate = 0.1f;

float Fixed_time;

bool startGame = false;

AudioSource audio_source;

//audio clips

public AudioClip Running_SFX;

public AudioClip Crouch_SFX;

1
GameObject characterController;

void Start()

StartCoroutine("WaitTillLanded");

audio_source = GetComponent<AudioSource>();

if (!anim == gameObject.GetComponent<Animator>())

anim = gameObject.GetComponent<Animator>();

Debug.Log("Animator is set now ");

else

Debug.Log("Animator is set ");

cameraTransform = playerCamera.GetComponent<Transform>();

// if(!PV.IsMine)

// {

// //playerCamera.SetActive(false);

// }

2
IEnumerator WaitTillLanded()

yield return new WaitForSeconds(5f);

startGame = true;

void Update()

if (startGame)

if (Fixed_time < fixed_rate)

Fixed_time += Time.deltaTime;

if (!iS_crouch)

StartCoroutine("wait");

Input_AnimationControl();

else

StartCoroutine("wait1");

Crouch_AnimationControl();

Debug.Log("Crouch " + iS_crouch);

3
MouseLook();

private void Input_AnimationControl()

if (!iS_crouch && Input.GetKeyDown(KeyCode.C))

Fixed_time = 0f;

Debug.Log("Layer Weight of " + anim.GetLayerName(3) + " is


" + anim.GetLayerWeight(3));

iS_crouch = true;

if (!anim)

Debug.LogWarning("animator component is missing");

if (Input.GetMouseButtonDown(0))

4
anim.SetTrigger("Aim");

else

if (Input.GetMouseButtonUp(0))

anim.SetTrigger("Aim");

if (Input.GetKeyDown(KeyCode.S))

audio_source.PlayOneShot(Running_SFX, 1f);

anim.SetFloat("Backward", 1f);

if (Input.GetKey(KeyCode.Space))

anim.SetTrigger("Jump");

else

if (Input.GetKeyUp(KeyCode.S))

audio_source.Stop();

5
anim.SetFloat("Backward", 0);

if (Input.GetKeyDown(KeyCode.A))

audio_source.PlayOneShot(Running_SFX, 1f);

anim.SetFloat("Leftside", 1f);

if (Input.GetKey(KeyCode.Space))

anim.SetTrigger("Jump");

else

if (Input.GetKeyUp(KeyCode.A))

audio_source.Stop();

anim.SetFloat("Leftside", 0);

if (Input.GetKeyDown(KeyCode.D))

audio_source.PlayOneShot(Running_SFX, 1f);

6
anim.SetFloat("Rightside", 1f);

else

if (Input.GetKeyUp(KeyCode.D))

audio_source.Stop();

anim.SetFloat("Rightside", 0);

if (Input.GetKeyDown(KeyCode.W))

audio_source.PlayOneShot(Running_SFX, 1f);

anim.SetFloat("Forward", 1f);

else

if (Input.GetKeyUp(KeyCode.W))

audio_source.Stop();

anim.SetFloat("Forward", 0);

if (Input.GetKey(KeyCode.Space))

7
anim.Play("Jump");

private void Crouch_AnimationControl()

anim.SetLayerWeight(3, 1f);

//anim.SetFloat("Crouch", 1f);

anim.SetBool("CrouchB", true);

if (Fixed_time > fixed_rate && Input.GetKey(KeyCode.C) &&


iS_crouch)

iS_crouch = false;

Fixed_time = 0f;

// anim.SetFloat("Crouch", 0f);

anim.SetBool("CrouchB", false);

anim.SetLayerWeight(3, 0f);

Debug.Log("Layer Weight of " + anim.GetLayerName(3) + " is


" + anim.GetLayerWeight(3));

if (!anim)

Debug.LogWarning("animator component is missing");

8
if (Input.GetMouseButtonDown(0))

anim.SetTrigger("Aim");

if (Input.GetMouseButtonUp(0))

anim.SetTrigger("Aim");

// StartCoroutine("AimToNormal");

else

if (Input.GetMouseButtonUp(0))

anim.SetTrigger("Aim");

// StartCoroutine("AimToNormal");

if (Input.GetKeyDown(KeyCode.S))

9
audio_source.PlayOneShot(Crouch_SFX, 1f);

anim.SetFloat("Backward", 1f);

if (Input.GetKey(KeyCode.Space))

anim.SetTrigger("Jump");

else

if (Input.GetKeyUp(KeyCode.S))

audio_source.Stop();

anim.SetFloat("Backward", 0);

if (Input.GetKeyDown(KeyCode.A))

audio_source.PlayOneShot(Crouch_SFX, 1f);

anim.SetFloat("Leftside", 1f);

if (Input.GetKey(KeyCode.Space))

anim.SetTrigger("Jump");

10
}

else

if (Input.GetKeyUp(KeyCode.A))

audio_source.Stop();

anim.SetFloat("Leftside", 0);

if (Input.GetKeyDown(KeyCode.D))

audio_source.PlayOneShot(Crouch_SFX, 1f);

anim.SetFloat("Rightside", 1f);

else

if (Input.GetKeyUp(KeyCode.D))

audio_source.Stop();

anim.SetFloat("Rightside", 0);

if (Input.GetKeyDown(KeyCode.W))

11
audio_source.PlayOneShot(Crouch_SFX, 1f);

anim.SetFloat("Forward", 1f);

else

if (Input.GetKeyUp(KeyCode.W))

audio_source.Stop();

anim.SetFloat("Forward", 0);

if (Input.GetKey(KeyCode.Space))

anim.Play("Jump");

void MouseLook()

currentCameraRotation = Input.GetAxis("Mouse Y");

float mouseSensitivityUpAndDown = 0.7f;

float mouseSensitivity = 4f;

Transform playerRotation = gameObject.GetComponent<Transform>


();

cameraTransform.Rotate(Vector3.left * currentCameraRotation *
mouseSensitivityUpAndDown);

playerRotation.Rotate(0, Input.GetAxis("Mouse X") *


mouseSensitivity, 0);

12
if (Input.GetMouseButtonDown(0))

playerRotation.Rotate(0, 20, 0);

cameraTransform.Rotate(0, -20, 0);

else

if (Input.GetMouseButtonUp(0))

playerRotation.Rotate(0, -20, 0);

cameraTransform.Rotate(0, 20, 0);

IEnumerator wait()

yield return new WaitForSeconds(2);

IEnumerator wait1()

yield return new WaitForSeconds(2);

IEnumerator AimToNormal()

yield return new WaitForSeconds(2f);

13
anim.SetTrigger("Aim");

14

You might also like