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

I need help for the new input system player controller

Discussion in 'Scripting' started by Noahthegen, Aug 8, 2021.

  1. Noahthegen

    Noahthegen

    Joined:
    Jan 20, 2021
    Posts:
    13
    this is my code

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

    public class PlayerController : MonoBehaviour
    {
    public float speed = 5;
    private Vector2 movementInput;

    // Update is called once per frame
    void Update()
    {
    transform.Translate(new Vector3(movementInput.x 0, movementInput.y) * speed * Time.deltaTime);
    }

    public void OnMove(InputAction.CallbackContext ctx) => movementInput = ctx.ReadValue<Vector2>();
    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    I am trying to read your mind but I just can't seem to do it. You say you want help and then you don't even say what you need!!

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    Use code tags:
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    Looking further at your code this is just a simple typo (you failed to type one of the characters), but because there is no formatting I cannot even tell you which line it is on.

    When you are monkey-banging tutorial code in, it has to be 100% perfect.

    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.

    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!
     
  4. Noahthegen

    Noahthegen

    Joined:
    Jan 20, 2021
    Posts:
    13
    Is it possible to rotate the camera with the new input system and if it is how can I do add that
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    Yes of course. Everything is possible.

    Steps to success:

    - read the input system and display its values until you have it 100% working properly

    Do not proceed until the above is working. Use Debug.Log() to see the values.

    If you can't get this working, go look for tutorials on the new input system. There are THOUSANDS.

    When the above is working, drive those outputs to whatever you want to use to "rotate the camera."

    If you are unsure about "rotate the camera," google is your friend. Why? Because the answer will NEVER just be code. There will be lots of little fiddly scene setup that nobody here can type.

    Screen Shot 2021-08-08 at 3.53.38 PM.png
    Screen Shot 2021-08-08 at 3.52.32 PM.png
     
  6. Noahthegen

    Noahthegen

    Joined:
    Jan 20, 2021
    Posts:
    13
    I keep getting a error saying
    Assets\Camera.cs(9,13): error CS0246: The type or namespace name 'Vector' could not be found (are you missing a using directive or an assembly reference?)

    and how can I fix it? this is my code Btw

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

    public class Camera : MonoBehaviour
    {
    private float looksenesitivity = 5;
    private Vector lookVector;
    private Vector3 rotation;
    private Vector2 moveVector;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
    Rotate();
    }
    public void onLook(InputAction.CallbackContext context)
    {
    lookVector = context.ReadValue<vector2>();
    }

    private void Rotate()
    {
    rotation.y = lookvector.x*lookSenesitivity*Time.delteTime;
    transform.localEulerAngles = rotation;
    }
    }
     
  7. Rin-Dev

    Rin-Dev

    Joined:
    Jun 24, 2014
    Posts:
    558
    That error code is LITERALLY telling you the issue.

    Do you really see nothing wrong with trying to define Vector and not something else? Especially since you have it written in lookVector = context.ReadValue<Vector2>();
     
  8. Noahthegen

    Noahthegen

    Joined:
    Jan 20, 2021
    Posts:
    13
    but how do you fix it I just started learning cs btw
     
  9. Rin-Dev

    Rin-Dev

    Joined:
    Jun 24, 2014
    Posts:
    558
    You type in the correct thing instead of just Vector.

    What do you see later on in your code, that I bolded in my last post, that seems different from just Vector?
     
  10. Noahthegen

    Noahthegen

    Joined:
    Jan 20, 2021
    Posts:
    13
    can you elaborate on that
     
  11. Rin-Dev

    Rin-Dev

    Joined:
    Jun 24, 2014
    Posts:
    558
  12. Noahthegen

    Noahthegen

    Joined:
    Jan 20, 2021
    Posts:
    13
    can you please fix the code for me pls I don't understand anything you're telling me
     
  13. Rin-Dev

    Rin-Dev

    Joined:
    Jun 24, 2014
    Posts:
    558
    You gotta learn to debug and fix your own issues. Simple ones like this especially. If you can't do that then you're not going to last long here. Others aren't as nice as to sit here and practically hand you the answer just for you to want someone to do it for you still.

    You should really do some more basic coding before attempting to make a game. Learn why you're typing what you're typing. Learn why Vector doesn't exist yet Vector2 and Vector3 do. Learn to debug your issue just to find out you left out a 2 on your Vector.
     
  14. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,016
    What's not being very well articulated here is a few things. The new input system isn't newbie friendly. If you're having issues using it, then it might be better to stick with the old system while you familiarise yourself with Unity and coding in general.

    To give you a chance, Brackeys and his sexy accent did a good tutorial here:


    There's also the quick-start guide, which admittedly, is pretty garbage: https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/QuickStartGuide.html and if the code here is well beyond your understand, I think you should stick to the old system. It's perfectly usable whilst your learning.

    As mentioned rather callously by Rin-Dev, simple errors like compile errors are something you'll need to learn to solve on your own-some. But once you have the basics of c# syntax down you'll never have to ask about them again. I suggest heading to Unity Learn and doing their junior programming course to get you started: https://learn.unity.com/pathway/junior-programmer
     
  15. Noahthegen

    Noahthegen

    Joined:
    Jan 20, 2021
    Posts:
    13
    so I fixed the error but when it turns it reset back to the original rotation this is my code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class Camera : MonoBehaviour
    7. {
    8. public float lookSenesitivity = 5;
    9. private Vector2 lookVector;
    10. private Vector3 rotation;
    11. private Vector2 moveVector;
    12.  
    13. // Start is called before the first frame update
    14. void Start()
    15. {
    16.  
    17. }
    18.  
    19. // Update is called once per frame
    20. void Update()
    21. {
    22. Rotate();
    23. }
    24. public void onLook(InputAction.CallbackContext context)
    25. {
    26. lookVector = context.ReadValue<Vector2>();
    27. }
    28.  
    29. private void Rotate()
    30. {
    31. rotation.y = lookVector.x*lookSenesitivity*Time.deltaTime;
    32. transform.localEulerAngles = rotation;
    33. }
    34. }
     

    Attached Files: