Search Unity

Question I need help with my Rigidbody

Discussion in 'Getting Started' started by Sekudo, Jan 11, 2023.

  1. Sekudo

    Sekudo

    Joined:
    Dec 4, 2022
    Posts:
    36
    so, forgive me if this is really basic, i barely know how to use c# and just started using it. (i used a tutorial for this.) in my script, (shown below) on the last few lines, i get 4 different errors. i thought it was because something wasn't defined, but i know i still defined my rigidbody on line 27?

    #1,

    Assets/Scripts/PlayerMovement.cs(70,16): error CS1061: 'Rigidbody' does not contain a definition for 'Move' and no accessible extension method 'Move' accepting a first argument of type 'Rigidbody' could be found (are you missing a using directive or an assembly reference?)

    #2,
    Assets/Scripts/PlayerMovement.cs(67,16): error CS1061: 'Rigidbody' does not contain a definition for 'Rotate' and no accessible extension method 'Rotate' accepting a first argument of type 'Rigidbody' could be found (are you missing a using directive or an assembly reference?)

    #3,
    Assets/Scripts/PlayerMovement.cs(61,13): error CS0103: The name 'velocity' does not exist in the current context

    and #4, (this message was given to me 3 different times in different places with 2 on line 65 (different character spots) and 1 on 63)

    Assets/Scripts/PlayerMovement.cs(65,58): error CS0103: The name 'direction' does not exist in the current context

    if anyone is able to help, i would really appreciate it. now, here is the script.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     public CharacterController controller;
    8.     public Transform cam;
    9.  
    10.     public float speed = 6f;
    11.     public float gravity = -9.81f;
    12.     public float jumpHeight = 3f;
    13.  
    14.     public Transform groundCheck;
    15.     public float groundDistance = 0.4f;
    16.     public LayerMask groundMask;
    17.  
    18.     public Rigidbody rb;
    19.  
    20.     bool isGrounded;
    21.     bool jump;
    22.     public float turnSmoothTime = 0.1f;
    23.     float turnSmoothVelocity;
    24.  
    25.     private void Start()
    26.     {
    27.         rb = GetComponent<Rigidbody>();
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update()
    32.     {
    33.         if (Input.GetKeyDown("Jump"))
    34.         {
    35.             jump = true;
    36.         }
    37.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    38.  
    39.         if (Input.GetButtonDown("Jump"))
    40.         {
    41.             //better to do with onCollisionStay and OnCollisionExit, but fine for now. Also a Jump timer/cool off would be good idea
    42.             isGrounded = false;
    43.         }
    44.  
    45.         float horizontal = Input.GetAxisRaw("Horizontal");
    46.         float vertical = Input.GetAxisRaw("Vertical");
    47.         Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
    48.  
    49.     }
    50.  
    51.     void FixedUpdate()
    52.     {
    53.         Vector3 ve = rb.velocity;
    54.         ve.y += gravity * Time.deltaTime;
    55.         rb.velocity = ve;
    56.  
    57.         if (jump && isGrounded)
    58.         {
    59.             jump = false;
    60.             isGrounded = false;
    61.             velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    62.         }
    63.         if (direction.magnitude >= 0.1f)
    64.         {
    65.             float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
    66.             float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
    67.             rb.Rotate(Quaternion.Euler(0f, angle, 0f));
    68.  
    69.             Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
    70.             rb.Move(moveDir.normalized * speed * Time.deltaTime + transform.position);
    71.         }
    72.     }
    73. }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    The error tell you the exact line and column numbers and a description and saying Rigidbody doesn't have a "Move" method which of course it doesn't. Use the scripting reference to look at what's there: https://docs.unity3d.com/ScriptReference/Rigidbody.html You likely want MovePosition.

    The other errors are similar. You're using "direction" but that's defined in another method and not the one you're using.

    The forums though are not the place to give you a tutorial on how to use the language so I would highly recommend you follow some basic programming tutorials. Certainly you need to understand the scope of variables and look at the scripting reference if you're told something doesn't exist. Also, if you've not already, use a decent IDE which provides you the ability to show the available properties/methods on any type.

    There are lots of other erros in the code TBH.
     
  3. RichAllen2023

    RichAllen2023

    Joined:
    Jul 19, 2016
    Posts:
    1,026
    I hate to be pedantic but there's a LOT of typos in the code, C# is VERY case and spelling sensitive, if you mis-spell a word it definitely won't thank you.
     
    ALEXWARELLC likes this.
  4. Sekudo

    Sekudo

    Joined:
    Dec 4, 2022
    Posts:
    36
    thank you! used that to help fix me being confused after one of melvmay's suggestions didn't work
     
  5. Sekudo

    Sekudo

    Joined:
    Dec 4, 2022
    Posts:
    36
    thank you for the moveposition tip, it helped me a ton with the specified one and i was able to use MoveRotation instead. i was thinking that since direction didn't exist in the documentation you sent that it would be rotation instead of direction, so i replaced each direction with rotation, yet it still didn't compile. i also tried putting rb in front of them (like rb.rotation instead of rotation) but still had a similar issue, and the velocity one i don't understand since it is correct. i'm trying to figure why it doesn't show up, like maybe i need to reference it at the top or something, but if you're able to help one last time i would really appreciate it. if possible, can you also tell me an easy-to-understand coding guide, since the only reason i'm going here anyway is the reason i can't find one i'm able to understand. thank you!
     
  6. jbnlwilliams1

    jbnlwilliams1

    Joined:
    May 21, 2019
    Posts:
    267
  7. Sekudo

    Sekudo

    Joined:
    Dec 4, 2022
    Posts:
    36