射线 鼠标控制物体前进
2017-11-08 15:13
127 查看
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playercontrol : MonoBehaviour
{
//1.获取鼠标点击位置
//通过射线来实现
//2.让当前目标移动到目标位置
private Vector3 target;//获取目标位置
//private bool panduan = t;
public float speed;
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(1))
{
//创建射线,从主摄像机发射射线
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//发射射线
RaycastHit h = new RaycastHit();//存储碰撞信息
if(Physics.Raycast(ray,out h))//检测信息,如果调用h或者下面的hits必须先检测
{
target = h.point;
target.y = 0.5f;
}
//RaycastHit[] hits = Physics.RaycastAll(ray);
// for (int i = 0; i < hits.Length; i++)
// {
// if (Physics.Raycast(ray, out hits[i]))
// {
// if (hits[i].collider.name == "Plane")
// {
// target = hits[i].point;
// target.y = 0.5f;
// }
// }
// }
}
move(target);
}
//让目标移动到角色位置
private void move(Vector3 tar)
{
if (Vector3.Distance(transform.position, tar) > 0.1f)
{
Vector3 i = tar - transform.position;
transform.position += i.normalized * speed * Time.deltaTime;
}
else
{
transform.position = tar;
}
}
}
using System.Collections.Generic;
using UnityEngine;
public class playercontrol : MonoBehaviour
{
//1.获取鼠标点击位置
//通过射线来实现
//2.让当前目标移动到目标位置
private Vector3 target;//获取目标位置
//private bool panduan = t;
public float speed;
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(1))
{
//创建射线,从主摄像机发射射线
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//发射射线
RaycastHit h = new RaycastHit();//存储碰撞信息
if(Physics.Raycast(ray,out h))//检测信息,如果调用h或者下面的hits必须先检测
{
target = h.point;
target.y = 0.5f;
}
//RaycastHit[] hits = Physics.RaycastAll(ray);
// for (int i = 0; i < hits.Length; i++)
// {
// if (Physics.Raycast(ray, out hits[i]))
// {
// if (hits[i].collider.name == "Plane")
// {
// target = hits[i].point;
// target.y = 0.5f;
// }
// }
// }
}
move(target);
}
//让目标移动到角色位置
private void move(Vector3 tar)
{
if (Vector3.Distance(transform.position, tar) > 0.1f)
{
Vector3 i = tar - transform.position;
transform.position += i.normalized * speed * Time.deltaTime;
}
else
{
transform.position = tar;
}
}
}
相关文章推荐
- 射线检测,单击鼠标是否单击到一个物体上
- Unity简单利用屏幕坐标转换实现鼠标控制物体移动
- Unity学习2 鼠标点击控制物体移动
- UE4学习4 用射线控制物体转圈圈 C++
- UI界面基础如何用鼠标控制物体移动
- 【Unity 3D学习】获取鼠标滑动方向(上下左右)-控制摄像机围绕物体旋转
- 控制物体随鼠标旋转
- unity 点击鼠标左键控制物体360展示
- (glut)应用arcball原理实现鼠标左键控制物体旋转(2)
- Cocos2D-html5 2.1.4 实现鼠标控制物体旋转
- 鼠标控制摄像机旋转,平移,物体的移动
- 最新 Unity3D鼠标滑轮控制物体放大缩小 [
- Unity之一天一个技术点(十四)---通过鼠标控制镜头绕着某物体旋转
- 物体移动--通过改变transform--鼠标控制
- 利用射线实现鼠标控制角色转向和移动
- 通过鼠标控制镜头绕着某物体旋转
- 鼠标控制物体移动360旋转和视距
- 【unity3d游戏开发之基础篇】利用射线实现鼠标控制角色转向和移动(角色移动一)
- 鼠标控制摄像机围绕物体旋转拍摄
- 触发器,射线,鼠标控制移动,左移运算符