Skip to content

Instantly share code, notes, and snippets.

@JonathanYin
Created July 17, 2017 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonathanYin/ac4b2f7c11ecb76366ea0242ead898a4 to your computer and use it in GitHub Desktop.
Save JonathanYin/ac4b2f7c11ecb76366ea0242ead898a4 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
using System.Diagnostics;
public class RobotController1 : 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("Horizontal2");
this.GetComponent<Rigidbody2D>().velocity = new Vector3(move * maxSpeed, this.GetComponent<Rigidbody2D>().velocity.y);
//set our speed
anim.SetFloat("Speed", Mathf.Abs(move));
//Gives us of one if we are moving via the arrow keys
//move our Players rigidbody
/*float speed = 6;
if (Input.GetKey(KeyCode.A))
transform.Translate(Vector3.left * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.D))
transform.Translate(Vector3.right * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.W))
transform.Translate(Vector3.up * speed * Time.deltaTime * 2);
*/
//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 ((Input.GetKey(KeyCode.A)) && !facingLeft)
{
Flip();
}
else if ((Input.GetKey(KeyCode.D)) && facingLeft)
{
Flip();
}*/
if (move < 0 && !facingLeft)
{
Flip();
}
else if (move > 0 && facingLeft)
{
Flip();
}
/*if (grounded && Input.GetKeyDown(KeyCode.Space) && Time.time > canJump)
{
this.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpForce));
anim.SetBool("Ground", false);
canJump = Time.time + 1.75f;
//grounded = false;
}*/
if (grounded && Input.GetKeyDown(KeyCode.W) && 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;
}
}
@JonathanYin
Copy link
Author

This script is essentially the same as RobotController, except for the fact that it uses the "Horizontal2" axis from Input Manager, allowing them to move using "W-A-D".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment