Quantcast
Channel: Latest Questions by Risna
Viewing all articles
Browse latest Browse all 18

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

$
0
0
Hi guys, 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? using UnityEngine; using System.Collections; [RequireComponent(typeof(CharacterController))] // This script requires CharacterController attached to the game object where this script attached into public class CheckPlayer : MonoBehaviour { private Vector3 moveDirection = Vector3.zero; // Define and set for the movement of the player is not moving by default private float gravity = 20.0f, speed = 5.0f; // Define and set for the gravity, speed of the player private void Update() { // Rotate the Player transform.Rotate(0, Input.GetAxis("Rotate") * 60.0f * Time.deltaTime, 0); Camera.main.transform.eulerAngles = new Vector3(0, Input.GetAxis("Rotate") * 60.0f * Time.deltaTime, 0); // Get the CharacterController component CharacterController controller = GetComponent(); // If the character is on the ground if (controller.isGrounded) { // Get the axis direction for the movement of the character from the Input in the editor moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); // Player movement depends on the move direction moveDirection = transform.TransformDirection(moveDirection); // The player movement is depends on the player speed moveDirection *= speed; } // How much for the time for player takes to hit the ground because of gravity moveDirection.y -= gravity * Time.deltaTime; // Move the character each second while pressing the key input defined in the editor controller.Move(moveDirection * Time.deltaTime); } private void OnTriggerEnter(Collider col) { // If the game object colided with the certain tag if (col.gameObject.tag == "Inn's Door") { // Load another level GameManager.LoadLevel("Third Loading Scene"); } // If the game object colided with the certain tag else if (col.gameObject.tag == "Field's Door") { // Load another level GameManager.LoadLevel("Fourth Loading Scene"); } } } The code above I attached to the player and just refer the camera. Thank you before

Viewing all articles
Browse latest Browse all 18

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>