Unity反向动画无法正常播放
具体代码是usingUnityEngine;usingSystem.Collections;publicclassbox:MonoBehaviour{boolisPlay...
具体代码是
using UnityEngine;
using System.Collections;
public class box : MonoBehaviour {
bool isPlayAnim = false;
AnimationState aniani;
public bool isZhengOrFan;
// Use this for initialization
void Start () {
AnimationState aniani = animation["Take 001"];
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width/2,Screen.height/2,0));
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Debug.DrawLine(ray.origin, hit.point);
//print(hit.collider.gameObject.name);
//curObject = hit.collider.gameObject;
if (hit.collider.gameObject.name == gameObject.name)
{
isPlayAnim = true;
}
// 显示当前选中对象的名称
// print(hit.collider.gameObject);
}
}
if(isPlayAnim)
{
//animation.Play();
isZhengOrFan=isPositiveAndNegativePlay(gameObject,isZhengOrFan,aniani);
isPlayAnim = false;
}
}
public static box _initialise;
void Awake()
{
_initialise = this;
}
/// <summary>
/// 动画进行正反播放
/// </summary>
/// <param name="gameObject">需要播放动画的物体</param>
/// <param name="isZhengOrFan">动画的播放状态</param>
/// <param name="animation">动画状态</param>
/// <returns></returns>
public bool isPositiveAndNegativePlay(GameObject gameObject,bool isZhengOrFan,AnimationState animation)
{
//判断传入的动画的播放状态
//----传入的状态为true时,表示正常播放
//----传入的状态为false时,表示动画播放完,可以进行反向播放
if(!isZhengOrFan)
{
//当没有播放动画的时候,播放动画
if(!gameObject.animation.isPlaying)
{
gameObject.animation.Play();
//-----改变动画的播放状态-----
//动画的当前时间置为初始
animation.time = 0f;
//动画的播放速度为正常速度
animation.speed = 1.0f;
//改变传入的bool变量的值
isZhengOrFan = true;
}
}
else
{
//当没有播放动画的时候,播放动画
if (!gameObject.animation.isPlaying)
{
print("123");
gameObject.animation.Play();
//-----改变动画的播放状态-----
//动画的当前时间置为动画的时长
animation.time = animation.length;
//向后走,即动画进行反向播放,从终点向起点播放
animation.speed = -1.0f;
//改变传入的bool变量的值
isZhengOrFan = false;
print("123");
}
}
return isZhengOrFan;
}
}
挂载到box物体上时无法正常反向播放 报错
NullReferenceException: Object reference not set to an instance of an object
box.isPositiveAndNegativePlay (UnityEngine.GameObject gameObject, Boolean isZhengOrFan, UnityEngine.AnimationState animation) (at Assets/Standard Assets/doll of heart/box.cs:75)
box.Update () (at Assets/Standard Assets/doll of heart/box.cs:41)
求指导原因 展开
using UnityEngine;
using System.Collections;
public class box : MonoBehaviour {
bool isPlayAnim = false;
AnimationState aniani;
public bool isZhengOrFan;
// Use this for initialization
void Start () {
AnimationState aniani = animation["Take 001"];
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width/2,Screen.height/2,0));
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Debug.DrawLine(ray.origin, hit.point);
//print(hit.collider.gameObject.name);
//curObject = hit.collider.gameObject;
if (hit.collider.gameObject.name == gameObject.name)
{
isPlayAnim = true;
}
// 显示当前选中对象的名称
// print(hit.collider.gameObject);
}
}
if(isPlayAnim)
{
//animation.Play();
isZhengOrFan=isPositiveAndNegativePlay(gameObject,isZhengOrFan,aniani);
isPlayAnim = false;
}
}
public static box _initialise;
void Awake()
{
_initialise = this;
}
/// <summary>
/// 动画进行正反播放
/// </summary>
/// <param name="gameObject">需要播放动画的物体</param>
/// <param name="isZhengOrFan">动画的播放状态</param>
/// <param name="animation">动画状态</param>
/// <returns></returns>
public bool isPositiveAndNegativePlay(GameObject gameObject,bool isZhengOrFan,AnimationState animation)
{
//判断传入的动画的播放状态
//----传入的状态为true时,表示正常播放
//----传入的状态为false时,表示动画播放完,可以进行反向播放
if(!isZhengOrFan)
{
//当没有播放动画的时候,播放动画
if(!gameObject.animation.isPlaying)
{
gameObject.animation.Play();
//-----改变动画的播放状态-----
//动画的当前时间置为初始
animation.time = 0f;
//动画的播放速度为正常速度
animation.speed = 1.0f;
//改变传入的bool变量的值
isZhengOrFan = true;
}
}
else
{
//当没有播放动画的时候,播放动画
if (!gameObject.animation.isPlaying)
{
print("123");
gameObject.animation.Play();
//-----改变动画的播放状态-----
//动画的当前时间置为动画的时长
animation.time = animation.length;
//向后走,即动画进行反向播放,从终点向起点播放
animation.speed = -1.0f;
//改变传入的bool变量的值
isZhengOrFan = false;
print("123");
}
}
return isZhengOrFan;
}
}
挂载到box物体上时无法正常反向播放 报错
NullReferenceException: Object reference not set to an instance of an object
box.isPositiveAndNegativePlay (UnityEngine.GameObject gameObject, Boolean isZhengOrFan, UnityEngine.AnimationState animation) (at Assets/Standard Assets/doll of heart/box.cs:75)
box.Update () (at Assets/Standard Assets/doll of heart/box.cs:41)
求指导原因 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询