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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

An instance of type 'UnityEngine.Rigidbody' is required to access non static member 'velocity'

Discussion in 'Scripting' started by Albertounitytutoriales, May 29, 2015.

  1. Albertounitytutoriales

    Albertounitytutoriales

    Joined:
    May 29, 2015
    Posts:
    7
    Hello! I have this problem in my PlayerScript. Please Help me. I´m using Unity 5. This is my script:

    var walkAcceleration : float = 5;
    var walkDeacceleration : float = 5;
    @HideInInspector
    var walkDeaccelerationVolx : float;
    @HideInInspector
    var walkDeaccelerationVolz : float;
    var cameraObject : GameObject;
    var maxWalkSpeed : float = 20;
    @HideInInspector
    var horizontalMovement : Vector2;
    var jumpVelocity : float = 700;
    @HideInInspector
    var grounded : boolean = false;
    var maxSlope : float =60;

    function Update ()
    {
    horizontalMovement = Vector2(GetComponent.<Rigidbody>().velocity.x, GetComponent.<Rigidbody>().velocity.z);
    if(horizontalMovement.magnitude > maxWalkSpeed)
    {
    horizontalMovement = horizontalMovement.normalized;
    horizontalMovement *= maxWalkSpeed;
    }

    GetComponent.<Rigidbody>().velocity.x = horizontalMovement.x;
    GetComponent.<Rigidbody>().velocity.z = horizontalMovement.y;

    if(Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0 && grounded){
    GetComponent.<Rigidbody>().velocity.x = Mathf.SmoothDamp(Rigidbody.velocity.x, 0, walkDeaccelerationVolx, walkDeacceleration); // Here is the error
    GetComponent.<Rigidbody>().velocity.z = Mathf.SmoothDamp(Rigidbody.velocity.z, 0, walkDeaccelerationVolz, walkDeacceleration);} // Here is the error

    transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(MouseLookScript).RotacionActualY, 0);
    GetComponent.<Rigidbody>().AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * Time.deltaTime , 0, Input.GetAxis("Vertical") * walkAcceleration * Time.deltaTime);

    if(Input.GetButtonDown("Jump") && grounded)
    GetComponent.<Rigidbody>().AddForce(0,jumpVelocity,0);

    }

    function OnCollisionStay (collision : Collision)
    {
    for(var contact : ContactPoint in collision.contacts)
    {
    if(Vector3.Angle(contact.normal, Vector3.up) < maxSlope)
    grounded = true;

    }
    }

    function OnCollisionExit ()
    {
    grounded = false;
    }
     
    Last edited: May 29, 2015
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,851
    HI.

    Please use code tags when pasting code, it makes it much easier to read ;)
    The problem is Rigidbody.velocity.x and other variations of this. You need to get the rigidbody component.
    E.G
    Code (csharp):
    1. GetComponent<Rigidbody>().velocity.x
     
    prietokevin likes this.
  3. Albertounitytutoriales

    Albertounitytutoriales

    Joined:
    May 29, 2015
    Posts:
    7
    what´s code tags? Sorry but I don´t understand what is this? I´m only 14 years
     
  4. Albertounitytutoriales

    Albertounitytutoriales

    Joined:
    May 29, 2015
    Posts:
    7
    karl.jones
    your answer is the same as that put in the script
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,200
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,851
    If you used code tags I could give you a line number ;)

    This

    Code (csharp):
    1. Mathf.SmoothDamp(Rigidbody.velocity.x, 0, walkDeaccelerationVolx, walkDeacceleration);
    Should be

    Code (csharp):
    1. Mathf.SmoothDamp(GetComponent.<Rigidbody>().velocity.x, 0, walkDeaccelerationVolx, walkDeacceleration);
    Read this for information about code tags.