简体   繁体   English

如何使角色根据相机旋转角度旋转?

[英]How can make the character to rotate according to the camera rotation angle?

This is the script i'm using attached to the Main Camera. 这是我正在使用的附在主摄像机上的脚本。 I can rotate the camera 360 degrees. 我可以将相机旋转360度。 But the character(ThirdPersonController) is keep standing idle facing same position all the time. 但是角色(ThirdPersonController)一直保持闲置面对相同的位置。

I want to make that when i rotate the camera that also the character will rotate. 我想使我旋转相机时角色也会旋转。

And a problem i have is i can rotate the came to the ground level a bit under it. 我遇到的一个问题是我可以将其旋转到地面以下一点。 How can i limit the camera rotation to the ground/terrain level ? 如何将相机旋转限制在地面/地面水平? This is a screenshot what i mean the camera is under the terrain/character: 这是一个截图,我的意思是相机位于地形/角色下方:

屏幕截图

And this is the script code: I tried to add this line to rotate the character with the camera but it's not doing it, it does nothing. 这是脚本代码:我试图添加此行以使用相机旋转角色,但它没有这样做,什么也没做。

transform.rotation = Quaternion.RotateTowards(transform.rotation, rotation, rotationSpeed);

The script: 剧本:

using UnityEngine;
using System.Collections;

[AddComponentMenu("Camera-Control/Mouse Orbit with zoom")]
public class MouseOrbitImproved : MonoBehaviour
{

    public Transform target;
    public float distance = 5.0f;
    public float xSpeed = 120.0f;
    public float ySpeed = 120.0f;

    public float yMinLimit = -20f;
    public float yMaxLimit = 80f;

    public float distanceMin = .5f;
    public float distanceMax = 15f;

    private Rigidbody rigidbody;

    float x = 0.0f;
    float y = 0.0f;

    public float rotationSpeed = 5f;

    // Use this for initialization
    void Start()
    {
        Vector3 angles = transform.eulerAngles;
        x = angles.y;
        y = angles.x;

        rigidbody = GetComponent<Rigidbody>();

        // Make the rigid body not change rotation
        if (rigidbody != null)
        {
            rigidbody.freezeRotation = true;
        }
    }

    void LateUpdate()
    {
        if (target)
        {
            x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
            y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;

            y = ClampAngle(y, yMinLimit, yMaxLimit);

            Quaternion rotation = Quaternion.Euler(y, x, 0);

            distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);

            RaycastHit hit;
            if (Physics.Linecast(target.position, transform.position, out hit))
            {
                distance -= hit.distance;
            }
            Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
            Vector3 position = rotation * negDistance + target.position;

            transform.rotation = rotation;
            transform.position = position;

            transform.rotation = Quaternion.RotateTowards(transform.rotation, rotation, rotationSpeed);
        }
    }

    public static float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360F)
            angle += 360F;
        if (angle > 360F)
            angle -= 360F;
        return Mathf.Clamp(angle, min, max);
    }
}

I created a new small clean script that should rotate the character according to the camera rotation. 我创建了一个新的小型清理脚本,该脚本应根据相机的旋转来旋转角色。 The above script is attached to the Main Camera. 上面的脚本附在主摄像机上。

Now i have the ThirdPersonController and i have a script already attached to it: 现在,我拥有ThirdPersonController,并且已经附加了一个脚本:

using UnityEngine;
using System.Collections;

public class ClickToMove : MonoBehaviour
{
    public int speed = 5; // Determines how quickly object moves towards position
    public float rotationSpeed = 5f;

    private Vector3 targetPosition;
    private Animator _animator;
    private Vector3 destination;
    private Quaternion targetRotation;

    void Start()
    {
        _animator = GetComponent<Animator>();
        _animator.CrossFade("Idle", 0);
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            _animator.CrossFade("Walk", 0);
            Plane playerPlane = new Plane(Vector3.up, transform.position);
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            float hitdist = 0.0f;

            if (playerPlane.Raycast(ray, out hitdist))
            {
                Vector3 targetPoint = ray.GetPoint(hitdist);
                targetPosition = ray.GetPoint(hitdist);
                targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
                destination = targetPosition;
            }
        }

        transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationSpeed);

        if ((transform.position - destination).magnitude < 0.7f)
        {
            _animator.CrossFade("Idle", 0);
        }
    }
}

This script is working fine. 这个脚本工作正常。 Also the script above that rotate the camera. 上面的脚本也会旋转相机。 But once i attach to the ThirdPersonController the new small script that should rotate the character so first it's not rotating the character and second it's making the character to walk on forward the whole ClickToMove script is not working good. 但是,一旦我将新的小脚本附加到ThirdPersonController上,它应该旋转角色,因此首先它不会旋转角色,其次它会使角色向前行走,整个ClickToMove脚本无法正常工作。

using UnityEngine;
using System.Collections;

public class RotateCharacterToCamera : MonoBehaviour {

    void Update () {

        Vector3 characterPosition = transform.position;
        Vector3 cameraPosition = Camera.main.transform.position;
        Vector3 delta = new Vector3(characterPosition.x - characterPosition.x, 0.0f, characterPosition.z - characterPosition.z);
        transform.LookAt(delta);

    }
}

I guess simply adjusting the yMinLimit value to 0 or less should be enough to limit your camera vertical angle. 我想只要将yMinLimit值调整为0或更小就足以限制相机的垂直角度。

About your character rotation, you should have a small script on him that calls something like : 关于角色旋转,您应该在他的身上写一个小脚本,该脚本应类似于:

Vector3 characterPosition = transform.position;
Vector3 cameraPosition = Camera.main.transform.position;
Vector3 delta = new Vector3(characterPosition.x - characterPosition.x, 0.0f, characterPosition.z - characterPosition.z);
transform.LookAt(delta);

I set the y value of delta vector to 0 so only the "horizontal" orientation of the camera will be considered. 我将增量矢量的y值设置为0,因此仅考虑摄影机的“水平”方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM