02.Unity 脚本笔记

gameObject的transform属性可以进行位置、旋转、大小的设置。

位置:

position:直接到指定位置

Translate:通过向量渐变效果

旋转:rotate

大小:localScale

代码如下:

using UnityEngine;
using System.Collections;

public class HeroMove : MonoBehaviour
{

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        //按下w前进,按下s后退
        if (Input.GetKey(KeyCode.W))
        {
            transform.Translate(Vector3.forward * Time.deltaTime);
        }
        else if (Input.GetKey(KeyCode.S))
        {
            transform.Translate(Vector3.back * Time.deltaTime);
        }
        //按下a向左旋转,按下d向右旋转
        else if (Input.GetKey(KeyCode.A))
        {
            transform.Rotate(Vector3.down);
        }
        else if (Input.GetKey(KeyCode.D))
        {
            transform.Rotate(Vector3.up);
        }
    }
}


Vector3类中内置了up、down、left、right、forward、back、zero几个简化的静态成员变量,可以不用苦逼的写向量了。

有关鼠标判断:  Input.GetMouseButtonDown(0) GetMouseButtonUp    GetMouseButton。

GetMouseButton也是只要按下就一直返回true。  方法中的参数:0为左键,1为右键,2为滚轮。

获取滚轮值:Input.GetAxis(“Mouse ScrollWheel”),返回值为0表示不动,值大于0表示向前,值小于0表示后退。

然后自己写了一个摄像机视角缩放的代码:

using UnityEngine;
using System.Collections;

public class CameraScale : MonoBehaviour
{
    private float i;

    // Use this for initialization
    void Start()
    {
        i = 0;
    }

    // Update is called once per frame
    void Update()
    {
        //滚动鼠标滚轮缩放相机范围  就是缩放视角
        //Input.GetAxis(),值为0表示不懂,值大于0表示向前,值小于0表示后退
        i = Input.GetAxis("Mouse ScrollWheel");
        if (i < 0 )
        {
            //添加这一层判断是为了限制视角缩放的范围
            if (camera.fieldOfView <= 70)
            {
                camera.fieldOfView += 2f;
            }
        }
        else if (i > 0 )
        {
            if (camera.fieldOfView >= 5)
            {
                 camera.fieldOfView -= 2f;
            }  
        }
    }

}

键盘判断:Input.GetKeyDown(KeyCode.Space )   Input.GetKeyUp(KeyCode.LeftArrow)  Input.GetKey(KeyCode.A),GetKey像RepeatButton一样只要按下保持住就一直返回true。



播放动画,参数为动画名称
this.gameObject.animation.Play("Idle");







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值