using UnityEngine;

using System.Collections.Generic;
using UnityEngine.EventSystems;
using UnityEngine.UI; public class ManualRoam
{
private static ManualRoam mouse_this;
public static ManualRoam Instance()
{
if (mouse_this == null)
{
mouse_this = new ManualRoam();
}
return mouse_this;
} private Vector3 targetVector3;
public float movespeed = 1.5f;
private bool IsOver = true;
public GameObject player;
public Camera firstCamera;
float sensitivityX = 1f;
//控制视野缩放的速率
private float sensitivetyMouseWheel = 10f;
private float maximum = ;
private float minmum = ;
//定义控制人物
private float rotateSpeed = 45f;
private float playerMoveSpeed = 2.5f;
//鼠标双击的参数
private float delay = 0.5f;
private float _firstClickTime = ;
private bool _oneClick = false; //点击了第一下
public bool IsMove;
//UI防止穿透定义
private GraphicRaycaster graphicRaycaster;
private EventSystem eventSystem; float YDistance = 1.823f;
public ScreenView_1Main screenView_1Main;
public void Awake(GameObject _player, GameObject cam,ScreenView_1Main _this)
{
//YDistance = y;
//Debug.LogError("YDistance = " + YDistance);
//第一人称赋值
player = _player;
firstCamera = cam.GetComponent<Camera>();
targetVector3 = _player.transform.position;
screenView_1Main = _this;
//发现组件
graphicRaycaster = GameObject.Find("CanvasUI").GetComponent<GraphicRaycaster>();
eventSystem = GameObject.Find("EventSystem").GetComponent<EventSystem>();
//player = GameObject.Find("Player");
//firstCamera = player.transform.Find("playerCamera").GetComponent<Camera>();
allmouseIcon=new List<GameObject>();
} private RaycastHit hitInfo;
public void Update()
{
if (CheckGuiRaycastObjects())
return;
screenView_1Main.my_mouseController.SetRoamPoint (player.transform.position); //当player的距离和箭头的距离小于0.5的时候,销毁掉箭头
foreach (var kk in allmouseIcon)
{
if (kk != null)
{
if (Vector3.Distance(kk.transform.position, new Vector3 (player.transform.position.x, 0.1f, player.transform.position.z)) < 0.5f)
{
GameObject.Destroy (kk);
}
}
}
if (Input.GetMouseButtonDown() && !EventSystem.current.IsPointerOverGameObject())
{ if (_oneClick && Time.time - _firstClickTime < delay)
{
//双击
_oneClick = false;
IsMove = false;//移动暂停
}
else
{
//Debug.LogError("here");
_oneClick = true;
IsMove = true;//开始移动
_firstClickTime = Time.time;
//1. 获取鼠标点击位置
//创建射线;从摄像机发射一条经过鼠标当前位置的射线
Ray ray = firstCamera.ScreenPointToRay(Input.mousePosition);
//发射射线
hitInfo = new RaycastHit();
if (Physics.Raycast(ray, out hitInfo))
{
//Debug.LogError("hitInfo.collider.tag = " + hitInfo.collider.name);
//Debug.LogError("hitInfo.collider.name = " + hitInfo.collider.name);
//获取碰撞点的位置
if (hitInfo.collider.tag == "Ground")
{
//Debug.LogError(hitInfo.collider.name);
targetVector3 = hitInfo.point; //Debug.LogError("hitInfo.point = " + hitInfo.point);
//Debug.LogError("距离为:" + Vector3.Distance(player.transform.position, hitInfo.point));
targetVector3.y = targetVector3.y + 1.5f; ;
//朝向点击位置
player.transform.LookAt(targetVector3); GameObject.Destroy(mouseIcon);
foreach (var kk in allmouseIcon)
{
GameObject.Destroy (kk);
}
allmouseIcon.Clear();
CreateMousePoint(hitInfo.point); IsOver = false;
} }
Debug.DrawLine(ray.origin, hitInfo.point, Color.red);//画出射线
}
}
if (IsMove == true)
{ // Debug.Log("m_Play" + m_Play);
//2. 让角色移动到目标位置 MoveTo(targetVector3); } player.GetComponent<Rigidbody>().velocity = Vector3.zero; if (Input.GetMouseButton())
{
if (player == null)
return;
//视角移动
player.transform.Rotate(, -Input.GetAxis("Mouse X") * sensitivityX, );
}
}
//UI射线无响应
bool CheckGuiRaycastObjects()
{
PointerEventData eventData = new PointerEventData(eventSystem);
eventData.pressPosition = Input.mousePosition;
eventData.position = Input.mousePosition; List<RaycastResult> list = new List<RaycastResult>();
graphicRaycaster.GetComponent<GraphicRaycaster>().Raycast(eventData, list);
//Debug.LogError("list.Count = " + list.Count);
return list.Count > ;
}
public void FixedUpdate()
{
//手动控制移动
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
//朝一个方向移动 new Vector3(h, 0, v) * playerMoveSpeed * Time.deltaTime是个向量
player.transform.Translate(new Vector3(, , v) * playerMoveSpeed * Time.deltaTime);
player.transform.Rotate(new Vector3(, h, ) * rotateSpeed * Time.deltaTime);
}
//让角色移动到目标位置
private void MoveTo(Vector3 tar)
{
if (!IsOver)
{
Vector3 offSet = tar - player.transform.position; player.transform.position += offSet.normalized * movespeed * Time.deltaTime; if (Vector3.Distance(tar, player.transform.position) < 0.5f)
{
IsOver = true;
player.transform.position = tar; }
}
}
//创建鼠标点击点的图标及移动动画
private GameObject mouseIcon;
public List<GameObject> allmouseIcon;
private void CreateMousePoint(Vector3 pos)
{
Vector3 disnum;
Vector3 tempOrigin = new Vector3 (player.transform.position.x, 0.1f, player.transform.position.z);
screenView_1Main.myEquipSearch.isshoudongfollow = false;
if (Vector3.Distance (tempOrigin, pos)>=)
{
for (int i = ; i < Vector3.Distance (tempOrigin, pos)/; i++) {
mouseIcon = Resources.Load<GameObject>("ui/direction/Arrow");
mouseIcon = GameObject.Instantiate(mouseIcon);
allmouseIcon.Add (mouseIcon);
}
//Debug.Log (allmouseIcon.Count);
}
else
{
mouseIcon = Resources.Load<GameObject>("ui/direction/Arrow");
mouseIcon = GameObject.Instantiate(mouseIcon);
allmouseIcon.Add(mouseIcon);
} disnum = (pos- tempOrigin).normalized*Vector3.Distance (tempOrigin, pos)/allmouseIcon.Count;
for (int i = allmouseIcon.Count; i > ; i--)
{
int temp = i;
allmouseIcon[temp-].transform.position = tempOrigin + disnum * i;
allmouseIcon[temp-].transform.localEulerAngles = new Vector3(mouseIcon.transform.localEulerAngles.x, player.transform.localEulerAngles.y, mouseIcon.transform.localEulerAngles.z);//图标方向和摄像机方向一致
} } }

unity中让物体移动到鼠标点击地面任一点的位置(单击移动和双击暂停移动)并生成图标的更多相关文章

  1. unity中让摄像机移动到鼠标点击的位置和鼠标控制平移视角

    private Vector3 targetVector3; private float movespeed=0.5f; private bool IsOver = true; private Gam ...

  2. 在unity中用鼠标实现在场景中拖动物体,用鼠标滚轮实现缩放

    在场景中添加一个Plan,Camera,Directional Light,Cube.添加两个脚本scrollerScirpt(挂在Camera),CubeDragScript(挂在Cube上). 1 ...

  3. unity中实现物体在一定角度范围内来回旋转

    using System.Collections; using System.Collections.Generic; using UnityEngine; public class Rotate : ...

  4. html5中canvas的使用 获取鼠标点击页面上某点的RGB

    1.html5中的canvas在IE9中可以跑起来.在IE8则跑不起来,这时候就需要一些东西了. 我推荐这种方法,这样显得代码不乱. <!--[if lt IE9]> <script ...

  5. 关于Unity中NGUI图片精灵响应鼠标的方法

    我在Unity里做NGUI的时候发现一个问题. 在Unity2D场景当中,一个精灵图片只要加上了Box Collider或者Box Collider2D,就可以相应OnMouseEnter和OnMou ...

  6. Unity中Instantiate物体失效问题

    才开始学Unity,开始总是这样用Instantiate函数: GameObject temp = (GameObject)Instantiate(bulletSource, transform.po ...

  7. Unity中获取物体的子物体

    如果想获取一级子节点 foreach (Transform child in this.transform) { Debug.Log(child.name); } 如果想获取所有子节点 foreach ...

  8. Unity中使物体自动寻路的方法

    在做一个FPS游戏时,需要敌方自动找到玩家方位并向玩家移动,在查找资料(并走了不少坑)后,我试了三个方法,经测试,这三个方法都能实现自动寻路功能. 方法一:使用Mathf.Lerp()方法 代码很简单 ...

  9. Unity中激活子物体

    void GetChildrenAndSetActive() { Transform[] imageTargetObjects = GetComponentsInChildren<Transfo ...

随机推荐

  1. 从flask视角学习angular(一)整体对比

    写在前面 前端框架完全不懂. 看着angular中文官网的英雄编辑器教程和核心知识,用偷懒的类比法,从flask django的角度 记录一下自己对angular的理解. 作为工科的武曲,自己的体会是 ...

  2. DEBUG(2)--函数的输入参数要做适当的检查

      今天在调试程序时发现,在单步运行的情况下,程序执行没有问题,但是直接运行就会出问题.出问题的代码如下 for(int col=0;col<=9;++col) { int killid=Pos ...

  3. Django 基础介绍

    Django 介绍 Python下有许多款不同的 Web 框架.Django是重量级选手中最有代表性的一位.许多成功的网站和APP都基于Django. Django是一个开放源代码的Web应用框架,由 ...

  4. ubuntu下安装Firefox中国版解决Ubuntu与Windows下Firefox账号同步问题(已解决)

    1. 下载最新版本火狐Linux版 下载地址:http://firefox.com.cn/download/ 选择火狐Linux64-bit版,下载后文件为:Firefox-latest-x86_64 ...

  5. English trip V1 - 20.Look at me 看着我 Teacher:Solo Key: 声调(英语默认就声调[rising]和降调[falling]两种)

    In this lesson you will learn to describe a person. 课上内容(Lesson) appearance  -> ap pea ran ce  外貌 ...

  6. 11月29日 The Rails philosophy 完成rails on guide 的第一章getting started with rails

    the rails philosophy includes two major guiding principles: Don't repeat yourself: DRY is a principl ...

  7. p2725 Stamps

    背包. #include <iostream> #include <cstdio> #include <cmath> #include <algorithm& ...

  8. n!的位数 斯特林公式

    #include<iostream> #include<cstring> #include<string> #include<cstdio> #incl ...

  9. Mysql 中如何创建数据库和数据表

    这里的数据库为:user  数据表为 aaa mysql –uroot –p                 进入mysql create database user;            创建数据 ...

  10. 内存池、进程池、线程池介绍及线程池C++实现

    本文转载于:https://blog.csdn.net/ywcpig/article/details/52557080 内存池 平常我们使用new.malloc在堆区申请一块内存,但由于每次申请的内存 ...