unity脚本物体移动,旋转,属性可见性

1.如下例子:在unity中跑一下就能测出结果,点击鼠标,按住w键,S键

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//在unity组件列表中的名字
[AddComponentMenu("Demoxxxx")]
public class Demo02 : MonoBehaviour
{
    //私有属性,但加上[SerializeField]描述后unity中可见。
    [SerializeField]
    private float speed;

    void Start()
    {
        Debug.Log("Hello World");//在unity控制台中打印信息
    }
    void Update()
    {
        if (Input.GetKey(KeyCode.W)) {//监听 W 键
            // Time.deltaTime表示:保证一定的帧率
            this.transform.Translate(new Vector3(0F, 0F, 20F) * Time.deltaTime , Space.World);//像指定方向移动
        } else if (Input.GetKey(KeyCode.S)) {
            this.transform.Translate(new Vector3(0F, 0F, -20F) * Time.deltaTime, Space.World);//像指定方向移动
        }
        if (Input.GetMouseButtonDown(0)) {//鼠标右键,点一下动一下
            this.transform.Rotate(new Vector3(0F, 0F, 100F) * Time.deltaTime, Space.World);//旋转
        } else if (Input.GetMouseButtonDown(1)) {//鼠标左键,点一下动一下

        //当前对象围绕指定物体旋转,Camera.main.transform.position当前摄像头,Vector3.up方向,速度
        this.transform.RotateAround(Camera.main.transform.position, Vector3.up, 1000F * Time.deltaTime);

        }
    }
}

 

 

posted @ 2020-12-11 19:50  ~~mr.li~~  阅读(395)  评论(0编辑  收藏  举报