• unity3d中最简单物体的旋转移动


    void Awake (){  

    }       

    //初始化函数,在游戏开始时系统自动调用。一般用来创建变量之类的东西。  

      

    void Start(){  

    }  

    //初始化函数,在所有Awake函数运行完之后(一般是这样,但不一定),在所有Update函数前系统自动条用。一般用来给变量赋值。

    我们通常书写的脚本,并不会定义[ExecuteInEditMode]这个Attribute,所以AwakeStart都只有在Runtime中才会执行。

     

    using UnityEngine;
    using System.Collections;

     

    public class square1 : MonoBehaviour {
    public float m_speed = 1;
    protected Transform m_transform;
    //是否被拖拽
    private bool onDrag=false;
    //旋转速度
    public float speed=6f;
    //阻尼速度
    private float tempSpeed;
    //水平
    private float axisX;
    //竖直
    private float axisY;
    //鼠标移动的距离
    private float cXY;

     

    // Use this for initialization
    void Start () {
    //调用
    m_transform = this.transform;

    }

    // Update is called once per frame
    void Update () {

     

    float movev = 0;
    float moveh = 0;
    if (Input.GetKey(KeyCode.UpArrow))
    {
    movev += m_speed * Time.deltaTime;
    }

     

    if (Input.GetKey(KeyCode.DownArrow))
    {
    movev -= m_speed * Time.deltaTime;
    }

     

    if (Input.GetKey(KeyCode.RightArrow))
    {
    moveh += m_speed * Time.deltaTime;
    }

     

    if (Input.GetKey(KeyCode.LeftArrow))
    {
    moveh -= m_speed * Time.deltaTime;
    }

     

    gameObject.transform.Rotate(new Vector3(axisX,axisY,0)*Rigid(),Space.World);
    if (!Input.GetMouseButton(0))
    {
    onDrag = false;
    }
    transform.Translate(Vector3.forward*Time.captureFramerate);
    this.m_transform.Translate(new Vector3(moveh, 0, movev));
    }

    //接受鼠标按下
    void OnMouseDown()
    {
    axisX = 0f;
    axisY = 0f;
    }

     

    //鼠标拖拽时的操作
    void OnMouseDrag()
    {
    onDrag = true;
    axisX = -Input.GetAxis("Mouse X");
    axisY = Input.GetAxis("Mouse Y");
    cXY = Mathf.Sqrt(axisX*axisX+axisY*axisY);
    if (cXY==0f)
    {
    cXY = 1f;
    }
    }
    //计算阻尼
    float Rigid()
    {
    if (onDrag)
    {
    tempSpeed = speed;
    }
    else
    {
    if (tempSpeed>0)
    {
    tempSpeed -= speed * 2 * Time.deltaTime / cXY;
    }
    else
    {
    tempSpeed = 0;
    }
    }
    return tempSpeed;
    }
    //实现鼠标的拖拽移动
    IEnumerator OnMouse()
    {
    var camear = Camera.mainCamera;
    if (camear)
    {
    Vector3 screenPosition = camear.WorldToScreenPoint(transform.position);
    Vector3 mscreenPosition = new Vector3(Input.mousePosition.x,Input.mousePosition.y,screenPosition.z);
    Vector3 offset = transform.position - camear.ScreenToWorldPoint(mscreenPosition);
    print("drag strating:"+transform.name);

     

    while (Input.GetMouseButton(0))
    {
    mscreenPosition = new Vector3(Input.mousePosition.x,Input.mousePosition.y,screenPosition.z);
    transform.position = offset + camear.ScreenToWorldPoint(mscreenPosition);
    yield return new WaitForEndOfFrame();
    }
    print("drag compeleted");
    }
    }
    }

     

     

  • 相关阅读:
    java枚举类的常见用法
    Sublime Text 3 3126 安装+注册码
    XtraFinder
    WinForm多线程+委托防止界面假死
    Win10添加简体中文美式键盘的方法
    查看sqlserver版本
    C#,PHP对应加密函数
    PHP文件缓存实现
    √GMAP.NET 地图
    JSON C# Class Generator ---由json字符串生成C#实体类的工具
  • 原文地址:https://www.cnblogs.com/FLWL/p/3653785.html
Copyright © 2020-2023  润新知