【问题标题】:Make a camera follow the player as the player turns around rotate当玩家转身时让相机跟随玩家旋转
【发布时间】:2014-11-29 01:54:20
【问题描述】:

我已经可以通过在统一编辑器上定义的轴的输入使播放器绕 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");
        }
    }
}

上面的代码我附在播放器上,只是引用了相机。

谢谢你之前

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    我认为你把事情弄得太复杂了。

    要让相机在观察对象时跟随该对象,请将相机设为该对象的子对象。所以玩家会有一个相机作为子对象。摄像机会放在他身后,指向他。

    这应该会给你你想要的功能,但它看起来不是很好。

    我的建议是在资产商店中寻找新的示例资产(对所有 unity3d 用户免费并由统一技术创建)。里面有一个相机预制件,效果很好。

    祝你好运,希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多