unity3d模仿魔兽世界鼠标对游戏操作

  1、新建unity3d项目,在项目中导入CharacterController包。在游戏中创建Plane作为地面,把Plane的Tag设为Ground。创建Directional light照亮游戏世界。把第三人称控制器放到Plane上面,之后把挂载第三人称的脚本Remove掉,把它的Tag设为Player。

  2、创建LookTargetPos脚本,把它挂载到第三人称控制器上。它的作用是当鼠标左键按下且按下的位置为Plane时,第三人称控制器朝向鼠标按下方向。

using UnityEngine;
using System.Collections;

public class LookTargetPos : MonoBehaviour
{


    public static Vector3 targetPos;//用来保存鼠标点击到地面的位置
    private bool isMouseDown = false;//判断鼠标左键是否一直按下
    void Start()
    {
        targetPos = this.transform.position;
    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            isMouseDown = true;
            LookAtPos();
        }
        else if (Input.GetMouseButtonUp(0))
        {
            isMouseDown = false;
        }
        //如果鼠标左键一直按下,则一直更新鼠标位置
        if (isMouseDown == true)
        {
            LookAtPos();
        }
    }

    void LookAtPos()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hitinfo;
        bool isCollider = Physics.Raycast(ray, out hitinfo);
        //判断鼠标是否点击地面
        if (isCollider == true && hitinfo.collider.tag == "Ground")
        {
            targetPos = hitinfo.point;
            targetPos.y = this.transform.position.y;
            this.transform.LookAt(targetPos);
        }
    }
}

  3、创建PlayerMove脚本,把它挂载到第三人称控制器上。它的作用是控制第三人称控制器移动到鼠标左键按下的位置。

using UnityEngine;
using System.Collections;

public enum Playerstate
{
    Moveing,
    Idle
}
public class PlayerMove : MonoBehaviour
{

    private CharacterController controller;
    public int speed = 4;
    private float distance;//用来保存游戏主角和目标点的距离
    public Playerstate state;//保存游戏主角的状态
    // Use this for initialization
    void Start()
    {
        controller = this.GetComponent<CharacterController>();
        state = Playerstate.Idle;
    }

    // Update is called once per frame
    void Update()
    {
        Move();
    }
    void Move()
    {
        distance = Vector3.Distance(this.transform.position, LookTargetPos.targetPos);
        if(distance>0.05f)
        {
            controller.SimpleMove(this.transform.forward * speed);
            state = Playerstate.Moveing;
            print(distance);
        }
        else
        {
            state = Playerstate.Idle;
        }
    }
}

  4、创建PlayerState脚本,把它挂载到第三人称控制器上。它的作用是控制第三人称控制器动画播放。

using UnityEngine;
using System.Collections;

public class PlayerState : MonoBehaviour
{

    private PlayerMove playerState;
    private float distance;
    // Use this for initialization
    void Start()
    {
        playerState = this.GetComponent<PlayerMove>();
    }

    // Update is called once per frame
    void LateUpdate()
    {
        //如果游戏主角在移动就播放跑的动画
        if (playerState.state == Playerstate.Moveing)
        {
            PlayAnimation("run");
        }
        //如果游戏主角在等待就播放站的动画
        else if (playerState.state == Playerstate.Idle)
        {
            PlayAnimation("idle");
        }
    }
    void PlayAnimation(string animationName)
    {
        animation.CrossFade(animationName);
    }
}

  5、创建CameraFollow脚本,把它挂载到Main Camera上。它的作用是:1、让Main Camera跟随第三人称控制器移动;2、改变Main Camera视角;3、控制Main Camera与第三人称控制器的距离。

using UnityEngine;
using System.Collections;

public class CameraFollow : MonoBehaviour
{
    private Vector3 followPos;//用来保存摄像机和游戏主角的相对位置
    private GameObject player;
    public float scrollSpeed = 10;//摄像机拉近的速度
    private bool isRotating = false;//判断鼠标右键是否按下
    public float rotateSpeed = 2;//摄像机左右旋转的速度
    private float distance;//保存摄像机和游戏主角距离
    // Use this for initialization
    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player");
        followPos = player.transform.position - this.transform.position;
        this.transform.LookAt(player.transform.position);
    }

    // Update is called once per frame
    void Update()
    {
        Follow();
        RotateView();
        ScrollView();
    }
    //使摄像机跟随游戏主角运动
    void Follow()
    {
        this.transform.position = player.transform.position - followPos;
    }
    //滑动鼠标滑轮的时候可以改变摄像机与游戏主角的距离
    void ScrollView()
    {
        distance = followPos.magnitude;
        distance += Input.GetAxis("Mouse ScrollWheel");
        distance=Mathf.Clamp(distance,2,18);
        followPos = followPos.normalized * distance;
    }
    //按下鼠标右键的时候移动鼠标可以改变摄像机的视角
    void RotateView()
    {
        if(Input.GetMouseButtonDown(1))
        {
            isRotating = true;
        }
        else if(Input.GetMouseButtonUp(1))
        {
            isRotating = false;
        }
        if(isRotating)
        {
            this.transform.RotateAround(player.transform.position, player.transform.up, rotateSpeed * Input.GetAxis("Mouse X"));
            Vector3 originalPos = this.transform.position;
            Quaternion originalRotation = this.transform.rotation;
            this.transform.RotateAround(player.transform.position, this.transform.right, -rotateSpeed * Input.GetAxis("Mouse Y"));
            float x = this.transform.eulerAngles.x;
            print(x);
            //限制摄像机旋转的最大,最小位置
            if(x<10||x>70)
            {
                this.transform.position = originalPos;
                this.transform.rotation = originalRotation;
            }
        }
        followPos = player.transform.position - this.transform.position;
    }
}

  6、完成以后的效果,录屏鼠标看不见了,抱歉了。

 

posted @ 2015-09-19 09:58  jj391  阅读(2571)  评论(0编辑  收藏  举报