点击鼠标右键,角色随之移动到右键位置
using UnityEngine;
using System.Collections;
//角色移动控制
public class Move : MonoBehaviour {
// Use this for initialization
private bool finish = true;
private Vector3 pos;
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(1))//鼠标点击右键
{//1. 获取鼠标点击位置
//创建射线;从摄像机发射一条经过鼠标当前位置的射线
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//发射射线
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(ray,out hit))
{
if (hit.collider.tag=="TT")//地板plane的标签
{
pos = hit.point;//点击位置,即,角色要去的位置
// pos.y=1f;
finish = false;
}
}
}
if (!finish)
{
Vector3 offset = pos - transform.position;
transform.position += offset.normalized * 20 * Time.deltaTime;
if (Vector3.Distance(pos,transform.position)<1f)
{
transform.position = pos;
finish = true;
}
}
}
}
第二种小方法
这种方法有点小复杂,实用性比较好,下面是简单代码
using UnityEngine;
using System.Collections;
public class Move_1 : MonoBehaviour {
// Use this for initialization
/// <summary>
/// 物体移动到鼠标点击位置
/// </summary>
public float speed = 5f;//移动速度
private PlayDir dir;//依据鼠标点击位置改变朝向,也是个类
private CubeCS cubeCS;//控制钢体移动的类
private void Awake()
{
dir = this.GetComponent<PlayDir>();
cubeCS = this.GetComponent<CubeCS>();
}
void Start () {
}
// Update is called once per frame
void Update () {
}
private void FixedUpdate()
{
//获取当前物体到目标的距离
float distance = Vector3.Distance(dir.targetPosition, transform.position);
if (distance>0.1f)
{
cubeCS.rb.velocity = transform.forward * speed * distance;
}else
{
cubeCS.rb.velocity = transform.forward * 0;
}
}
}
-------------------------------
using UnityEngine;
using System.Collections;
/// <summary>
/// *控制刚体的移动与旋转
/// </summary>
public class CubeCS : MonoBehaviour {
public Rigidbody rb;
public float speed = 5;
public float angularSpeed = 3;
// Use this for initialization
private void Awake()
{
rb = this.GetComponent<Rigidbody>();
}
void Start () {
}
// Update is called once per frame
void Update () {
}
private void FixedUpdate()
{
Move();
}
private void Move()
{
float v = Input.GetAxis("Vertical");
rb.velocity = this.transform.forward * v * speed;
float h = Input.GetAxis("Horizontal");
rb.angularVelocity = this.transform.up * h * angularSpeed;
}
}
-----------------------------------
using UnityEngine;
using System.Collections;
/// <summary>
/// 依据鼠标点击位置改变朝向
/// </summary>
public class PlayDir : MonoBehaviour {
// Use this for initialization
// public GameObject effect;
private bool isMoving = false;// 左键状态
public Vector3 targetPosition;
private void Awake()
{
targetPosition = this.transform.position;
}
void Start () {
}
// Update is called once per frame
void Update () {
//判断右键是否按下
if (Input.GetMouseButtonDown(1))
{
print("15");
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
bool isCollider = Physics.Raycast(ray,out hitInfo);
if (isCollider&&hitInfo.collider.tag=="TT")
{
ShowClickEffect(hitInfo.point);
isMoving = true;
LookAtTarget(hitInfo.point);
}
}
}
void ShowClickEffect(Vector3 hitpoint)
{
hitpoint = new Vector3(hitpoint.x, hitpoint.y + 0.3f, hitpoint.z);
// GameObject.Instantiate(effect,hitpoint,Quaternion.identity);
}
void LookAtTarget(Vector3 hitpoint)
{ //获取触发目标物体的位置信息
targetPosition = hitpoint;
//将目标位置的y轴修改为当前物体的y轴
targetPosition = new Vector3(targetPosition.x, transform.position.y, targetPosition.z);
//当前物体朝向目标位置
this.transform.LookAt(targetPosition);
}
}
----
题外话,VS 控制台测试代码,运行时为了防止闪退,加一句代码
Console.ReadKey();
快捷键
- 加粗
Ctrl + B
- 斜体
Ctrl + I
- 引用
Ctrl + Q
- 插入链接
Ctrl + L
- 插入代码
Ctrl + K
- 插入图片
Ctrl + G
- 提升标题
Ctrl + H
- 有序列表
Ctrl + O
- 无序列表
Ctrl + U
- 横线
Ctrl + R
- 撤销
Ctrl + Z
- 重做
Ctrl + Y
Markdown及扩展
Markdown 是一种轻量级标记语言,它允许人们使用易读易写的纯文本格式编写文档,然后转换成格式丰富的HTML页面。 —— [ 维基百科 ]
使用简单的符号标识不同的标题,将某些文字标记为粗体或者斜体,创建一个链接等,详细语法参考帮助?。
本编辑器支持 Markdown Extra , 扩展了很多好用的功能。具体请参考Github.
表格
Markdown Extra 表格语法:
项目 | 价格 |
---|---|
Computer | $1600 |
Phone | $12 |
Pipe | $1 |
可以使用冒号来定义对齐方式:
项目 | 价格 | 数量 |
---|---|---|
Computer | 1600 元 | 5 |
Phone | 12 元 | 12 |
Pipe | 1 元 | 234 |
定义列表
- Markdown Extra 定义列表语法:
- 项目1
- 项目2
- 定义 A
- 定义 B
- 项目3
- 定义 C
-
定义 D
定义D内容
代码块
代码块语法遵循标准markdown代码,例如:
@requires_authorization
def somefunc(param1='', param2=0):
'''A docstring'''
if param1 > param2: # interesting
print 'Greater'
return (param2 - param1 + 1) or None
class SomeClass:
pass
>>> message = '''interpreter
... prompt'''
脚注
生成一个脚注1.
目录
用 [TOC]
来生成目录:
数学公式
使用MathJax渲染LaTex 数学公式,详见math.stackexchange.com.
- 行内公式,数学公式为:
Γ(n)=(n−1)!∀n∈N 。 - 块级公式:
更多LaTex语法请参考 这儿.
UML 图:
可以渲染序列图:
或者流程图:
离线写博客
即使用户在没有网络的情况下,也可以通过本编辑器离线写博客(直接在曾经使用过的浏览器中输入write.blog.csdn.net/mdeditor即可。Markdown编辑器使用浏览器离线存储将内容保存在本地。
用户写博客的过程中,内容实时保存在浏览器缓存中,在用户关闭浏览器或者其它异常情况下,内容不会丢失。用户再次打开浏览器时,会显示上次用户正在编辑的没有发表的内容。
博客发表后,本地缓存将被删除。
用户可以选择 把正在写的博客保存到服务器草稿箱,即使换浏览器或者清除缓存,内容也不会丢失。
注意:虽然浏览器存储大部分时候都比较可靠,但为了您的数据安全,在联网后,请务必及时发表或者保存到服务器草稿箱。
浏览器兼容
- 目前,本编辑器对Chrome浏览器支持最为完整。建议大家使用较新版本的Chrome。
- IE9以下不支持
- IE9,10,11存在以下问题
- 不支持离线功能
- IE9不支持文件导入导出
- IE10不支持拖拽文件导入
- 这里是 脚注 的 内容. ↩