Click the right mouse button, the character will move to the right-click position

Click the right mouse button, the character will move to the right-click position

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;
            }

        }

    }
}

The second small method

This method is a bit complicated and practical, the following is the simple code

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);

    }

}
----

Off topic, VS console test code, in order to prevent flashback when running, add a code

Console.ReadKey();

hot key

  • boldCtrl + B
  • ItalicsCtrl + I
  • quoteCtrl + Q
  • insert linkCtrl + L
  • insert codeCtrl + K
  • Insert pictureCtrl + G
  • boost titleCtrl + H
  • ordered listCtrl + O
  • unordered listCtrl + U
  • horizontal lineCtrl + R
  • revokeCtrl + Z
  • redoCtrl + Y

Markdown and extensions

Markdown is a lightweight markup language that allows people to write documents in a plain text format that is easy to read and write, which is then converted into richly formatted HTML pages. - [Wikipedia]

Use simple symbols to identify different headings, mark certain text as bold or italic , create a link , etc. See help for detailed syntax? .

This editor supports Markdown Extra , extending many useful functions. Please refer to Github for details .

sheet

Markdown Extra table syntax:

project price
Computer $1600
Phone $12
Pipe $1

Alignment can be defined using colons:

project price quantity
Computer 1600 yuan 5
Phone $12 12
Pipe 1 Yuan 234

Definition list

Markdown Extra  defines list syntax:
item 1
item 2
Definition A
Definition B
item 3
Definition C

Definition D

define D content

code block

Code block syntax follows standard markdown code, for example:

@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'''

footnote

Generate a footnote 1 .

content

Use [TOC]to generate the directory:

Mathematical formula

Render LaTex math equations with MathJax, see math.stackexchange.com for details .

  • Inline formula, the mathematical formula is: Γ ( n ) = ( n - 1 ) !nN
  • Block level formula:

x=b±b24ac2 a

More LaTex syntax can be found here .

UML php:

Sequence diagrams can be rendered:

Created with Raphaël 2.1.0 张三 张三 李四 李四 嘿,小四儿, 写博客了没? 李四愣了一下,说: 忙得吐血,哪有时间写。

Or flow chart:

Created with Raphaël 2.1.0 开始 我的操作 确认? 结束 yes no
  • For sequence diagram syntax, refer to here ,
  • For flowchart syntax, refer here .

Blogging offline

Even if users do not have a network, they can write blogs offline through this editor (just enter write.blog.csdn.net/mdeditor in the browser they have used before . The Markdown editor uses the browser offline storage to store The content is saved locally.

When a user writes a blog, the content is stored in the browser cache in real time, and the content will not be lost when the user closes the browser or in other abnormal situations. When the user opens the browser again, the unpublished content that the user was editing last time is displayed.

After the blog is published, the local cache will be deleted. 

User can chooseSave the blog you are writing to the server draft, even if you change the browser or clear the cache, the content will not be lost.

Note: Although browser storage is reliable most of the time, for your data security, please be sure to publish it in time or save it to the server draft box after connecting to the Internet .

Browser Compatible

  1. At present, this editor has the most complete support for the Chrome browser. We recommend that you use a newer version of Chrome.
  2. IE9 and below are not supported
  3. IE9, 10, 11 have the following problems
    1. Offline functionality is not supported
    2. IE9 does not support file import and export
    3. IE10 does not support drag and drop file import


  1. Here are the footnotes .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325165367&siteId=291194637