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. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Question curly brackets are gltiching

Discussion in 'Scripting' started by joe2500, Nov 24, 2022.

  1. joe2500

    joe2500

    Joined:
    Apr 28, 2018
    Posts:
    1
    So, the curly brackets are not matching up together correectly. Im trying to put movement code into the IsMine statement. Im a beginner. When i do, the parenthesy connect to the one that has all the code in it. (line 6) That one is not the one im trying to connect it to even though there are enoguh brackets to go around.


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using TMPro;
    using Photon.Pun;
    public class PlayerMovementTutorial : MonoBehaviour {
    PhotonView view;

    [Header("Movement")]
    public float moveSpeed;

    public float groundDrag;

    public float jumpForce;
    public float jumpCooldown;
    public float airMultiplier;
    bool readyToJump;

    [HideInInspector] public float walkSpeed;
    [HideInInspector] public float sprintSpeed;

    [Header("Keybinds")]
    public KeyCode jumpKey = KeyCode.Space;

    [Header("Ground Check")]
    public float playerHeight;
    public LayerMask whatIsGround;
    bool grounded;

    public Transform orientation;

    float horizontalInput;
    float verticalInput;

    Vector3 moveDirection;

    Rigidbody rb;

    private void Start()
    {
    rb = GetComponent<Rigidbody>();
    rb.freezeRotation = true;

    readyToJump = true;
    view = GetComponent<PhotonView>();
    }

    if (view.IsMine) {

    private void Update()
    {
    // ground check
    grounded = Physics.Raycast(transform.position, Vector3.down, playerHeight * 0.5f + 0.3f, whatIsGround);

    MyInput();
    SpeedControl();

    // handle drag
    if (grounded)
    rb.drag = groundDrag;
    else
    rb.drag = 0;
    }

    private void FixedUpdate()
    {
    MovePlayer();
    }

    private void MyInput()
    {
    horizontalInput = Input.GetAxisRaw("Horizontal");
    verticalInput = Input.GetAxisRaw("Vertical");

    // when to jump
    if (Input.GetKey(jumpKey) && readyToJump && grounded)
    {
    readyToJump = false;

    Jump();

    Invoke(nameof(ResetJump), jumpCooldown);
    }
    }

    private void MovePlayer()
    {
    // calculate movement direction
    moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;

    // on ground
    if (grounded)
    rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);

    // in air
    else if (!grounded)
    rb.AddForce(moveDirection.normalized * moveSpeed * 10f * airMultiplier, ForceMode.Force);
    }

    private void SpeedControl()
    {
    Vector3 flatVel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);

    // limit velocity if needed
    if (flatVel.magnitude > moveSpeed)
    {
    Vector3 limitedVel = flatVel.normalized * moveSpeed;
    rb.velocity = new Vector3(limitedVel.x, rb.velocity.y, limitedVel.z);
    }
    }

    private void Jump()
    {
    // reset y velocity
    rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);

    rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
    }
    private void ResetJump()
    {
    readyToJump = true;
    }

    }
    }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    8,629
    Please edit your post to use code-tags. Also, your post isn't about the Editor is it so please use the Scripting forum.

    I'll move your post for you.

    I presume by the odd description of "curly brackets are glitching" you mean you get a compiler error? So you know, devs are not going to compile it in their heads so you must always post the full compiler error which includes the actual line and columns numbers. I presume also that you've already checked the code over at those lines indicated before posting and asking someone else to?

    Does this look normal to you? It doesn't to me. :)
    Code (CSharp):
    1. if (view.IsMine) {
    2.  
    3. private void Update()
     
    Bunny83 likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    29,825
    You are just making massive amounts of typing mistakes.

    You can fix those all by yourself, no need to post to the forum. Here's how:

    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.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

    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.

    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.

    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, don't post here... just go fix your errors! Start back at the top of this thread.
     
    Bunny83 likes this.