Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

I have no idea what I'm doing wrong...could some one please help me troubleshoot?

Discussion in 'Scripting' started by timberpatton, Oct 24, 2021.

  1. timberpatton

    timberpatton

    Joined:
    Oct 24, 2021
    Posts:
    2
    I copied some one for this script directly and it's continuously giving me error after error and it always changes. what am I doing wrong with this script for player movement and camera control? (i haven't added jumping yet)






    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerController : MonoBehaviour
    {

    [SerializeField] Transform playerCamera = null;
    [SerializeField] float mouseSensitivity = 3.5f;
    [SerializeField] float walkspeed = 6.0f;
    [SerializeField] float gravity = -13.0f;
    [SerializeField] [Range(0.0f, 0.5f)] float moveSmoothTime = 0.3f;
    [SerializeField] [Range(0.0f, 0.5f)] float mouseSmoothTime= 0.03f;

    [SerializeField] bool Cursor = true;

    float cameraPitch = 0.0f;
    float velocityY = 0.0f;
    CharacterController controller = null;

    Vector2 currentDir = Vector2.Zero;
    Vector2 currentDirVelocity = Vector2.zero;

    Vector2 currentMouseDelta = Vector2.Zero;
    Vector2 currentMouseDeltaVelocity = Vector2.Zero;

    void Start()
    {
    controller = GetCompnent<CharacterController>();
    if(lockcursor)
    {
    Cursor.lockState = CursorLockMode.Locked;
    Cursor.visible = false;
    }
    }


    void Update()
    {
    UpdateMouseLook();
    UpdateMovement();
    }

    void UpdateMouseLook()
    {
    vector2 targetMouseDelta = new vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));

    currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, targetMouseDelta, ref currentMouseDeltaVelocity, mouseSmoothTime);

    cameraPitch -= currentMouseDelta.y* mouseSensitivity;

    cameraPitch = Mathf.Clamp(cameraPitch, -90.0f, 90.0f);

    playerCamera.localEulerAngles = Vector3.right * cameraPitch;

    transform.Rotate(Vector3.up * currentmouseDelta.x * mouseSensitivity);



    }

    void UpdateMovement()
    {
    Vector2 targetDir = new Vector2(inputDir.GetAxisRaw("Horizontal"), inputDir.GetAxisRaw("Vertical"));
    targetDir.Normalize();

    currentDir = Vector2.SmoothDamp(currentDir, targetDir, ref currentDirVelocity, moveSmoothTime);

    if (controller.isGrounded)
    velocityY = 0.0f;

    velocityY += gravity * Time.deltaTime;

    Vector3 velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * walkSpeed + Vector3.up * velocityY;

    controller.Move(velocity * Time.deltaTime);

    }
    }
     
  2. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    497
    Vector2.Zero;
    should be
    Vector2.zero;

    GetCompnent
    should be
    GetComponent

    vector2
    should be
    Vector2

    inputDir
    has not be defined anywhere.

    If you are going to try to write a script you need to get all the capitalisation and spelling correct for everything. This looks like stuff has just been thrown together without any understanding of C# or the Unity methods. I would recommend doing some tutorials so that you get better acquainted with Unity.
    Also when you post code you should put it in code tags (it tells you how to do that in the guidelines for the forum).
    If you are getting errors, then you should also be posting the entire error message (don't paraphrase anything, copy and paste the actual message including the numbers between the brackets as that identifies the line/character location which is helpful when you post your script in code tags).

    There are potentially more errors in that script, but the ones I highlighted are the ones that I could see straight away.
     
    timberpatton likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Here is a super-basic starter prototype FPS based on Character Controller (BasicFPCC):

    https://forum.unity.com/threads/a-basic-first-person-character-controller-for-prototyping.1169491/

    That one has run, walk, jump, slide, crouch... it's crazy-nutty!!

    If you do decide to go typing instead of copy / pasting, you have to step your typing game up to 100% accuracy, not even one error allowed.

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly:

    Tutorials are a GREAT idea. Tutorials should be used this way:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly. Fortunately this is the easiest part to get right.

    Be a robot. Don't make any mistakes. BE PERFECT IN EVERYTHING YOU DO HERE.

    If you get any errors, learn how to read the error code and fix it. Google is your friend here. Do NOT continue until you fix the error. The error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

    Finally, when you have errors...

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.
     
    timberpatton likes this.
  4. timberpatton

    timberpatton

    Joined:
    Oct 24, 2021
    Posts:
    2
    Okay thank you both. I'll look at the guidelines, I had no idea.
    Also, thank you for the prototype and as for the tutorial video I followed...yeah I'll have to go through and I dissect it. I found a C# tutorial for beginners (by Programming with Mosh) so I'll start there. Initially, I didn't think I would have to code to use Unity, but it's clear to me that I will be too limited if I don't learn C#. As for typing mistakes...I need a new keyboard because it likes to double press "wasd" and throw random letters in sometimes.

    Again thank you both!
     
    Kurt-Dekker likes this.