I'm new to programming please help. Error between 1 - 42
using UnityEngine;
public class Controls : MonoBehaviour {
public float horizontalSpeed;
float speedX;
public float verticalImpulse;
Rigidbody2D rb;
bool isGrounded;
void Start () {
rb = GetComponent();
}
void FixedUpdate () {
if (Input.GetKey(KeyCode.A))
{
speedX = -horizontalSpeed;
}
else if (Input.GetKey(KeyCode.D))
{
speedX = horizontalSpeed;
}
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
rb.AddForce(new Vector2(0, verticalImpulse), ForceMode2D.Impulse);
}
transform.Translate(speedX, 0, 0);
speedX = 0;
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Ground")
isGrounded = true;
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.tag == "Ground")
isGrounded = false;
private bool faceRight = true;
if (moveX > 0 && !faceRight)
flip ();
else if (moveX < 0 && faceRight)
flip ();
}
void flip () {
faceRight = !faceRight;
transform.localScale = new Vector3 (transform.localScale.x * -1, transform.localScale.y, transform.localScale.z);
}
}
↧