Search Unity

Make a camera follow the player as the player turns around rotate

Discussion in 'Scripting' started by Risna, Nov 29, 2014.

  1. Risna

    Risna

    Joined:
    Nov 28, 2014
    Posts:
    10
    I already can make the player rotate around y- axis by the input of the axis that has been defined on the unity editor. But I want camera also rotating while the player rotates. I already made like below, but the camera not rotating, just the player. Anyone could help me?

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [RequireComponent(typeof(CharacterController))] // This script requires CharacterController attached to the game object where this script attached into
    6.  
    7. public class CheckPlayer : MonoBehaviour
    8. {
    9.   private Vector3 moveDirection = Vector3.zero; // Define and set for the movement of the player is not moving by default
    10.  
    11.   private float gravity = 20.0f, speed = 5.0f; // Define and set for the gravity, speed of the player
    12.  
    13.   private void Update()
    14.   {
    15.   // Rotate the Player
    16.   transform.Rotate(0, Input.GetAxis("Rotate") * 60.0f * Time.deltaTime, 0);
    17.  
    18.   Camera.main.transform.eulerAngles = new Vector3(0, Input.GetAxis("Rotate") * 60.0f * Time.deltaTime, 0);
    19.  
    20.   // Get the CharacterController component
    21.   CharacterController controller = GetComponent<CharacterController>();
    22.  
    23.   // If the character is on the ground
    24.   if (controller.isGrounded)
    25.   {
    26.   // Get the axis direction for the movement of the character from the Input in the editor
    27.   moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    28.  
    29.   // Player movement depends on the move direction
    30.   moveDirection = transform.TransformDirection(moveDirection);
    31.  
    32.   // The player movement is depends on the player speed
    33.   moveDirection *= speed;
    34.   }
    35.  
    36.   // How much for the time for player takes to hit the ground because of gravity
    37.   moveDirection.y -= gravity * Time.deltaTime;
    38.  
    39.   // Move the character each second while pressing the key input defined in the editor
    40.   controller.Move(moveDirection * Time.deltaTime);
    41.   }
    42.  
    43.   private void OnTriggerEnter(Collider col)
    44.   {
    45.   // If the game object colided with the certain tag
    46.   if (col.gameObject.tag == "Inn's Door")
    47.   {
    48.   // Load another level
    49.   GameManager.LoadLevel("Third Loading Scene");
    50.   }
    51.  
    52.   // If the game object colided with the certain tag
    53.   else if (col.gameObject.tag == "Field's Door")
    54.   {
    55.   // Load another level
    56.   GameManager.LoadLevel("Fourth Loading Scene");
    57.   }
    58.   }
    59. }
    60.  
    61.  
    The code above I attached to the player and just refer the camera.

    Thank you before