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
  3. Dismiss Notice

Question Problem with Unity File (Testing out Unity / Not an experienced coder)

Discussion in 'Editor & General Support' started by Tazerboy_10, May 4, 2021.

  1. Tazerboy_10

    Tazerboy_10

    Joined:
    Apr 1, 2021
    Posts:
    2
    YouTube Unity Tutorial I have been following:
    https://rb.gy/wbeb0e

    I also got inspiration from this person's character in their DevLog:
    https://rb.gy/4nuzww

    (They made a character within Unity and animated it as well...)

    I have gone back and watched the video and I don't see anything wrong...
    (I can't tell because I don't know coding or Unity that well...)


    Issues I'm having with my current Unity Project (SEE VIDEO):
    1.) The character can move around freely, but things aren't working properly (SEE CODE BELOW)
    > My character is made with in Unity (I don't know how to use Blender...)
    > My character uses "Character Controller"
    >The character moves in the direction of the opposite the desired key
    - A = Goes Right
    - W = Goes Backward
    - S = Goes Backwards
    - D = Goes Left
    (The character starts acting jittery when pressing the S key)

    2.) This might be influenced by the "Camera Controller"
    > The camera also has some "resistance" to it...
    (I don't how else to describe it...)


    "Player Controller Script"
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     public float moveSpeed;
    8.     //public Rigidbody RB;
    9.     public float jumpForce;
    10.     public CharacterController controller;
    11.     public bool isGrounded;
    12.  
    13.     private Vector3 moveDirection;
    14.     public float gravityScale;
    15.  
    16.     public Animator anim;
    17.     public Transform pivot;
    18.     public float rotateSpeed;
    19.  
    20.     public GameObject playerModel;
    21.  
    22.     // Start is called before the first frame update
    23.     void Start()
    24.     {
    25.         //RB = GetComponent<Rigidbody>();
    26.         controller = GetComponent<CharacterController>();
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update()
    31.     {
    32.         /* RB.velocity = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, RB.velocity.y, Input.GetAxis("Vertical") * moveSpeed);
    33.  
    34.         if (Input.GetButtonDown("Jump"))
    35.         {
    36.             RB.velocity = new Vector3(RB.velocity.x, jumpForce, RB.velocity.z);
    37.         } */
    38.  
    39.         moveDirection = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, moveDirection.y, Input.GetAxis("Vertical") * moveSpeed);
    40.  
    41.         float yStore = moveDirection.y;
    42.         moveDirection = (transform.forward * Input.GetAxis("Vertical")) + (transform.right * Input.GetAxis("Horizontal"));
    43.         moveDirection = moveDirection.normalized * moveSpeed;
    44.         moveDirection.y = yStore;
    45.  
    46.         if (controller.isGrounded)
    47.         {
    48.             moveDirection.y = 0f;
    49.             if (Input.GetButtonDown("Jump"))
    50.             {
    51.                 moveDirection.y = jumpForce;
    52.             }
    53.         }
    54.  
    55.         moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale);
    56.         controller.Move(moveDirection * Time.deltaTime);
    57.  
    58.         //Move the player in different directions based where on the camera is looking
    59.         if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
    60.         {
    61.             transform.rotation = Quaternion.Euler(0f, pivot.rotation.eulerAngles.y, 0f);
    62.             Quaternion newRotation = Quaternion.LookRotation(new Vector3(moveDirection.x, 0f, moveDirection.z));
    63.             playerModel.transform.rotation = Quaternion.Slerp(playerModel.transform.rotation, newRotation, rotateSpeed * Time.deltaTime);
    64.         }
    65.  
    66.         anim.SetBool("isGrounded", controller.isGrounded);
    67.         anim.SetFloat("Speed", (Mathf.Abs(Input.GetAxis("Vertical")) + Mathf.Abs(Input.GetAxis("Horizontal"))));
    68.     }
    69. }
    "Camera Controller Script"
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraController : MonoBehaviour
    6. {
    7.     public Transform target;
    8.     public Vector3 offset;
    9.     public bool useOffsetValues;
    10.     public float rotateSpeed;
    11.     public Transform pivot;
    12.     public float maxViewAngle;
    13.     public float minViewAngle;
    14.     public bool invertY;
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         if (!useOffsetValues)
    20.         {
    21.             offset = target.position - transform.position;
    22.         }
    23.  
    24.         pivot.transform.position = target.transform.position;
    25.         //pivot.transform.parent = target.transform;
    26.         pivot.transform.parent = null;
    27.  
    28.         Cursor.lockState = CursorLockMode.Locked;
    29.     }
    30.  
    31.     // Update is called once per frame
    32.     void LateUpdate()
    33.     {
    34.         pivot.transform.position = target.position - offset;
    35.  
    36.         //Get the X position of the mouse and rotate the target
    37.         float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
    38.         pivot.Rotate(0, horizontal, 0);
    39.  
    40.         //Get the Y position of the mouse and rotate the pivot
    41.         float vertical = Input.GetAxis("Mouse Y") * rotateSpeed;
    42.         pivot.Rotate(vertical, 0, 0);
    43.  
    44.         if(invertY)
    45.         {
    46.             pivot.Rotate(vertical, 0, 0);
    47.         }else
    48.         {
    49.             pivot.Rotate(-vertical, 0, 0);
    50.         }
    51.  
    52.         //Limit the up/down camera rotation
    53.         if(pivot.rotation.eulerAngles.x > maxViewAngle && pivot.rotation.eulerAngles.x < 180f)
    54.         {
    55.             pivot.rotation = Quaternion.Euler(maxViewAngle, 0, 0);
    56.         }
    57.  
    58.         if(pivot.rotation.eulerAngles.x > 180f && pivot.rotation.eulerAngles.x < 360f + minViewAngle)
    59.         {
    60.             pivot.rotation = Quaternion.Euler(360f + minViewAngle, 0, 0);
    61.         }
    62.  
    63.         //Move the camera based on the current rotation of the target and the original offset (X Axis)
    64.         float desiredYAngle = pivot.eulerAngles.y;
    65.  
    66.         //Move the camera based on the current rotation of the target and the orignal offset (Y Axis)
    67.         float desiredXAngle = pivot.eulerAngles.x;
    68.  
    69.         Quaternion rotation = Quaternion.Euler(desiredXAngle, desiredYAngle, 0);
    70.         transform.position = target.position - (rotation * offset);
    71.  
    72.         //transform.position = target.position - offset;
    73.  
    74.         if (transform.position.y < target.position.y)
    75.         {
    76.             transform.position = new Vector3(transform.position.x, target.position.y -.5f, transform.position.z);
    77.         }
    78.  
    79.         transform.LookAt(target);
     
    Last edited: May 4, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    Player controller scripts are such well-travelled territory, it probably doesn't make sense to start writing your own until you've worked with at least a half dozen other ones.

    Here's one example based on the Unity CharacterController documentation code:

    https://forum.unity.com/threads/how...racter-movement-in-unity.981939/#post-6379746

    In my proximity_buttons package I have a demo of how controls need to be rotated based on camera heading. Look at around line 128, and be sure to download the whole project to see it properly set up and running.

    https://github.com/kurtdekker/proxi...edControls/RotatedControlsPlayerController.cs

    Otherwise, if you want to track stuff down like this it is a lot of fiddling and experimenting and trying to find out what is actually happening. Timing is critical, as is logic flow, obviously.

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.

    If you are running a mobile device you can also see the console output. Google for how.
     
  3. Tazerboy_10

    Tazerboy_10

    Joined:
    Apr 1, 2021
    Posts:
    2
    In the title of the post I said, "Not an experienced coder..."
    • The advice sounds like it's more aimed at someone who knows how to code and unity...
    • "Player controller scripts are well-travelled..." (Well, not in my case...)

    I linked a video tutorial that had I been following and not just experimenting with the coding...
    • I have absolutely no idea what I'm doing without the watching the video...
    (It seems like your going off of your previous knowledge and did not watch the video...)
     
    Last edited: May 4, 2021
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    Looking at that block of code, you cannot reliably read and write to eulerAngles due to a limitation described commonly as "gimbal lock." You can look it up if you like.

    Instead, for heading and elevation, you will find that the vast majority of camera scripts either track their own floating point variables that they can move to any scalar value, then use those values to create fresh rotations for the camera, often using Quaternion.Euler() as you do above, but from fresh local variables never read out of eulerAngles, which gimbal-locks at 180 degrees.

    In general, camera work is about a 8 or 9 out of 10 on the difficulty scale, which is why Unity made the Cinemachine package (see Package Manager). You may want to consider integrating Cinemachine to watch your guy walking around.

    As for the movement, as I said above there are LOTS of ways that can go wrong. I suggested adding some Debug.Log() lines to print out variables on the fly and figure out what is going on.

    Remember that the ONLY purpose of all programs is to transform data from one form to another, nothing else. In this case you are transforming finger input to player motion, and reporting that the data is reversed from your expectation. You need to find out what that data is (with Debug.Log()) so you can reason about why it is incorrect.