Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

2D CHARACTER CONTROLLERS video problem with the script

Discussion in 'Scripting' started by CrossM, Mar 6, 2015.

  1. CrossM

    CrossM

    Joined:
    Mar 5, 2015
    Posts:
    4
    Hello Guys, can you please help me with the code used in the "2D CHARACTER CONTROLLERS" video.
    Here is the Code I have written, copying exactly the used on the video (at least I checked it a million times).
    But it gets two error on the lines i marked with ->, the following errors are:

    Error CS1061: 'UnityEngine.Component' does not contain a definition for 'velocity' and no extension method 'velocity' accepting a first argument of type 'UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?) (CS1061) (Assembly-CSharp-firstpass)

    and

    Error CS1061: 'UnityEngine.Component' does not contain a definition for 'AddForce' and no extension method 'AddForce' accepting a first argument of type 'UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?) (CS1061) (Assembly-CSharp-firstpass)

    I started using unity today, so I'm really noob, be really clear please :D

    using UnityEngine;
    using System.Collections;

    public class controlerobo : MonoBehaviour {
    public float maxSpeed = 10f;
    bool facingRight = true;

    bool grounded = false;
    public Transform groudcheck;
    float groundRadius = 0.1f;
    public LayerMask whatIsGround;

    public float jumpForce = 700f;

    Animator anim;

    // Use this for initialization
    void Start () {
    anim = GetComponent<Animator>();

    }

    // Update is called once per frame
    void FixedUpdate () {
    grounded = Physics2D.OverlapCircle (groudcheck.position, groundRadius, whatIsGround);
    anim.SetBool("ground",grounded);

    --------------------------------> anim.SetFloat("vSpeed", rigidbody2D.velocity.y); <------------------------

    float move = Input.GetAxis ("Horizontal");

    anim.SetFloat ("speed", Mathf.Abs (move));

    GetComponent<Rigidbody2D>().velocity = new Vector2 (move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
    if (move > 0 && !facingRight)
    Flip ();
    else if (move < 0 && facingRight)
    Flip ();
    }


    void Update(){

    if (grounded && Input.GetKeyDown (KeyCode.Space))
    {
    anim.SetBool("chao",false);
    ----------------------------> rigidbody2D.AddForce(new Vector2(0, jumpForce)); <----------------------------
    }

    }

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



    thanks :D
     
  2. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    The API's have changed in Unity 5, meaning there were tons of changes such as to rigidbody.
    Instead of just using "rigidbody.velocity...", you'll now need to get the component of it, either each time you use it or just use a variable for it.
    Code (CSharp):
    1. void Update()
    2. {
    3. Rigidbody2D myRigidbody = GetComponent<Rigidbody2D>().velocity.y...
    4. }
     
    CrandellWS and CrossM like this.
  3. CrossM

    CrossM

    Joined:
    Mar 5, 2015
    Posts:
    4
    My code changed automaticaly and I didn't understand why, now I know.
    Thanks for the answer. :D
     
  4. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    Your code changed as Unity has a system which automatically upgrades/updates obsolete codes to the new API being used (the one I told you) :)
    No problem at all!
     
  5. Icorre

    Icorre

    Joined:
    Apr 13, 2015
    Posts:
    1
    Sorry to bring up an already answered question,

    But I am having the same issue "Error CS1061: 'UnityEngine.Component' does not contain a definition for 'velocity'..."
    I've tried the fix mention above using .velocty, .velocity.x and .velocity.y. All three variants give me the error:
    "Cannont implicity convert type float to UnityEngine.Rigidbody2D' (CS0029)"