unity fps 相机

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//旋转轴
public enum RotationAxes
{
    MouseXAndY = 0, MouseX = 1, MouseY = 2
}

public class CabeMove : MonoBehaviour {

    CameraOnMove cameraOnMove;

    Transform transform_Image;

    Vector3 last_Camera_Po;
    Vector3 now_Camera_Po;
    Vector3 difference_Value;

    float x;
    float y;

    // Start is called before the first frame update
    void Start()
    {
        if(cameraOnMove == null)
        {
            cameraOnMove = new CameraOnMove();
        }
        if(transform_Image == null)
        {
            transform_Image = GetComponentsInChildren<Transform>()[2];
        }
        last_Camera_Po = Vector3.zero;
        now_Camera_Po = Vector3.zero;
        difference_Value = Vector3.zero;
        x = 0;
        y = 0;
    }

    // Update is called once per frame
    void Update()
    {
        OnMove();
        CameraMove();
        BulBul();
    }

    private void BulBul()
    {
        if (Input.GetKeyDown(KeyCode.J))
        {
            GameObject bullet = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            bullet.transform.position = transform.position;
            bullet.transform.LookAt(transform_Image);
            bullet.AddComponent<BulletsFly>();
        }
    }

    private void OnMove()
    {
        x = Input.GetAxis("Horizontal");
        y = Input.GetAxis("Vertical");

        transform.Translate(Vector3.forward * y * Time.deltaTime);
        transform.Translate(Vector3.left * -x * Time.deltaTime);
    }

    public void CameraMove()
    {
        if (Input.GetMouseButton(0))
        {
            now_Camera_Po = Input.mousePosition;
            if (!Input.GetMouseButtonDown(0))
            {
                difference_Value = now_Camera_Po - last_Camera_Po;
                if(difference_Value != Vector3.zero)
                cameraOnMove.CameraMove(transform, difference_Value.x/30, difference_Value.y/30);
            }
            last_Camera_Po = now_Camera_Po;
        }
    }
}

public class CameraOnMove
{
    //x轴(水平)速度
    public float sensitivityX = 15F;
    //y轴(垂直)速度
    public float sensitivityY = 15F;
    //x轴(水平)最小旋转值
    public float minimumX = -360F;
    //x轴(水平)最大旋转值
    public float maximumX = 360F;
    //y轴(垂直)最小旋转值
    public float minimumY = -60F;
    //y轴(垂直)最大旋转值
    public float maximumY = 60F;
    //旋转轴
    public RotationAxes axes = RotationAxes.MouseXAndY;
    private float rotationY = 0F;

    public void CameraMove(Transform transform, float x, float y)
    {
        if (axes == RotationAxes.MouseXAndY)
        {
            float rotationX = transform.localEulerAngles.y + x * sensitivityX;

            rotationY += y * sensitivityY;
            rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);

            transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
        }
        else if (axes == RotationAxes.MouseX)
        {
            transform.Rotate(0, x * sensitivityX, 0);
        }
        else if (axes == RotationAxes.MouseY)
        {
            rotationY += y * sensitivityY;
            rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);

            transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
        }
    }
}

public class BulletsFly : MonoBehaviour
{
    private void Update()
    {
        transform.Translate(Vector3.forward * 3 * Time.deltaTime);
    }
}

  

 

转载于:https://www.cnblogs.com/ProjectDeveloping/p/11130461.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值