How do I fix this code below!? |
I was watching a Youtube video on making a Platfomer Game and I was writing a Jump and move left and Right code. I also DID ever thing he told me to but, I got a BUNCH of error and I don't know how to fix it. Please Help, here is the code below
using UnityEngine; using System.Collections;
public class PlayerController : MonoBehaviour {
//Movement
public float speed;
public float jump;
float moveVelocity;
//Grounded Vars
bool grounded = true;
void Update ()
{
//Jumping
if(Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Z) || Input.GetKeyDown(KeyCode.W)
)
if (grounded)
GetComponent <Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jump);
}
}
//Left Right Movement
if (Input.GetKey(KeyCode.LeftArrow) || (Input.GetKey(KeyCode.A) )
(
moveVelocity = speed:
)
if (Input.GetKey(KeyCode.RightArrow) || (Input.GetKey(KeyCode.D)
(
moveVelocity = speed
)
}
Please do help! Thank you!
Answer by Landern · Nov 30, 2016 at 08:28 PM
You need to learn the basics, you're using parenthesis where you need curly braces(code blocks), colons (:) instead of semi-colons (;), parenthesis where they're not needed, closing the class before completing the Update method and on and on.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
//Movement
public float speed;
public float jump;
float moveVelocity;
//Grounded Vars
bool grounded = true;
void Update()
{
//Jumping
if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Z) || Input.GetKeyDown(KeyCode.W))
{
if (grounded)
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jump);
}
//Left Right Movement
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
{
moveVelocity = speed;
}
if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
{
moveVelocity = speed;
}
}
}
Please compare what you posted and what i've posted.
Answer by UltraProCode · Nov 30, 2016 at 10:47 PM
Thank you so much! This explains all the errors I have made.Now I will learn the basics to c#. Again, thank you!