Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Platformer/Platformer/Assets/Scripts/Player&Enemy/PlayerInput.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
96 lines (67 sloc)
2.9 KB
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 UnityEngine; | |
using System.Collections; | |
using UnityEngine.UI; | |
[RequireComponent(typeof(Player))] | |
public class PlayerInput : MonoBehaviour | |
{ | |
Player player; //reference to player script | |
private Rigidbody2D m_Rigidbody2D; | |
public static bool m_FacingRight = true; // For determining which way the player is currently facing. | |
public Transform attackPoint; //point of attack | |
public Transform leftattackPoint; //point of attack | |
public Transform rightattackPoint; //point of attack | |
public Text timertext; | |
private float startTime; | |
public SpriteRenderer mySpriteRenderer; //to flip | |
public Animator animator; | |
private void Awake () | |
{ | |
m_Rigidbody2D = GetComponent<Rigidbody2D>(); | |
} | |
void Start() | |
{ | |
player = GetComponent<Player>(); | |
startTime = Time.time; | |
} | |
void Update() | |
{ | |
//timer things | |
float t = Time.time - startTime; | |
string minutes = ((int)t / 60).ToString(); | |
string seconds = (t % 60).ToString("f2"); | |
timertext.text = minutes + ":" + seconds; | |
Vector2 directionalInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); // | |
player.SetDirectionalInput(directionalInput); //pass in directional input | |
//set up attack positions so on turn the sword circle was in correct position | |
attackPoint.position = new Vector3(attackPoint.position.x, attackPoint.position.y, attackPoint.position.z); | |
leftattackPoint.position = new Vector3(leftattackPoint.position.x, leftattackPoint.position.y, attackPoint.position.z); | |
rightattackPoint.position = new Vector3(rightattackPoint.position.x, rightattackPoint.position.y, attackPoint.position.z); | |
//run animation | |
animator.SetFloat("Speed", Mathf.Abs(Input.GetAxisRaw("Horizontal"))); | |
if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow)) //space down | |
{ | |
if (mySpriteRenderer != null) | |
{ | |
// flip the sprite | |
mySpriteRenderer.flipX = true; | |
attackPoint.position = new Vector3(leftattackPoint.position.x, leftattackPoint.position.y, attackPoint.position.z); | |
m_FacingRight = false; | |
} | |
} | |
if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow)) //space down | |
{ | |
mySpriteRenderer.flipX = false; | |
attackPoint.position = new Vector3(rightattackPoint.position.x, attackPoint.position.y, attackPoint.position.z); | |
m_FacingRight = true; | |
} | |
if (Input.GetKeyDown(KeyCode.Space)) //space down | |
{ | |
SoundManagerScript.PlaySound("Jump"); | |
player.OnJumpInputDown(); //call this method | |
} | |
if (Input.GetKeyUp(KeyCode.Space) || Input.GetKeyDown(KeyCode.W)) //release | |
{ | |
player.OnJumpInputUp(); //call this method | |
} | |
} | |
} |