W
W
wirtun2018-01-16 07:53:44
2D
wirtun, 2018-01-16 07:53:44

Error in creating 2d platformer on unity. How to fix?

5a5d84f95e83b931983421.png
There were two errors in creating a script to control a character in a unit. The entire code can be read here:

using UnityEngine;
using System.Collections;

public class characterController : MonoBehaviour {
    public float maxSpeed = 10f;
    public float jumpForce = 700f;
    bool facingRight = true;
    bool grounded = false;
    public Transform groundCheck;
    public float groundRadius = 0.2f;
    public LayerMask whatIsGround;

    public float move;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void FixedUpdate () {

        grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);

        move = Input.GetAxis ("Horizontal");

    }

    void Update(){
        if (grounded && (Input.GetKeyDown (KeyCode.W)||Input.GetKeyDown (KeyCode.UpArrow))) {

            rigidbody2D.AddForce (new Vector2(0f,jumpForce));
        }
        rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);

        if (move > 0 && !facingRight)
            Flip ();
        else if (move < 0 && facingRight)
            Flip ();


        if (Input.GetKey(KeyCode.Escape))
        {
            Application.Quit();
        }

        if (Input.GetKey(KeyCode.R))
        {
            Application.LoadLevel(Application.loadedLevel);
        }

    }

    void Flip(){
        facingRight = !facingRight;
        Vector3 theScale = transform.localScale;
        theScale.x *= -1;
        transform.localScale = theScale;
    }              
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2018-01-16
@wirtun

But where are you climbing from ... Why immediately into game dev? Learn the language first!
How to fix it: carefully read the error, take an even closer look at the data types. Seriously. This is the initial level, and if you make mistakes at this stage, then everything will only get worse.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question