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. cmd中mvn命令,出现No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?

    在cmd里执行mvn命令,出错 查看mvn -v 发现mvn运行在jre上,更改高级设置.我的电脑-->属性-->高级系统设置-->环境变量 更改完之后,再次查看 mvn -v 搞定 ...

  2. 从flask视角理解angular(二)Blueprint VS Component

    Component类似flask app下面的每个blueprint. import 'rxjs/add/operator/switchMap'; import { Component, OnInit ...

  3. synchronized同步方法《一》

    1.方法内的变量为线程安全 "非线程安全"问题存在于"实例变量"中,如果是方法内部的私有变量,则不存在"非线程安全"问题,所得结果也就是&q ...

  4. Windows下使用创建多层文件夹 SHCreateDirectoryEx 函数需要注意的问题

    1.在使用SHCreateDirectoryEx函数创建多层文件夹的过程中,发现在文件夹路径中,只能使用\\而不能使用/,否则将创建文件夹失败. 2.下面为在MFC中使用的代码片段 CString n ...

  5. html+css+js实现类似音乐app似的列表播放

    最近做了一个类似于音乐app里面列表播放的功能,主要是音频播放和按钮状态的联动: 界面如下: 如上图所示 上面有一个播放按钮 下面有一个播放列表 上面还有一个歌曲长度的总时长 上面一个按钮能控制下面所 ...

  6. Axure 元件焦点的控制

    讲解如何控制光标的位置,主要学习了以下三种场景: 1.点击邮箱或者密码时,光标分别自动定位到email.password的文本框处: 2.还有将密码对应的文本框的内容设置为密码的格式(····) 3. ...

  7. 几种RAID介绍(总结)

    概念 RAID是Redundent Array of Inexpensive Disks的缩写,简称为“磁盘阵列”.后来RAID中的字母I被改作了Independent,RAID就成了“独立冗余磁盘阵 ...

  8. boke练习: springboot整合springSecurity出现的问题,传递csrf

    boke练习: springboot整合springSecurity出现的问题,传递csrf freemarker模板 在html页面中加入: <input name="_csrf&q ...

  9. English trip V1 - 11.What's That? 那是什么?Teacher:Patrick Key:There's/There are

    In this lesson you will learn to describe the position of an object. 在本课中,您将学习如何描述对象的位置. 课上内容(Lesson ...

  10. 请问WCF 跟 WebService之间的相同跟异同

    https://social.msdn.microsoft.com/Forums/zh-CN/c06420d1-69ba-4aa6-abe5-242e3213b68f/wcf-webservice W ...