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

Please Help a Dad Out! Making Game W/ Son> Camera Script (in Post) Rotates Player Yaxis w/ cam

Discussion in 'Scripting' started by VisionizeTech, Apr 5, 2020.

  1. VisionizeTech

    VisionizeTech

    Joined:
    Mar 4, 2020
    Posts:
    5
    Hello,
    I Know you guys get Tons of Noob BS but please Try to Help me Out. I'm not looking for someone to do the work for me but i have a tutorial i'm following on making a 3d platformer. Very Beginner C# scripting knowledge.

    I think i added the script if not just tell me how and i seriously appreciate it you have no idea. My 4 year old son wanted to make a game

    i used to use game maker 2d back in the day but things are different.

    Making a game with my son and i'm stuck on this rotation problem weeks. i'm new to forum so forgive me if i can't upload script right.:(

    I Stopped the camera going through the floor and camera uses mouse x and y for input.


    I've been researching quaternions and eulerangles and rotation axis' and i'm STUCK.o_O:eek:

    I'm gonna try to add cameracontroller script and player respectively.

    Any help is MUCH appreciated!:D

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraController : MonoBehaviour
    6. {
    7.  
    8.     public Transform target;
    9.  
    10.     public Vector3 offset;
    11.  
    12.     public bool useOffsetValues;
    13.  
    14.     public float rotateSpeed;
    15.  
    16.     public Transform pivot;
    17.  
    18.     public float maxViewAngle;
    19.     public float minViewAngle;
    20.  
    21.     public bool invertY;
    22.  
    23.     // Start is called before the first frame update
    24.     void Start()
    25.     {
    26.         if(!useOffsetValues)
    27.         {
    28.             offset = target.position - transform.position;
    29.         }
    30.  
    31.         pivot.transform.position = target.transform.position;
    32.         pivot.transform.parent = target.transform;
    33.  
    34.         Cursor.lockState = CursorLockMode.Locked;
    35.     }
    36.  
    37.     // Update is called once per frame
    38.     void LateUpdate()
    39.     {
    40.  
    41.         //Get the X Position of Mouse and Rotate
    42.         float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
    43.         target.Rotate(0, horizontal, 0);
    44.  
    45.         //Get Y Position of Mouse & Rotate the Pivot
    46.         float vertical = Input.GetAxis("Mouse Y") * rotateSpeed;
    47.         pivot.Rotate(-vertical, 0, 0);
    48.  
    49.         //Invert Y Option (Use Instead of Line 49)
    50.         //if(invertY)
    51.         //{
    52.         //    pivot.Rotate(vertical, 0, 0);
    53.         //} else
    54.         //{
    55.         //    pivot.Rotate(-vertical, 0, 0);
    56.         //}
    57.  
    58.         //Move Camera Based on Current Rotation of Target & Orig Offset
    59.         float desiredYAngle = target.eulerAngles.y;
    60.         float desiredXAngle = pivot.eulerAngles.x;
    61.         Quaternion rotation = Quaternion.Euler(desiredXAngle, desiredYAngle, 0);
    62.         transform.position = target.position - (rotation * offset);
    63.         //transform.position = target.position - offset;
    64.  
    65.         //Limit the Up/Down Camera
    66.         if (pivot.rotation.eulerAngles.x > maxViewAngle && pivot.rotation.eulerAngles.x < 180f)
    67.         {
    68.             pivot.rotation = Quaternion.Euler(maxViewAngle, 0, 0);
    69.         }
    70.  
    71.         if (pivot.rotation.eulerAngles.x > 180 && pivot.rotation.eulerAngles.x < 360f + minViewAngle)
    72.         {
    73.             pivot.rotation = Quaternion.Euler(360f + minViewAngle, 0, 0);
    74.         }
    75.  
    76.         if (transform.position.y < target.position.y)
    77.         {
    78.             transform.position = new Vector3(transform.position.x, pivot.position.y -.25f, transform.position.z);
    79.         }
    80.         transform.LookAt(target.transform);
    81.     }
    82. }
    83.  
    and player controller

    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 float jumpForce;
    9.     public CharacterController controller;
    10.    
    11.     private Vector3 moveDirection;
    12.     public float gravityScale;
    13.  
    14.     public Animator anim;
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.  
    20.         controller = GetComponent<CharacterController>();
    21.  
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.  
    27.     {
    28.         //moveDirection = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, moveDirection.y, Input.GetAxis("Vertical") * moveSpeed);
    29.  
    30.         //float yStore = moveDirection.y;
    31.  
    32.         moveDirection = (transform.forward * Input.GetAxis("Vertical")) + (transform.right * Input.GetAxis("Horizontal"));
    33.         moveDirection = moveDirection.normalized * moveSpeed;
    34.         //moveDirection.y = yStore;
    35.  
    36.         //Attempt to Add Gravity
    37.         //moveDirection.y = -4.11f;
    38.  
    39.         if (controller.isGrounded)
    40.         {
    41.             moveDirection.y = -.14f;
    42.             if (Input.GetButtonDown("Jump"))
    43.             {
    44.                 moveDirection.y = jumpForce;
    45.             }  
    46.         }
    47.  
    48.         moveDirection.y = (moveDirection.y + (Physics.gravity.y * gravityScale * Time.deltaTime));
    49.         controller.Move(moveDirection * Time.deltaTime);
    50.  
    51.         anim.SetBool("isGrounded", controller.isGrounded);
    52.         anim.SetFloat("Speed", (Mathf.Abs(Input.GetAxis("Vertical")) + Mathf.Abs(Input.GetAxis("Horizontal"))));
    53.  
    54.     }
    55. }
    56.  
    ok i think i got it in.:confused: (Pre Post Update) -> the Script looks like theyre both in like i see it in the forums!

    The Comments are Because i was trying to debug in a noob way i guess to see what caused player to move with the empty pivot i created as a child of camera in hierarchy and assigned it to player in script.

    All i Want is to keep my move direction that took weeks to get to. (moving towards camera view basically is considered up). and my camera is supposed to use a game object empty "pivot" to rotate the view instead of player.
    the x axis doesn't rotate player and y axis (mouse moving side to side) rotates smooth and nice but so does the player i added. FYI the player is i think a child of a capsule with toggled mesh renderer for collider. its under the arrow of capsule ( player) and the actual player with animations is called human guy lol.

    I know it's complicated at least to me so please remember i'm a noob but also i'm able to understand very well things like this.:confused::eek:o_O
     
  2. VisionizeTech

    VisionizeTech

    Joined:
    Mar 4, 2020
    Posts:
    5
    I Think line 62 of Cam controller looks suspect because it involves quaternions and euler.
    but i am like a C# How to Book with 5 Pages right Now