zoukankan      html  css  js  c++  java
  • Deme_遥感控制物体移动(涉及遮罩,小摄像机跟随)

    using UnityEngine;
    using System.Collections;
    using UnityEngine.EventSystems;
    
    public class JoyController : MonoBehaviour,IDragHandler,IEndDragHandler {
    
        //摇杆圆盘半径
        public float radius = 75f;
        //摇杆原始位置
        private Vector3 origin;
        //玩家的角色控制器
        private CharacterController player;
        //玩家移动速度
        public float speed = 10;
        //声明一个方向向量
        Vector3 dir;
    
        void Start()
        {
            //获取当前位置
            origin = transform.position;
            //找到主角玩家
            player = GameObject.FindWithTag ("Player").
                GetComponent<CharacterController> ();
        }
    
        void Update()
        {
            //玩家移动
            player.SimpleMove (new Vector3(dir.x,0,dir.y) * speed * Time.deltaTime);
        }
    
        /// <summary>
        /// 摇杆拖拽中
        /// </summary>
        /// <param name="eventData">Event data.</param>
        public void OnDrag (PointerEventData eventData)
        {
            //计算起点位置与当前鼠标位置的距离
            float distance = Vector3.Distance (origin, Input.mousePosition);
            //求方向向量
            dir = Input.mousePosition - origin;
            //如果拖拽在圆盘范围内
            if (distance <= radius) {
                //摇杆随鼠标移动而移动
                transform.position = Input.mousePosition;
            } else {
                //实际向量
                Vector3 real = dir.normalized * radius;
                //最终摇杆的位置
                transform.position = real + origin;
            }
        }
        /// <summary>
        /// 结束拖拽
        /// </summary>
        /// <param name="eventData">Event data.</param>
        public void OnEndDrag (PointerEventData eventData)
        {
            dir = Vector3.zero;
            //回到初始位置
            transform.position = origin;
        }
    }

    要注意的几个位置,添加相应组件。1,脚本挂载在遥感中心点Controller上.2,小摄像头(设置正交)挂为Player子物体跟随玩家移动3,遮罩设置

  • 相关阅读:
    分页查询
    matlab 常用机器学习算法的实现
    powerpoint(ppt) 的制作
    powerpoint(ppt) 的制作
    libsvm 的使用
    libsvm 的使用
    TFRecord —— tensorflow 下的统一数据存储格式
    numpy 数据类型与 Python 原生数据类型
    numpy 数据类型与 Python 原生数据类型
    Python str 与 bytes 类型(Python2/3 对 str 的处理)
  • 原文地址:https://www.cnblogs.com/VR-1024/p/6020960.html
Copyright © 2011-2022 走看看