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的访问: ...
随机推荐
- SpringBoot @Transactional声明事务无效问题
查看系统支持的存储引擎:show engines; 查看表使用的引擎:show table status from db_name where name='table_name'; 修改表引擎方法: ...
- python 基础 字典
字典操作 字典一种key - value 的数据类型 特性: 无顺序 去重 查询速度快,比列表快多了 比list占用内存多 语法: info = { 'abc001': "Ben" ...
- 006-springboot2.0.4 配置log4j2,以及打印mybatis的sql
一.pom配置 普通项目 <!-- log4j2 --> <dependency> <groupId>org.apache.logging.log4j</gr ...
- Unity 补充安装
当需要下载 安装Unity之时没勾选的一些组件时, 1.去Unity官网点开Unity旧版本 2.找到你的Unity版本,然后只要下载Unity安装程序 3.点开安装程序,去掉已安装组件的勾选,勾选你 ...
- [LeetCode] 161. One Edit Distance_Medium
Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 pos ...
- Locust性能测试4-参数关联
前言 前面[Locust性能测试2-先登录场景案例]讲了登录的案例,这种是直接传账号和密码就能登录了,有些登录的网站会复杂一点, 需要先从页面上动态获取参数,作为登录接口的请求参数,如[学信网:htt ...
- Selenium Webdriver——操作隐藏的元素(二)display属性
有时候我们会碰到一些元素不可见,这个时候selenium就无法对这些元素进行操作了.例如,下面的情况: 页面主要通过“display:none”来控制整个下拉框不可见.这个时候如果直接操作这个下拉框, ...
- HTML 显示/隐藏DIV的技巧(visibility与display的差别)
参考链接:http://blog.csdn.net/szwangdf/article/details/1548807 div的visibility可以控制div的显示和隐藏,但是隐藏后页面显示空白: ...
- c++虚函数实现机制(转)
前言 C++中的虚函数的作用主要是实现了多态的机制.关于多态,简而言之就是用父类型别的指针指向其子类的实例,然后通过父类的指针调用实际子类的成员函数.这种技术可以让父类的指针有“多种形态”,这是一种泛 ...
- Qt addStretch()详解
addStretch函数,是在布局的时候用到. 函数原型: void QBoxLayout::addStretch ( int stretch = 0 ) 作用:平均分配Layout 比如: QVBo ...