我已经可以通过在Unity编辑器上定义的轴的输入使播放器围绕y轴旋转。但我希望相机在播放器旋转时也能旋转。我已经在下面做了,但相机没有旋转,只是玩家。有人可以帮帮我吗?
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");
}
}
}
上面的代码我附在播放器上,只是引用相机。
谢谢你
答案 0 :(得分:1)
我认为你这太复杂了。
要让相机在观察该物体时跟踪物体,请将相机作为物体的子物。因此玩家将相机作为子对象。摄像机将安装在他身后,指着他。
这应该可以为您提供所需的功能,但它看起来并不是很好。
我的建议是在资产商店中寻找新的样本资产(对所有unity3d用户免费并由BY统一技术创建)。在里面他们有一个相机预制件,工作得很好。
祝你好运,我希望有所帮助。