0
votes

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<CharacterController>();

        // 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

1

1 Answers

1
votes

I think you're making this too complicated.

To simply have a camera follow an object while looking at that object, make the camera a child of the object. So the player would have a camera as a child object. The camera would be set up behind him, pointing at him.

This should give you the functionality you want but it doesn't look very good.

My suggestion is to look for the new sample assets (free to all unity3d users and created BY unity technologies) on the asset store. Inside they have a camera prefab which works rather well.

Good luck, I hope that helps.