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

Pivoting instead of rotating

Discussion in 'Scripting' started by left4kill5, Sep 19, 2020.

  1. left4kill5

    left4kill5

    Joined:
    Sep 17, 2020
    Posts:
    12
    Hello! I wanted to ask if there is any way to make my character pivot when I move my FPS camera. My character is rotating & moving instead of staying still and pivoting when I look around (see youtube video).
    Please do not type a whole script for me, but instead, tell me what to tweak. Thanks!

    Code (CSharp):
    1. Mouse camera:
    2.  
    3.  
    4. using System.Collections;
    5. using System.Collections.Generic;
    6. using UnityEngine;
    7.  
    8. public class Mouselook : MonoBehaviour
    9.  
    10. {
    11.  
    12.     public float mouseSensitivity = 100f;
    13.     public Transform Playerbody;
    14.     private float xRotation = 0f;
    15.  
    16.     void Start()
    17.     {
    18.         Cursor.lockState = CursorLockMode.Locked;
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    25.         float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
    26.         xRotation -= mouseY;
    27.         xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    28.         transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    29.         Playerbody.Rotate(Vector3.up * mouseX);
    30.        
    31.        
    32.        
    33.     }
    34. }
    35.  
    36.  
    37. Movement:
    38.  
    39. using System.Collections;
    40. using System.Collections.Generic;
    41. using UnityEngine;
    42.  
    43. public class PlayerMovement : MonoBehaviour
    44. {
    45.     public CharacterController controller;
    46.  
    47.     public float speed = 10f;
    48.     public float gravity = -10f;
    49.     public float jumpHeight = 5f;
    50.  
    51.     public Transform groundCheck;
    52.     public float groundDistance = 0.4f;
    53.     public LayerMask groundMask;
    54.  
    55.     Vector3 velocity;
    56.     bool isGrounded;
    57.    
    58.     void Update()
    59.     {
    60.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    61.  
    62.         if(isGrounded && velocity.y < 0){
    63.             velocity.y = -2f;
    64.         }
    65.  
    66.         float x = Input.GetAxis("Horizontal");
    67.         float z = Input.GetAxis("Vertical");
    68.  
    69.         Vector3 move = transform.right * x + transform.forward * z;
    70.  
    71.         controller.Move(move * speed * Time.deltaTime);
    72.  
    73.         if(Input.GetButton("Jump") && isGrounded){
    74.             velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    75.         }
    76.  
    77.         velocity.y += gravity * Time.deltaTime;
    78.  
    79.         controller.Move(velocity * Time.deltaTime);
    80.  
    81.     }
    82. }
    Youtube video link:
     
  2. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    488
    Is the script attached to the "Player" game object? If it is, it could be finding the median point between all children and rotating that, thus creating the pivot behavior you are seeing.

    If that, indeed, is the case, I would fix it by parenting the Gun under the Graphics game object. Then, instead of rotating the Player GO, rotate Graphics GO and you should see that your player no longer pivots.
     
  3. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    Check your origins of everything. You probably have something offset.
     
  4. left4kill5

    left4kill5

    Joined:
    Sep 17, 2020
    Posts:
    12
    Thanks, I'll double-check it.