- Home /
This question was
closed Nov 10, 2014 at 08:53 PM by
robertbu for the following reason:
Duplicate Question
Question by
OctoSloths · Nov 10, 2014 at 08:53 PM ·
c#
2D character controller script not working?
Hey, I'm trying to make a 2D character controller, I'm running into a lot of errors, does anyone see what the issue is?
using UnityEngine;
using System.Collections;
public class RobotControllerScript : MonoBehaviour
{
public float maxSpeed = 10f;
bool facingRight = true;
Animator anim;
bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask whatIsGround;
public float jumpForce = 700f;
bool doubleJump = false;
void Start ()
{
anim = GetComponent<Animator> ();
}
void FixedUpdate ()
{
grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
anim.SetBool ("Ground", grounded);
if (grounded)
doubleJump = false;
anim.SetFloat ("vSpeed", Rigidbody2D.velocity.y);
if(!grounded) return;
float move = Input.GetAxis ("Horizontal");
anim.SetFloat ("Speed", Mathf.Abs (move));
Rigidbody2D.velocity = new Vector2(move + maxSpeed, Rigidbody2D.velocity.y);
if (move > 0 && !facingRight)
Filp ();
else if (move < 0 && facingRight)
Filp ();
}
void Update ()
{
if ((grounded || !doubleJump) && Input.GetKeyDown (KeyCode.Space))
{
anim.SetBool("Ground", false);
Rigidbody2D.AddForce(new Vector2(0, jumpForce));
if(!doubleJump && !grounded)
doubleJump = true;
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = Transform.localScale;
theScale.x *= -1;
Transform.localScale = theScale;
}
}
Comment
This question isn't descriptive enough. In what way does it not work as expected? Please give us a clue when asking questions here.
Answer by robertbu · Nov 10, 2014 at 08:55 PM
For future questions, please include copies of your error messages.
References to your Rigidbody component should use 'rigidbody' with a lower case 'r'. I see this problem on lines 31, 38 and 50.
References to your Transform, should use 'transform' with a lower case 't'. I see this problem on lines 58 and 60.
You've misspelled 'Flip()'.