点击鼠标右键,角色随之移动到右键位置

博客介绍了如何通过点击鼠标右键实现角色移动,提供了一种实用的方法,并包含VS控制台测试代码。同时,文章还涵盖了Markdown语法、快捷键、离线写博客的兼容性和浏览器支持等内容。
摘要由CSDN通过智能技术生成

点击鼠标右键,角色随之移动到右键位置

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 () {
        //判断右键是否按下
        
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值