Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerMovement2 : MonoBehaviour
- {
- public Animator animation;
- public float velocity = 20f;
- [Range(1f, 100f)]
- public float jumpVelocity;
- Transform playerGraphics;
- bool isOnGround = false;
- public AudioClip jump;
- private AudioSource audioSource;
- private void Awake()
- {
- playerGraphics = transform.Find("Player GFX"); // Used to split the player GFX from his arms or guns GFX
- }
- public void Start()
- {
- audioSource = GetComponent<AudioSource>(); // Footsteps Audio
- }
- public void Update()
- {
- // Movement
- Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
- transform.position += movement * Time.deltaTime * velocity;
- //Audio
- if (Input.GetAxis("Horizontal") == 0)
- {
- audioSource.Play();
- }
- if (Input.GetButtonDown("Jump") && isOnGround == true)
- {
- audioSource.PlayOneShot(jump);
- }
- // Animation
- if (Input.GetAxis("Horizontal") != 0)
- {
- animation.SetFloat("Speed", velocity);
- }
- else
- {
- animation.SetFloat("Speed", 0f);
- }
- // Jump
- if (Input.GetButtonDown("Jump") && isOnGround == true)
- {
- GetComponent<Rigidbody2D>().velocity = Vector2.up * jumpVelocity;
- }
- // Sprite Flip
- Vector3 potatoPunch = playerGraphics.localScale;
- if (Input.GetAxis("Horizontal") < 0)
- {
- potatoPunch.x = -1;
- }
- else if (Input.GetAxis("Horizontal") > 0)
- {
- potatoPunch.x = 1;
- }
- playerGraphics.localScale = potatoPunch;
- }
- // Jump Detection
- public void OnCollisionEnter2D(Collision2D collision)
- {
- if (collision.collider.tag == "Ground")
- {
- audioSource.Play(); //Audio continues on entering ground collision after jumping for walking
- isOnGround = true;
- }
- Debug.Log("Enter");
- }
- private void OnCollisionExit2D(Collision2D collision)
- {
- if (collision.collider.tag == "Ground")
- {
- audioSource.Stop(); //Audio stop on exiting ground collision for walking
- isOnGround = false;
- }
- Debug.Log("Exit");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement