鼠标在屏幕上的移动来控制相机水平,垂直旋转

using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
    public float distance_v;
    public float distance_h;
    public float rotation_H_speed=1;
    public float rotation_V_speed=1;
    public float max_up_angle = 80;              //越大,头抬得越高
    public float max_down_angle =-60;            //越小,头抬得越低
    public Transform follow_obj;        //player


    private float current_rotation_H;      //水平旋转结果
    private float current_rotation_V;  //垂直旋转结果
    void LateUpdate()      
    {
        //控制旋转
        current_rotation_H+= Input.GetAxis("Mouse X")*rotation_H_speed;
        current_rotation_V += Input.GetAxis("Mouse Y")*rotation_V_speed;
        current_rotation_V=Mathf.Clamp(current_rotation_V, max_down_angle, max_up_angle);       //限制垂直旋转角度
        transform.localEulerAngles = new Vector3(-current_rotation_V,current_rotation_H,0f);
        
        //改变位置,以跟踪的目标为视野中心,且视野中心总是面向follow_obj
        transform.position =follow_obj.position;
        transform.Translate(Vector3.back * distance_h, Space.Self);
        transform.Translate(Vector3.up * distance_v, Space.World);          //相对于世界坐标y轴向上
    }
}

 

转载于:https://www.cnblogs.com/wanggonglei/p/7856023.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值