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");