Unity 旋转动画(1)

来源:互联网 发布:淘宝网店卖衣服 编辑:程序博客网 时间:2024/06/06 14:00

此文主要讲让一个地球模型 响应键盘上的4个方向键 做旋转动画!

1--创建一个Sphere 更名为“Earth”,添加光源,相机,再调整相机的位置。

2--在网上找一张地球的展开平面图,在Project中新建一个文件夹IMG,把图拖到IMG文件夹中 在把图附加到Hierarchy 中Earth对象上!结果如下图:


3-- 在Project 新建一个C# Script文件(这里命名为“EarthCtr”),在里面写如下代码(切记一定编写好后要保存哦!):

using UnityEngine;
using System.Collections;


public class EarthCtr : MonoBehaviour {


private GameObject earth;//声明游戏对象earth
void Start () 
{
earth = GameObject.Find ("Earth");//找到相对应的对象
}
// Update is called once per frame
void Update () 
{
//当按下键盘的4个方向键时 让Earth 旋转 
if(Input.GetKey(KeyCode.UpArrow))
{
earth.transform.Rotate (Vector3.right*Time.deltaTime*20);
}
if (Input.GetKey (KeyCode.DownArrow)) 
{
earth.transform.Rotate(Vector3.left*Time.deltaTime*20);
}
if (Input.GetKey (KeyCode.RightArrow)) 
{
earth.transform.Rotate(Vector3.down*Time.deltaTime*20);
}
if (Input.GetKey (KeyCode.LeftArrow)) 
{
earth.transform.Rotate(Vector3.up*Time.deltaTime*20);
}
}
}

4-- 回到Unity 中把EarthCtr.cs文件 附加到 Earth对象中,然后运行,分别按下四个方向键看其效果!

注释:上面的转动方向和键盘上的方向键是 按“左手定理” 来判断的!大拇指指示(转动的方向)4指指示(向量的方向) 

比如:左手做一个“赞”的手势 是 大拇指向上 ,4指向右!是吧!呵呵!就是按下向上的方向键 改变Vector3 的right !是吧!!!

1 0
原创粉丝点击