码迷,mamicode.com
首页 > 移动开发 > 详细

unity3d之角色的移动篇 -- 俯视视角下的鼠标移动

时间:2014-12-23 21:01:46      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

类似DOTA游戏的移动方法, 其余没什么好说的了。

using UnityEngine;
using System.Collections;

public class gogogogo : MonoBehaviour {

    public float moveSpeed;
    public Vector3 newPosition;
    public CharacterController charactercontroller;
    
    //public AnimationClip idle;
    //public AnimationClip run;
    
    // Use this for initialization
    void Start () {
        newPosition = transform.position;
    }
    
    // Update is called once per frame
    void Update () {
        if (Input.GetMouseButton (0)) {
            locateposition ();
        }
        MoveToPosition ();
    }

    //一个获取点击鼠标点击位置的方法
    void locateposition(){
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        
        if (Physics.Raycast (ray, out hit, 1000)) {
            if(hit.collider.tag != "Player"){
                newPosition = new Vector3(hit.point.x,hit.point.y,hit.point.z);
            }
        }
    }

    //一个开始移动并且旋转的方法
    void MoveToPosition(){    
        if(Vector3.Distance(transform.position,newPosition) > 2){
            //旋转
            Quaternion newRotation = Quaternion.LookRotation (newPosition - transform.position, Vector3.forward);
            newRotation.x = 0f;
            newRotation.z = 0f;
            transform.rotation = Quaternion.Slerp (transform.rotation, newRotation, Time.deltaTime*20);
            //移动
            charactercontroller.SimpleMove (transform.forward * moveSpeed);
            
            //animation.CrossFade(run.name);
        }
        else
        {
            //animation.CrossFade(idle.name);
        }
    }
}

 

unity3d之角色的移动篇 -- 俯视视角下的鼠标移动

标签:

原文地址:http://www.cnblogs.com/hellozzz/p/4181123.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!