Unity3D学习笔记(十四):Animation旧动画
旧动画系统:需要用代码来驱动Animation

自动添加动画,红色按钮

能侦测同属一个物体身上的脚本方法

2、旧动画,进入Rig,修改Legacy

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimationManager : MonoBehaviour {
public GameObject prefs;
private float curTime = ;
private float interval = 1.0f;
void Update () {
if (Time.time < )
{
curTime += Time.deltaTime;
if (curTime >= interval)
{
Instantiate<GameObject>(prefs).transform.position = Vector3.right * Mathf.FloorToInt(Time.time);
curTime = ;
}
}
}
}
用代码控制Animation动画片段
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimationChange : MonoBehaviour {
public Animation anim;
void Update () {
if (Input.GetKeyDown(KeyCode.Q))
{
anim.Play("AnimationChangeMove");
}
if (Input.GetKeyDown(KeyCode.E))
{
anim.Play("AnimationChangeScale");
}
if (Input.GetKeyDown(KeyCode.S))
{
anim.Stop();
}
}
void SayBig()
{
Debug.Log("变大了");
}
void SaySmall()
{
Debug.Log("变小了");
}
}
递归树

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tree : MonoBehaviour {
public float size;
public GameObject treeGo;
public void CreateSelf()
{
if (size < 0.05f) return;//给递归添加限值
for (int i = ; i < ; i++)
{
GameObject go = Instantiate<GameObject>(treeGo);
go.transform.position = transform.position + transform.up * * size;
go.transform.rotation = transform.rotation;
go.transform.rotation *= Quaternion.AngleAxis(Random.Range(, ), Vector3.up) * Quaternion.AngleAxis(Random.Range(-, ), Vector3.right);
go.GetComponent<Tree>().size = size * 0.5f;
go.transform.localScale = Vector3.one * size * 0.5f;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAnimation : MonoBehaviour {
public Animation anim;
// Use this for initialization
void Start () { } // Update is called once per frame
void Update () {
//如果每帧检测到播放的是当前动画,则不会重复播放动画第一帧,会让动画继续播下去
//anim.Play("Idle");
if (Input.GetKeyDown(KeyCode.Alpha1))
{
anim.Play("Move");
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
anim.Play("Idle");
}
if (Input.GetKeyDown(KeyCode.Alpha3))
{
anim.Play("Death");
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum PlaySta
{
Move,
Idle,
Death
}
public class PlayerControl : MonoBehaviour {
public Animation anim;
public PlaySta playSta;
// Use this for initialization
void Start () {
playSta = PlaySta.Move;
} // Update is called once per frame
void Update () {
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
moveDir.x = h;
moveDir.z = v;
cd += Time.deltaTime;
Action();
}
void Action()
{
switch (playSta)
{
case PlaySta.Move:
Move();
break;
case PlaySta.Idle:
Idle();
break;
case PlaySta.Death:
Death();
break;
}
}
Vector3 moveDir;
Vector3 lookPoint;
RaycastHit hit;
public float moveSpeed = ;
void Move()
{
anim.Play("Move");
transform.position += moveDir * moveSpeed * Time.deltaTime;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, , << ))
{
lookPoint = hit.point;
lookPoint.y = transform.position.y;
transform.LookAt(lookPoint);
}
if (moveDir.magnitude < 0.01f)
{
playSta = PlaySta.Idle;
}
}
void Idle()
{
anim.Play("Idle");
if (moveDir.magnitude >= 0.01f)
{
playSta = PlaySta.Move;
}
}
float cd;
public void Damage()
{
cd = ;
playSta = PlaySta.Death;
}
void Death()
{
anim.Play("Death");
}
public void OnTriggerEnter(Collider other)
{
if (other.tag == "Monster")
{
if (cd > 0.5f)
{
Damage();
}
}
}
}
抛异常的用法:
try:包含异常的语句
try不能单独使用,要搭配catch和finally使用
catch:捕获异常,可以填参数
catch(Exception e){},(Exception是所有异常的基类)
finally:最后都会执行,作为替补收尾工作,
无论如何都会执行,即使前面有return
结果:输出“捕获到了异常”,而“”不会输出

Unity3D学习笔记(十四):Animation旧动画的更多相关文章
- python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例
python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例 新浪爱彩双色球开奖数据URL:http://zst.aicai.com/ssq/openInfo/ 最终输出结果格 ...
- (C/C++学习笔记) 十四. 动态分配
十四. 动态分配 ● C语言实现动态数组 C语言实现动态数组,克服静态数组大小固定的缺陷 C语言中,数组长度必须在创建数组时指定,并且只能是一个常数,不能是变量.一旦定义了一个数组,系统将为它分配一个 ...
- 【转】angular学习笔记(十四)-$watch(1)
本篇主要介绍$watch的基本概念: $watch是所有控制器的$scope中内置的方法: $scope.$watch(watchObj,watchCallback,ifDeep) watchObj: ...
- angular学习笔记(十四)-$watch(1)
本篇主要介绍$watch的基本概念: $watch是所有控制器的$scope中内置的方法: $scope.$watch(watchObj,watchCallback,ifDeep) watchObj: ...
- SharpGL学习笔记(十四) 材质:十二个材质球
材质颜色 OpenGL用材料对光的红.绿.蓝三原色的反射率来近似定义材料的颜色.象光源一样,材料颜色也分成环境.漫反射和镜面反射成分,它们决定了材料对环境光.漫反射光和镜面反射光的反射程度.在进行光照 ...
- Unity3D学习笔记(四)Unity的网络基础(C#)
一 网络下载可以使用WWW类下载资源用法:以下载图片为例WWW date = new WWW("<url>");yield return date;texture = ...
- Unity3D学习笔记(四):物理系统碰撞和预制体
Rigidbody(刚体组件):加了此组件游戏物体就变成刚体了 ----Mass(质量,单位kg):重力G = 质量m * 重力加速度g(g=9.81 m/s^2) --------冲量守恒定理 动量 ...
- Java学习笔记十四:如何定义Java中的类以及使用对象的属性
如何定义Java中的类以及使用对象的属性 一:类的重要性: 所有Java程序都以类class为组织单元: 二:什么是类: 类是模子,确定对象将会拥有的特征(属性)和行为(方法): 三:类的组成: 属性 ...
- [转]Unity3D学习笔记(四)天空、光晕和迷雾
原文地址:http://bbs.9ria.com/thread-186942-1-1.html 作者:江湖风云 六年前第一次接触<魔兽世界>的时候,被其绚丽的画面所折服,一个叫做贫瘠之地的 ...
- MYSQL进阶学习笔记十四:MySQL 应用程序优化!(视频序号:进阶_32)
知识点十五:MySQL 的应用程序优化(32) 一.访问数据库采用连接池 把连接当做对象或设备,统一放在‘连接池’里.凡是需要访问数据库的地方都从连接池里取连接 二.采用缓存减少对于MySQL的访问: ...
随机推荐
- Service 的 onStartCommand(Intent, int, int) 返回值
(1)START_NOT_STICKY If the system kills the service after onStartCommand() returns, do not recreate ...
- sql server 2005 使用Log Explorer查看和恢复数据
使用Log Explorer查看和恢复数据 Log Explorer 4.1.可用于SQL Server2005的日志查看工具 下载地址: http://download.csdn.net/ ...
- ubuntu设置目录容量大小
1:方法如下 sudo dd if=/dev/zero of=/root/disk1.img bs=2M count=10 // 2M*10=20M zero 是de ...
- 使用node.js 进行服务器端JavaScript编程
node.js 入门 node.js 可以运行在 Linux.Windows 和 Macintosh 等主流的操作系统上.在 Windows 平台上运行 node.js ...
- [LeetCode] 38. Count and Say_Easy
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- C#在splitContainer1控件和panel控件中显示窗体
现在有两个窗体 Form1 和Form2 Form1中有控件splitContainer1 和panel .控件.我们希望Form2在splitContainer1或者panel控件中显示 1:首先看 ...
- MYSQL主从不同步延迟原理分析及解决方案(摘自http://www.jb51.net/article/41545.htm)
1. MySQL数据库主从同步延迟原理.要说延时原理,得从mysql的数据库主从复制原理说起,mysql的主从复制都是单线程的操作,主 库对所有DDL和DML产生binlog,binlog是顺序写,所 ...
- tfs项目解绑及svn上传
1.tfs解绑 file--源代码管理——tfs解绑 2.svn将本地的文件夹上传到server 右击--import--url--新建文件夹
- mysql套接字文件
- Redis缓存穿透问题及解决方案
上周在工作中遇到了一个问题场景,即查询商品的配件信息时(商品:配件为1:N的关系),如若商品并未配置配件信息,则查数据库为空,且不会加入缓存,这就会导致,下次在查询同样商品的配件时,由于缓存未命中,则 ...