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

Question [error] CS0019 Operator '+' cannot be applied to operands of type 'Vector3' and 'float'

Discussion in 'Scripting' started by UnityAcct1625, Sep 28, 2022.

  1. UnityAcct1625

    UnityAcct1625

    Joined:
    Sep 28, 2022
    Posts:
    6
    Hey everyone, i've been working on Brackeys' Third Person Movement script to try and add gravity, but i keep getting this error, could anyone help me? Still a beginner, so sorry if there's any newbie mistakes

    code:

    Code (CSharp):
    1. using System.Collections;
    2.  
    3. using System.Collections.Generic;
    4.  
    5. using UnityEngine;
    6.  
    7.  
    8.  
    9. public class ThirdPersonMovement : MonoBehaviour
    10.  
    11. {
    12.  
    13.     public  CharacterController controller;
    14.  
    15.     public Transform cam;
    16.  
    17.  
    18.  
    19.     public float speed = 6f;
    20.     public float gravity = -9.81f;
    21.  
    22.     Vector3 velocity = new Vector3(0f, 1f, 0f).normalized;
    23.  
    24.  
    25.  
    26.     public float turnSmoothTime = 0.1f;
    27.  
    28.     float turnSmoothVelocity;
    29.    
    30.  
    31.     // Update is called once per frame
    32.  
    33.     void Update()
    34.  
    35.     {
    36.  
    37.         float horizontal = Input.GetAxisRaw("Horizontal");
    38.  
    39.         float Vertical = Input.GetAxisRaw("Vertical");
    40.  
    41.         Vector3 direction = new Vector3(horizontal, 0f, Vertical).normalized;
    42.  
    43.  
    44.  
    45.         if (direction.magnitude >= 0.1f)
    46.  
    47.         {
    48.  
    49.             float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
    50.  
    51.             float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
    52.  
    53.             transform.rotation = Quaternion.Euler(0f, angle, 0f);
    54.  
    55.  
    56.  
    57.             Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
    58.            
    59.          
    60.             controller.Move(moveDir.normalized * speed * Time.deltaTime);
    61.            
    62.             velocity.y += gravity + Time.deltaTime;
    63.  
    64.             controller.Move(velocity + Time.deltaTime);
    65.         }
    66.  
    67.     }
    68.  
    69. }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,622
    No need to ask for help, simply go back to the tutorial and follow it exactly. The above error, even if you don't know what it means, does tell you the exact line and column the error is on. You only then have to go back to the tutorial and look at what you should've typed.

    In the hope you'll go back to the error and look at the line:
    You typed this: "velocity + Time.deltaTime". It's likely this: "velocity * Time.deltaTime" but only you can determine that by looking at the tutorial.

    In short, look at the line/column the error is on. Go back to the tutorial and check what that line should be rather than waiting on a forum post. :)

    I would also suggest for when you really don't follow something, post the code without all those spaces so the line numbers match your script. That way, you can post the full error, including the line number and devs can see which line it means.

    Good luck!
     
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,572
    And while you're at it, check the line above as well. It's not a compiler error, but you did the same mistake as well. It would compile, but would not do what it should.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    Ya, just typos... but you can fix those easy-easy yourself. 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.

    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.

    Getting back to your tutorial, make sure you are doing it properly or you might be wasting your time!

    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... back you go to the top of this post!
     
  5. UnityAcct1625

    UnityAcct1625

    Joined:
    Sep 28, 2022
    Posts:
    6
    thanks everyone :)
     
    angrypenguin likes this.