简体   繁体   中英

Camera: How to stop the player turn up and down on y-axis in unity?

How can I stop the character turning and tilting with the camera in the y-axis? I mean I want to be able to look up and down without changing the player position. It's ok to have it turning around on the x-axis (left and right). Here is the code:

using UnityEngine;
using System.Collections;

public class Actions : MonoBehaviour
{
        public float speedH = 2.0f;
        public float speedV = 2.0f;

        private float yaw = 0.0f;
        private float pitch = 2.0f;

        private void Update()
        {
            yaw += speedH * Input.GetAxis("Mouse X");
            pitch = Input.GetAxis("Mouse Y");

            pitch = Mathf.Clamp(pitch, -30f, 45f);

            transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);

        }
        private void Start()
        {
        }
}

I would create an empty game object and parent the camera to it. Then I would use a script to move the obj to the player's position and then rotate the obj based on the mouse input axis.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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