Created
July 17, 2017 16:36
-
-
Save JonathanYin/882101b6c921ac00d15f718b0fa1bdbf 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 UnityEngine; | |
using System.Collections; | |
using System.Diagnostics; | |
public class RobotController : MonoBehaviour | |
{ | |
//This will be our maximum speed as we will always be multiplying by 1 | |
public static float maxSpeed = 9f; | |
//a boolean value to represent whether we are facing left or not | |
bool facingLeft = true; | |
//a value to represent our Animator | |
Animator anim; | |
// Use this for initialization | |
//to check ground and to have a jumpforce we can change in the editor | |
bool grounded; | |
public Transform groundCheck; | |
float groundRadius = 0.2f; | |
public LayerMask whatIsGround; | |
public float jumpForce = 450; | |
private Rigidbody myRigidbody; | |
private float canJump = 0f; | |
private void Starting() | |
{ | |
this.myRigidbody = this.GetComponent<Rigidbody>(); | |
} | |
void Start() | |
{ | |
//set anim to our animator | |
anim = GetComponent<Animator>(); | |
} | |
// Update is called once per frame (dependent on fps), FixedUpdate is used when dealing with Rigidbody (every fixed frame) | |
void FixedUpdate() | |
{ | |
grounded = false; | |
//set our vSpeed | |
anim.SetFloat("vSpeed", this.GetComponent<Rigidbody2D>().velocity.y); | |
//set our grounded bool | |
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround); | |
//set ground in our Animator to match grounded | |
anim.SetBool("Ground", grounded); | |
float move = Input.GetAxis("Horizontal1");//Gives us of one if we are moving via the arrow keys | |
//move our Players rigidbody | |
this.GetComponent<Rigidbody2D>().velocity = new Vector3(move * maxSpeed, this.GetComponent<Rigidbody2D>().velocity.y); | |
//set our speed | |
anim.SetFloat("Speed", Mathf.Abs(move)); | |
//if we are moving left but not facing left flip, and vice versa | |
if (move < 0 && !facingLeft) | |
{ | |
Flip(); | |
} | |
else if (move > 0 && facingLeft) | |
{ | |
Flip(); | |
} | |
if (grounded && Input.GetKeyDown(KeyCode.UpArrow) && Time.time > canJump) | |
{ | |
this.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpForce)); | |
anim.SetBool("Ground", false); | |
canJump = Time.time + 1.25f; | |
//grounded = false; | |
} | |
} | |
public virtual void ComputeVelocity() | |
{ | |
} | |
void Update() | |
{ | |
} | |
//flip if needed | |
void Flip() | |
{ | |
facingLeft = !facingLeft; | |
Vector3 theScale = transform.localScale; | |
theScale.x *= -1; | |
transform.localScale = theScale; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script, which is attached to the first player, allows them to move via the "Horizontal1" axis defined in the Input Manager settings. Thus, they are able to move using the arrow keys, and the void Flip method allows the character to constantly face the direction in which they are moving.