1
\$\begingroup\$

Been trying to get my player control working with a wall jump. I'm only getting errors from my rigidbody2D.velocity.y; the error states

are you missing a using directive or an assembly reference.

My script is

using System;
using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{

    public bool onGround = false;
    public bool jump = false;
    public float maxSpeedX = 3f;
    public float maxSpeedY = 3f;
    public LayerMask whatIsGround;
    public Transform groundChecker;

    private float groundRadius = 0.1f;
    private bool facingRight = true;
    public bool touchLeft = false;
    public bool touchRight = false;
    private CircleCollider2D circleCollider;

    public float velocityX;
    public float velocityY;

    void Start()
    {
        circleCollider = GetComponent<CircleCollider2D>();
    }

    void FixedUpdate()
    {
        float forceX = 0;
        float forceY = 0;

        //Je test si mon personnage est collé à un mur
        checkWallPosition();

        onGround = Physics2D.OverlapCircle(groundChecker.position, groundRadius, whatIsGround);
        float moveH = Input.GetAxisRaw("Horizontal");
        jump = Input.GetButtonDown("Jump");

        //Si je touche le sol
        if (onGround)
        {
            if (moveH != 0)
            {
                forceX = maxSpeedX * moveH;
            }
            if (jump && !touchLeft && !touchRight)
            {
                forceY = maxSpeedY;
            }
            else if (jump && (touchLeft || touchRight))
            {
                if (touchLeft)
                {
                    forceX = maxSpeedX;
                }
                else
                {
                    forceX = -maxSpeedX;
                }
                forceY = maxSpeedY;
                Flip();
            }
            else
            {
                forceY = GetComponent<Rigidbody2D>.velocityY;
            }
        }


        //Si je ne touche pas le sol
        else
        {
            if (!jump && !touchLeft && !touchRight)
            {
                forceY = rigidbody2D.velocity.y;
                forceX = rigidbody2D.velocity.x;
            }
            else if (jump && (touchLeft || touchRight))
            {
                if (touchLeft)
                {
                    forceX = maxSpeedX;
                    if (!facingRight)
                        Flip();
                }
                else
                {
                    forceX = -maxSpeedX;
                    if (facingRight)
                        Flip();
                }
                forceY = maxSpeedY;
            }
            else if (touchLeft || touchRight)
            {
                if (moveH == 0)
                {
                    forceX = 0;
                    forceY = rigidbody2D.velocity.y;
                }
                else
                {
                    if ((touchLeft && moveH == -1) || (touchRight && moveH == 1))
                    {
                        if (moveH == -1 && facingRight)
                        {
                            Flip();
                        }
                        if (moveH == 1 && !facingRight)
                        {
                            Flip();
                        }
                        forceY = rigidbody2D.velocity.y / 3;
                    }
                }
            }
        }


        //Ici les forces sont appliquées
        rigidbody2D.velocity = new Vector2(forceX, forceY);


        //Ici le personnage est retourné en fonction de sa position
        if (onGround)
        {
            if (moveH > 0 && !facingRight)
            {
                Flip();
            }
            else if (moveH < 0 && facingRight)
            {
                Flip();
            }
        }

    }

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

    private void checkWallPosition()
    {
        touchLeft = touchRight = false;
        float yCircle = transform.localPosition.y + (circleCollider.center.y * transform.localScale.y);
        Vector3 circlePosition = new Vector3(transform.localPosition.x, yCircle, transform.localPosition.z);
        bool hitRight = Physics2D.Raycast(circlePosition, transform.right, circleCollider.radius * transform.localScale.y + transform.localScale.y, 1 << LayerMask.NameToLayer("Walls"));
        bool hitLeft = Physics2D.Raycast(circlePosition, -transform.right, circleCollider.radius * transform.localScale.y + transform.localScale.y, 1 << LayerMask.NameToLayer("Walls"));
        if (hitRight)
        {
            touchRight = true;
        }
        if (hitLeft)
        {
            touchLeft = true;
        }
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Are you defining rigidbody2D anywhere? Recent versions of Unity deprecated the properties that access common components, so you may need to define this as a member variable and populate it with a GetComponent<Rigidbody2D>() call yourself. \$\endgroup\$ – DMGregory Sep 16 '16 at 16:23
1
\$\begingroup\$

GameObject.rigidbody and GameObject.rigidbody2d have both been removed as of Unity 5.5.0p1.

Rigidbody2d rb = GetComponent<Rigidbody2D>();
forceY = rb.velocity.y;
\$\endgroup\$
0
\$\begingroup\$

This sometimes occurs and if all imports are correct. Try restarting unity editor usually that does the job with me

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.