Search Unity

First Person Fixed Camera

Discussion in 'Scripting' started by kian192837465, Aug 17, 2019.

  1. kian192837465

    kian192837465

    Joined:
    Aug 13, 2019
    Posts:
    5
    ive been looking on how to do this for around 5 days and have found nothing, we want our player to be fixed with the camera so that when you move the mouse they player rotates with it, like pubg and other games.
    im new to coding so could someone help with my current code to change it to a fixed camera. thanks alot is advance.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Hotdogcontrollernew : MonoBehaviour
    6. {
    7.     public float walkSpeed = 10;
    8.     public float runSpeed = 16;
    9.     public float gravity = -12;
    10.     public float jumpHeight = 1;
    11.     [Range(0,1)]
    12.     public float airControlPercent;
    13.  
    14.     public float turnSmoothTime = 0.2f;
    15.     float turnSmoothVelocity;
    16.  
    17.     public float speedSmoothTime = 0.1f;
    18.     float speedSmoothVelocity;
    19.     float currentSpeed;
    20.     float velocityY;
    21.  
    22.     Animator animator;
    23.     Transform cameraT;
    24.     CharacterController controller;
    25.  
    26.     // Start is called before the first frame update
    27.     void Start()
    28.     {
    29.         animator = GetComponent<Animator>();
    30.         cameraT = Camera.main.transform;
    31.         controller = GetComponent<CharacterController>();
    32.  
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     void Update(){
    37.         // input
    38.         Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
    39.         Vector2 inputDir = input.normalized;
    40.         bool running = Input.GetKey(KeyCode.LeftShift);
    41.  
    42.         Move(inputDir, running);
    43.  
    44.         if (Input.GetKeyDown (KeyCode.Space)){
    45.             Jump ();
    46.         }
    47.         //animator
    48.         float animationSpeedPercent = ((running) ? currentSpeed / runSpeed : currentSpeed / walkSpeed * .5f);
    49.         animator.SetFloat("speedPercent", animationSpeedPercent, speedSmoothTime, Time.deltaTime);
    50.  
    51.     }
    52.     void Move(Vector2 inputDir, bool running){
    53.         if (inputDir != Vector2.zero)
    54.         {
    55.             float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg + cameraT.eulerAngles.y;
    56.             transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, GetModifiedSmoothTime(turnSmoothTime));
    57.         }
    58.  
    59.    
    60.         float targetSpeed = ((running) ? runSpeed : walkSpeed) * inputDir.magnitude;
    61.         currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, GetModifiedSmoothTime(speedSmoothTime));
    62.  
    63.         velocityY += Time.deltaTime * gravity;
    64.         Vector3 velocity = transform.forward * currentSpeed + Vector3.up * velocityY;
    65.  
    66.         controller.Move(velocity * Time.deltaTime);
    67.         currentSpeed = new Vector2(controller.velocity.x, controller.velocity.z).magnitude;
    68.  
    69.         if (controller.isGrounded)
    70.         {
    71.             velocityY = 0;
    72.         }
    73.  
    74.    
    75.     }
    76.  
    77.     void Jump(){
    78.         if (controller.isGrounded) {
    79.             float jumpVelocity = Mathf.Sqrt(-2 * gravity * jumpHeight);
    80.             velocityY = jumpVelocity;
    81.  
    82.         }
    83.  
    84.     }
    85.  
    86.     float GetModifiedSmoothTime(float smoothTime) {
    87.         if (controller.isGrounded){
    88.             return smoothTime;
    89.         }
    90.  
    91.         if (airControlPercent == 0){
    92.             return float.MaxValue;
    93.         }
    94.         return smoothTime / airControlPercent;
    95.     }
    96. }
    97.  


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Hotdogcontrollernew : MonoBehaviour
    6. {
    7.     public float walkSpeed = 10;
    8.     public float runSpeed = 16;
    9.     public float gravity = -12;
    10.     public float jumpHeight = 1;
    11.     [Range(0,1)]
    12.     public float airControlPercent;
    13.  
    14.     public float turnSmoothTime = 0.2f;
    15.     float turnSmoothVelocity;
    16.  
    17.     public float speedSmoothTime = 0.1f;
    18.     float speedSmoothVelocity;
    19.     float currentSpeed;
    20.     float velocityY;
    21.  
    22.     Animator animator;
    23.     Transform cameraT;
    24.     CharacterController controller;
    25.  
    26.     // Start is called before the first frame update
    27.     void Start()
    28.     {
    29.         animator = GetComponent<Animator>();
    30.         cameraT = Camera.main.transform;
    31.         controller = GetComponent<CharacterController>();
    32.  
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     void Update(){
    37.         // input
    38.         Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
    39.         Vector2 inputDir = input.normalized;
    40.         bool running = Input.GetKey(KeyCode.LeftShift);
    41.  
    42.         Move(inputDir, running);
    43.  
    44.         if (Input.GetKeyDown (KeyCode.Space)){
    45.             Jump ();
    46.         }
    47.         //animator
    48.         float animationSpeedPercent = ((running) ? currentSpeed / runSpeed : currentSpeed / walkSpeed * .5f);
    49.         animator.SetFloat("speedPercent", animationSpeedPercent, speedSmoothTime, Time.deltaTime);
    50.  
    51.     }
    52.     void Move(Vector2 inputDir, bool running){
    53.         if (inputDir != Vector2.zero)
    54.         {
    55.             float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg + cameraT.eulerAngles.y;
    56.             transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, GetModifiedSmoothTime(turnSmoothTime));
    57.         }
    58.  
    59.    
    60.         float targetSpeed = ((running) ? runSpeed : walkSpeed) * inputDir.magnitude;
    61.         currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, GetModifiedSmoothTime(speedSmoothTime));
    62.  
    63.         velocityY += Time.deltaTime * gravity;
    64.         Vector3 velocity = transform.forward * currentSpeed + Vector3.up * velocityY;
    65.  
    66.         controller.Move(velocity * Time.deltaTime);
    67.         currentSpeed = new Vector2(controller.velocity.x, controller.velocity.z).magnitude;
    68.  
    69.         if (controller.isGrounded)
    70.         {
    71.             velocityY = 0;
    72.         }
    73.  
    74.    
    75.     }
    76.  
    77.     void Jump(){
    78.         if (controller.isGrounded) {
    79.             float jumpVelocity = Mathf.Sqrt(-2 * gravity * jumpHeight);
    80.             velocityY = jumpVelocity;
    81.  
    82.         }
    83.  
    84.     }
    85.  
    86.     float GetModifiedSmoothTime(float smoothTime) {
    87.         if (controller.isGrounded){
    88.             return smoothTime;
    89.         }
    90.  
    91.         if (airControlPercent == 0){
    92.             return float.MaxValue;
    93.         }
    94.         return smoothTime / airControlPercent;
    95.     }
    96. }
    97.  
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    You seem to have followed the tutorial by Sebastian Lague, since i recognize the code. If you completed the tutorial you should have a ThirdPersonCamera script assigned to your camera used for following the player. To get it into first person perspective, simply change the "distance from target" to 0, and maybe adjust the min/ max pitch values to your liking.

    To get the character to rotate with the camera while standing (it already does so while walking), simply comment out line 53 in your example. You are only rotating the character, if there was an input. Without it, you are always rotating it, which is what you were looking to do.

    Please dont get this the wrong way, but i really dont think you should be using huge chunks of code you do not understand. If the code is a black-box, like for example a function you simply call and use, then that may be fine. But if you plan on modifying the code you should build a basic understanding of which commands result in which behavior, so you know what to modify in ordner to get the desired result.

    Did you follow the tutorial, or simply copy the code? Sebastian Lague, imho, explains what he does pretty well. So if you did not properly follow the tutorial, but want to work on camera movement, then i highly recommend following the tutorial, line by line and trying to understand what's happening.

    Edit: Also, i just saw you opened two identical posts for this, which is a big no-no.
    I never tried if we can delete threads, but it that's possible, delete the other one.
    Edit 2: I just saw that you did not (possibly by accident) open 2 threads yesterday... , but 4.
    FOUR! You are aware that this is against the forum rules?
    Like.. not just on this forum, but basically all over the entirety of the internet?
    You are also aware that it may take more than half a day to get an answer, right?
     
    Last edited: Aug 18, 2019
  3. kian192837465

    kian192837465

    Joined:
    Aug 13, 2019
    Posts:
    5
    thanks, yeah i followed the tutorial line by line to try understand everything the best i could, it was just my lack of knowledge that brought this problem, i apologise for the added forums, i am quite new to this type of thing, also on each on you will see i structured the question differently so that people would understand what i was talking about the best they could, i will delete them now.

    [EDIT] It worked perfectly, you are a living god, thanks alot.
     
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Glad it worked, and yes please delete them if possible. If you think you have to make your question clearer, if nobody answered so far, it's better to edit the original post than to make a new one. If somebody already answered, and for example asked for more information, then replying is better than editing the original post, so others can follow the discussion. But never ever make multiple threads for the same question, that's just ends up delaying everything.
    If you did not get an answer after sevaral days, then you may post a simple "Bump" in your original thread, so it shows up on the first page in the forum again. That said, spamming "Bump"s is nearly as bad as opening new threads, so wait at least a day or so before doing it.
    Most of these rules make sense when you think about them. Simply imagine everybody doing it, then it gets clear why it's a bad idea. Without rules like these the forum would quickly be full of duplicated, empty threads. People wont answer the same question repeatedly, so the search function (for people looking up information for their problems) would also mostly spit out empty threads, making it harder to find the information you were looking for. So in a best case scenario, every question would only have a single thread on the entire internet. Only if there is new information that justifies opening a new thread, then that should be done.

    Hope this makes the situation a bit more clear. Generally people will answer you, sometimes there is just nobody online who knows the answer, so waiting a bit is key. Wish you a nice day :)