Last active
May 31, 2019 17:45
-
-
Save aviralgoel/cf8a3725694f2d4e593da69cc434ccb0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class PlayerMovement : MonoBehaviour | |
{ | |
public float turnSpeed = 20f; | |
Animator m_Animator; | |
Vector3 m_Movement; | |
Rigidbody m_Rigidbody; | |
Quaternion m_Rotation; | |
AudioSource m_MyAudioSource; | |
// Start is called before the first frame update | |
void Start() | |
{ | |
m_Animator = GetComponent<Animator>(); | |
m_Rigidbody = GetComponent<Rigidbody>(); | |
m_MyAudioSource = GetComponent<AudioSource>(); | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
float horizontal = Input.GetAxis("Horizontal"); | |
float vertical = Input.GetAxis("Vertical"); | |
m_Movement.Set(horizontal, 0f, vertical); | |
m_Movement.Normalize(); | |
m_Rigidbody.MovePosition(m_Rigidbody.position + m_Movement * Time.deltaTime); | |
bool hasHorizontalInput = !Mathf.Approximately(horizontal, 0f); | |
bool hasVerticalInput = !Mathf.Approximately(vertical, 0f); | |
bool isWalking = hasHorizontalInput || hasVerticalInput; | |
m_Animator.SetBool("IsWalking", isWalking); | |
Vector3 desiredForward = Vector3.RotateTowards(transform.forward, m_Movement, turnSpeed * Time.deltaTime, 0f); | |
m_Rotation = Quaternion.LookRotation(desiredForward); | |
m_Rigidbody.MoveRotation(m_Rotation); | |
if (Input.GetKeyDown(KeyCode.Space)) | |
{ | |
//Send the message to the Animator to activate the trigger parameter named "Jump" | |
m_Animator.SetTrigger("Dance"); | |
m_MyAudioSource.Play(); | |
} | |
if(isWalking) | |
{ | |
m_MyAudioSource.Stop(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment