Advertisement
Guest User

player movement

a guest
Nov 17th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerMovement2 : MonoBehaviour
  6. {
  7.     public Animator animation;
  8.     public float velocity = 20f;
  9.  
  10.     [Range(1f, 100f)]
  11.     public float jumpVelocity;
  12.  
  13.     Transform playerGraphics;
  14.  
  15.     bool isOnGround = false;
  16.  
  17.     public AudioClip jump;
  18.     private AudioSource audioSource;
  19.    
  20.     private void Awake()
  21.     {
  22.         playerGraphics = transform.Find("Player GFX");  // Used to split the player GFX from his arms or guns GFX
  23.     }
  24.  
  25.     public void Start()
  26.     {
  27.         audioSource = GetComponent<AudioSource>(); // Footsteps Audio
  28.     }
  29.  
  30.     public void Update()
  31.     {
  32.         // Movement
  33.         Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
  34.         transform.position += movement * Time.deltaTime * velocity;
  35.  
  36.         //Audio
  37.         if (Input.GetAxis("Horizontal") == 0)
  38.         {
  39.             audioSource.Play();
  40.         }
  41.         if (Input.GetButtonDown("Jump") && isOnGround == true)
  42.         {
  43.             audioSource.PlayOneShot(jump);
  44.         }
  45.  
  46.         // Animation
  47.         if (Input.GetAxis("Horizontal") != 0)
  48.         {
  49.             animation.SetFloat("Speed", velocity);
  50.         }
  51.         else
  52.         {
  53.             animation.SetFloat("Speed", 0f);
  54.         }
  55.  
  56.         // Jump
  57.         if (Input.GetButtonDown("Jump") && isOnGround == true)
  58.         {
  59.             GetComponent<Rigidbody2D>().velocity = Vector2.up * jumpVelocity;
  60.         }
  61.  
  62.         // Sprite Flip
  63.         Vector3 potatoPunch = playerGraphics.localScale;
  64.         if (Input.GetAxis("Horizontal") < 0)
  65.         {
  66.             potatoPunch.x = -1;
  67.         }
  68.         else if (Input.GetAxis("Horizontal") > 0)
  69.         {
  70.             potatoPunch.x = 1;
  71.         }
  72.         playerGraphics.localScale = potatoPunch;
  73.     }
  74.  
  75.     // Jump Detection
  76.     public void OnCollisionEnter2D(Collision2D collision)
  77.     {
  78.         if (collision.collider.tag == "Ground")
  79.         {
  80.             audioSource.Play(); //Audio continues on entering ground collision after jumping for walking
  81.             isOnGround = true;
  82.         }
  83.  
  84.         Debug.Log("Enter");
  85.     }
  86.     private void OnCollisionExit2D(Collision2D collision)
  87.     {
  88.         if (collision.collider.tag == "Ground")
  89.         {
  90.             audioSource.Stop(); //Audio stop on exiting ground collision for walking
  91.             isOnGround = false;
  92.         }
  93.  
  94.         Debug.Log("Exit");
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement