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

Complete noob looking for feedback

Discussion in 'Scripting' started by fraserspree_unity, Aug 14, 2022.

?

Am I on the right track?

  1. Yes

  2. No

  3. You're posting in the wrong place of the forum

Results are only viewable after voting.
  1. fraserspree_unity

    fraserspree_unity

    Joined:
    Aug 10, 2022
    Posts:
    2
    Hi Everyone,

    I'm new to Game Development and coding in general and been having a go at teaching myself.

    I'm trying to make a game like GTA and I'm wanting to set up my player script to handle the basic movement, movement modifiers such as sprint jump crouch,
    open some UI for map and inventory.

    I've pieced together a script for my player using examples. I'd like some feedback on it and hopefully some guidance to completing the script. Especially how to get my Jump to work while sprinting

    I have pasted it below. I'm using Unity 2021.3.7f1

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerScript : MonoBehaviour
    6. {
    7.     /*          GOALS
    8.    
    9.      PLAYER MOVEMENT WITH WASD KEYS
    10.         W+S to Move Forward/Backward COMPLETE
    11.         A+D to Rotate the Player     COMPLETE
    12.    
    13.      PLAYER MOVEMENT MODIFIERS
    14.         Hold Shift to Sprint         COMPLETE **Wont Jump While Sprinting**
    15.         Space to Jump                COMPLETE **Wont Jump While Sprinting**
    16.         Ctrl to Crouch               --------
    17.         Q to Enter/Exit Cover        --------
    18.  
    19.      PLAYER INTERACTIVE FUNCTIONS
    20.         Tab to Draw and Holster      --------
    21.         F to Enter/Exit Vehicle      --------
    22.         I to Open/Close Inventory    --------
    23.         E to Interact                --------
    24.         M to Open/Close World Map    --------
    25.     */
    26.  
    27.     // FLOATS
    28.     public float walkSpeed = 5.0f;
    29.     public float runSpeed = 10.0f;
    30.     public float rotationSpeed = 250.0f;
    31.     public float gravity = -20.0f;
    32.     public float jumpHeight = 1.5f;
    33.  
    34.     // BOOLS
    35.     public bool isGrounded;
    36.  
    37.     // REFERENCES
    38.     CharacterController characterController;
    39.     Vector3 moveVelocity;
    40.     Vector3 turnVelocity;
    41.            
    42.    
    43.     void Awake()
    44.     {
    45.         characterController = GetComponent<CharacterController>();
    46.     }
    47.     void Update()
    48.     {  
    49.  
    50.         // Input Vertical and Horizontal Axis
    51.         var hInput = Input.GetAxis("Horizontal");
    52.         var vInput = Input.GetAxis("Vertical");
    53.    
    54.         // Gravity
    55.         moveVelocity.y += gravity * Time.deltaTime;
    56.         characterController.Move(moveVelocity * Time.deltaTime);
    57.         transform.Rotate(turnVelocity * Time.deltaTime);
    58.  
    59.         //Move, Jump, Sprint, Crouch, Climb
    60.         if(characterController.isGrounded)
    61.         {  
    62.             //Player Movement By Input Axis
    63.             moveVelocity = transform.forward * walkSpeed * vInput;
    64.             turnVelocity = transform.up * rotationSpeed * hInput;
    65.  
    66.             //Jump
    67.             if(Input.GetKeyDown(KeyCode.Space))
    68.             {moveVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravity);}
    69.  
    70.             //Sprint While Holding LShift Key
    71.             if(Input.GetKey("left shift"))
    72.             {moveVelocity = transform.forward * runSpeed * vInput;}
    73.  
    74.             //Toggle Crouch with Ctrl Key
    75.  
    76.             //Climb
    77.             /*Draw, Holster Equipped Item
    78.             if(characterController.isGrounded)
    79.        
    80.             //Draw Item
    81.             if(Input.GetKey("tab"))
    82.             {
    83.                            
    84.             }
    85.  
    86.             */
    87.         }
    88.     }
    89. }
     
  2. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    It looks fine. You'll probably end up rewriting it in any case when you learn more about the game you're making.
     
    fraserspree_unity likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,965
    While I admire your pluck and courage, you might want to start with making at least a half dozen or even a dozen incredibly simple games first: pong, space invaders, snake, flappy birds, angry birds, etc. simple simple simple stuff.

    Starting with GTA would be equivalent to making a Space Shuttle for your very first home-built airplane. Yeah, you could do it, but wow, wouldn't it be easier to start with a little ultralight airplane?

    Beyond that, expose yourself to every possible simple game tutorial you can find on Youtube. GO!

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

    How to do tutorials properly, two (2) simple steps to success:

    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 step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    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 your error. Google is your friend here. Do NOT continue until you fix your error. Your 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.

    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)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    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.
     
    fraserspree_unity likes this.
  4. fraserspree_unity

    fraserspree_unity

    Joined:
    Aug 10, 2022
    Posts:
    2
    Thank you so much for the advice, ive actually saved the steps and will use them.

    thanks for taking the time to help.