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

Hello I am 13 and just started learning and need some help

Discussion in 'Editor & General Support' started by BubbaJrMSA, Aug 28, 2020.

  1. BubbaJrMSA

    BubbaJrMSA

    Joined:
    Aug 28, 2020
    Posts:
    2
    It says there ins a compiler error and my public floats wont appear under my script component, Here is my code did I miss something?

    using UnityEngine;
    public class SwindleFPC : MonoBehaviour
    {
    public Transform camera;
    public Rigidbody rb;
    public float camRotationSpeed = 5f;
    public float cameraMinimumY = -60f;
    public float cameraMaximumY = 75f;
    public float rotationSmoothSpeed = 10f;
    public float walkSpeed = 9f;
    public float runSpeed = 14f;
    public float maxSpeed = 20f;
    public float jumpPower = 30f;
    public float extraGravity = 45;
    float bodyRotationX;
    float camRotationY;
    Vector3 directionIntentX;
    Vector3 directionIntentY;
    float speed;
    public bool grounded;
    void Update()
    {
    LookRotation();
    }
    void LookRotation()
    {
    Cursor.visible = false;
    Cursor.lockState = CursorLockMode.Locked;
    //Get camera and bode rotational values
    bodyRotationX += Input.GetAxis("Mouse X") * camRotationSpeed;
    camRotationY += Input.GetAxis("Mouse Y") * camRotationSpeed;
    //Stop our camra from rotating 360 degrees
    camRotationY = Mathf.Clamp(camRotationY, cameraMinimumY, cameraMaximumY):
    //create rotation targets and handle rotations of the body and camera
    Quaternion camTargetRotation = Quaternion.Euler(-camRotationY, 0, 0);
    Quaternion bodyTargetRotation = Quaternion.Euler(0, bodyRotationX, 0);
    //handle rotations
    transform.rotation = Quaternion.Lerp(Transform.rotation, bodyTargetRotation, Time.deltaTime * rotationSmoothSpeed);
    camera.localRotation = Quaternion.Lerp(camera.localRotation, camTargetRotation, Time.deltaTime * rotationSmoothSpeed);
    }
    }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Nothing is going to work until you fix that error. If you don't understand the error feel free to copy and paste the entire error message here and we can help you fix it.
     
    Joe-Censored likes this.
  3. BetaMark

    BetaMark

    Joined:
    Sep 27, 2014
    Posts:
    229
    I copy/pasted your code above into my Rider C# editor and it told me about three things right off the bat:

    1) Line 33 needs to end with a semi-colon -- you have a colon. I make the same mistake all the time :)
    2) on line 38 -- Transform.rotation is attempting to get information from the Transform Class -- what I think you meant was to get was the current transform on the gameObject that your code is attached to. To do that -- you can use the shorthand of
    transform.rotation
    instead.
    3) this last thing was just a warning -- but Rider told me that the name you use for your public transform
    camera 
    was hiding values under it (long explanation here). It would be best to rename that to something like
    myCamera 
    up top around line 5 and also at the end of your code around line 40 just avoid having unexpected issues down the road.

    At 13, you are doing great just coming on the forum and asking for help -- so keep working on your game and asking questions when you need help.

    edit: completed monobehavior class:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class SwindleFPC : MonoBehaviour
    4. {
    5.     public Transform myCamera;
    6.     public Rigidbody rb;
    7.     public float camRotationSpeed = 5f;
    8.     public float cameraMinimumY = -60f;
    9.     public float cameraMaximumY = 75f;
    10.     public float rotationSmoothSpeed = 10f;
    11.     public float walkSpeed = 9f;
    12.     public float runSpeed = 14f;
    13.     public float maxSpeed = 20f;
    14.     public float jumpPower = 30f;
    15.     public float extraGravity = 45;
    16.     float bodyRotationX;
    17.     float camRotationY;
    18.     Vector3 directionIntentX;
    19.     Vector3 directionIntentY;
    20.     float speed;
    21.     public bool grounded;
    22.  
    23.     void Update()
    24.     {
    25.         LookRotation();
    26.     }
    27.  
    28.     void LookRotation()
    29.     {
    30.         Cursor.visible = false;
    31.         Cursor.lockState = CursorLockMode.Locked;
    32.         //Get camera and bode rotational values
    33.         bodyRotationX += Input.GetAxis("Mouse X") * camRotationSpeed;
    34.         camRotationY += Input.GetAxis("Mouse Y") * camRotationSpeed;
    35.         //Stop our camra from rotating 360 degrees
    36.         camRotationY = Mathf.Clamp(camRotationY, cameraMinimumY, cameraMaximumY);
    37.         //create rotation targets and handle rotations of the body and camera
    38.         Quaternion camTargetRotation = Quaternion.Euler(-camRotationY, 0, 0);
    39.         Quaternion bodyTargetRotation = Quaternion.Euler(0, bodyRotationX, 0);
    40.         //handle rotations
    41.         transform.rotation =
    42.             Quaternion.Lerp(transform.rotation, bodyTargetRotation, Time.deltaTime * rotationSmoothSpeed);
    43.         myCamera.localRotation =
    44.             Quaternion.Lerp(myCamera.localRotation, camTargetRotation, Time.deltaTime * rotationSmoothSpeed);
    45.     }
    46. }
     
    MitchStan likes this.
  4. BubbaJrMSA

    BubbaJrMSA

    Joined:
    Aug 28, 2020
    Posts:
    2
    thanks for your help I will be sure to fix it.
     
  5. BetaMark

    BetaMark

    Joined:
    Sep 27, 2014
    Posts:
    229
    Let us know if you need more help! I just got into use this Rider editor and it gives me hints and helps me find errors a lot easier, so I'm happy to run any future code in it if you need.

    Also, I believe Rider is free for students if you want to check it out. It really has helped me learn Unity a lot faster and a lot of my heroes on youtube seem to be using it, so I figured I would give it a try and now I can't live without it.

    https://www.jetbrains.com/community/education/#students