8

我的问题是没有显示我在 Unity3D 的第三人称控制器中设置的自定义步行动画。

动画是从具有 model@walk.fbx 结构的 FBX 文件导入的。我知道动画有效,因为如果我将它用作它显示的空闲动画。另一方面,它似乎也不是控制器脚本的问题,因为它适用于原型角色。

似乎正在播放的动画是动画组件中选择的动画(选择了“自动播放”)。任何从第三人称控制器更改动画的尝试都适用于原型角色,但不适用于我的。

我没有也不想要跑步和跳跃动画。使用原型角色,我在每个角色上都设置了步行动画,没有任何不利影响。控制器不会以这种方式关闭动画,因为控制台中没有来自第三人称控制器脚本的日志条目。与 CrossFade 通话相关的线路正在被调用。

关于我接下来可以看哪里的任何线索?更可能是控制器问题还是动画问题?完全是别的东西吗?

更新:下面是我的控制器的代码。当我使用 Unity 提供的建筑工人样本模型时,它工作正常。_animation.CrossFade与在预期的时间被调用的线路。使用PlayorBlend没有帮助。控制台中没有记录错误。

但是对于我们的自定义动画,它不起作用。我现在怀疑问题出在模型上。不幸的是,我无权分享该模型的样本。我已经向动画师询问了有关他如何创建 FBX 导出的更多详细信息。他需要使用任何特定设置来使模型在 Unity 中工作吗?如果我将它们单独添加到场景中,动画确实可以工作,这仍然很奇怪。

// Require a character controller to be attached to the same game object
@script RequireComponent(CharacterController)

public var idleAnimation : AnimationClip;
public var walkAnimation : AnimationClip;

public var walkMaxAnimationSpeed : float = 0.75;

private var _animation : Animation;

enum CharacterState {
    Idle = 0,
    Walking = 1,
}

private var _characterState : CharacterState;

// The speed when walking
var walkSpeed = 2.0;
var speedSmoothing = 10.0;
var rotateSpeed = 500.0;

var targetPrecision = 5;
var targetMaxDistance = 200;

// The camera doesnt start following the target immediately but waits for a split second to avoid too much waving around.
private var lockCameraTimer = 0.0;

// The current move direction in x-z
private var moveDirection = Vector3.zero;
// The current x-z move speed
private var moveSpeed = 0.0;

// The last collision flags returned from controller.Move
private var collisionFlags : CollisionFlags; 

// Are we moving backwards (This locks the camera to not do a 180 degree spin)
private var movingBack = false;
// Is the user pressing any keys?
private var isMoving = false;

private var isControllable = true;

private var isTargetting : boolean = false;
private var targetPoint : Vector3 = Vector3.zero;

function Awake () {
    moveDirection = transform.TransformDirection(Vector3.forward);

    _animation = GetComponent(Animation);
    if(!_animation)
        Debug.Log("The character you would like to control doesn't have animations. Moving her might look weird.");

    if(!idleAnimation) {
        _animation = null;
        Debug.Log("No idle animation found. Turning off animations.");
    }
    //_animation[idleAnimation.name] = idleAnimation;

    if(!walkAnimation) {
        _animation = null;
        Debug.Log("No walk animation found. Turning off animations.");
    }
    //_animation[walkAnimation.name] = walkAnimation;
}

function UpdateSmoothedMovementDirection () {
    var cameraTransform = Camera.main.transform;

    // Forward vector relative to the camera along the x-z plane    
    var forward = cameraTransform.TransformDirection(Vector3.forward);
    forward.y = 0;
    forward = forward.normalized;

    // Right vector relative to the camera
    // Always orthogonal to the forward vector
    var right = Vector3(forward.z, 0, -forward.x);

    var v = Input.GetAxisRaw("Vertical");
    var h = Input.GetAxisRaw("Horizontal");

    // Are we moving backwards or looking backwards
    if (v < -0.2)
        movingBack = true;
    else
        movingBack = false;

    var wasMoving = isMoving;
    isMoving = Mathf.Abs (h) > 0.1 || Mathf.Abs (v) > 0.1;

    // Target direction relative to the camera
    var targetDirection = h * right + v * forward;

    // Lock camera for short period when transitioning moving & standing still
    lockCameraTimer += Time.deltaTime;
    if (isMoving != wasMoving)
        lockCameraTimer = 0.0;

    // We store speed and direction seperately,
    // so that when the character stands still we still have a valid forward direction
    // moveDirection is always normalized, and we only update it if there is user input.
    if (targetDirection != Vector3.zero) {
        // If we are really slow, just snap to the target direction
        if (moveSpeed < walkSpeed * 0.9) {
            moveDirection = targetDirection.normalized;
        }
        // Otherwise smoothly turn towards it
        else {
            moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
            moveDirection = moveDirection.normalized;
        }
    }

    // Smooth the speed based on the current target direction
    var curSmooth = speedSmoothing * Time.deltaTime;

    // Choose target speed
    //* We want to support analog input but make sure you cant walk faster diagonally than just forward or sideways
    var targetSpeed = Mathf.Min(targetDirection.magnitude, 1.0);

    _characterState = CharacterState.Idle;

    // Pick speed modifier
    targetSpeed *= walkSpeed;
    _characterState = CharacterState.Walking;

    moveSpeed = Mathf.Lerp(moveSpeed, targetSpeed, curSmooth);
}


function UpdateTargettedMovementDirection () {
    var cameraTransform = Camera.main.transform;

    var wasMoving = isMoving;
    isMoving = true;//Mathf.Abs (h) > 0.1 || Mathf.Abs (v) > 0.1;

    // Target direction relative to the camera
    // var targetDirection = h * right + v * forward;
    var targetDirection = Vector3.zero;
    targetDirection.x =  targetPoint.x - transform.position.x;
    targetDirection.z =  targetPoint.z - transform.position.z;
    targetDirection = targetDirection.normalized;
    //Debug.Log("Target direction is " + targetDirection);

    // Lock camera for short period when transitioning moving & standing still
    lockCameraTimer += Time.deltaTime;
    if (isMoving != wasMoving)
        lockCameraTimer = 0.0;

    // We store speed and direction seperately,
    // so that when the character stands still we still have a valid forward direction
    // moveDirection is always normalized, and we only update it if there is user input.
    if (targetDirection != Vector3.zero) {
        // If we are really slow, just snap to the target direction
        if (moveSpeed < walkSpeed * 0.9) {
            moveDirection = targetDirection.normalized;
        }
        // Otherwise smoothly turn towards it
        else {
            moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
            moveDirection = moveDirection.normalized;
        }
    }

    // Smooth the speed based on the current target direction
    var curSmooth = speedSmoothing * Time.deltaTime;

    // Choose target speed
    //* We want to support analog input but make sure you cant walk faster diagonally than just forward or sideways
    var targetSpeed = Mathf.Min(targetDirection.magnitude, 1.0);

    _characterState = CharacterState.Idle;

    // Pick speed modifier
    targetSpeed *= walkSpeed;
    _characterState = CharacterState.Walking;

    moveSpeed = Mathf.Lerp(moveSpeed, targetSpeed, curSmooth);      
}

function Update() {
    if (!isControllable) {
        // kill all inputs if not controllable.
        Input.ResetInputAxes();
    }

    var distance : float = 0;
    if (Input.GetMouseButtonUp(0)) {
        if (isTargetting) {
            isTargetting = false;
            // Debug.Log("Stopped moving");
            FaceCamera();
        } else {
            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            var layerMask = 1 << 8; // Terrain is layer 8
            var hit : RaycastHit;
            Physics.Raycast(Camera.main.transform.position, ray.direction, hit, 1000, layerMask);
            distance = Vector3.Distance(transform.position, hit.point);
            if (distance <= targetMaxDistance && hit.point != Vector3.zero) {
                targetPoint = hit.point;
                isTargetting = true;
                // Debug.Log("Mouse up at hit " + hit.point + " at distance " + distance);
            } else {
                isTargetting = false;
                // Debug.Log("Ignored mouse up at hit " + hit.point + " at distance " + distance);
            }
        }
    }

    if (isTargetting) {
        // Debug.Log("Moving to " + targetPoint);
        distance = Vector3.Distance(transform.position, targetPoint);
        if (distance < targetPrecision) {
            // Debug.Log("Reached point " + targetPoint + " at distance " + distance);
            isTargetting = false;
            FaceCamera();
        } else {
            UpdateTargettedMovementDirection();
        }
    } else {
        UpdateSmoothedMovementDirection();
    }

    // Calculate actual motion
    var movement = moveDirection * moveSpeed;
    movement *= Time.deltaTime;

    // Move the controller
    var controller : CharacterController = GetComponent(CharacterController);
    collisionFlags = controller.Move(movement);

    // ANIMATION sector
    if (_animation) {       
        if (controller.velocity.sqrMagnitude < 0.1) {
            _animation.CrossFade(idleAnimation.name);
        } else {
            //_animation[walkAnimation.name].speed = Mathf.Clamp(controller.velocity.magnitude, 0.0, walkMaxAnimationSpeed);
            _animation.CrossFade(walkAnimation.name);       
        }
    } else {
         Debug.Log("Animation is null!");
    }
    // ANIMATION sector

    // Set rotation to the move direction
    transform.rotation = Quaternion.LookRotation(moveDirection);
}

function OnControllerColliderHit (hit : ControllerColliderHit ) {
//  Debug.DrawRay(hit.point, hit.normal);
    if (hit.moveDirection.y > 0.01) 
        return;
}

function GetSpeed () {
    return moveSpeed;
}

function GetDirection () {
    return moveDirection;
}

function IsMovingBackwards () {
    return movingBack;
}

function GetLockCameraTimer () {
    return lockCameraTimer;
}

function IsMoving () : boolean {
    return Mathf.Abs(Input.GetAxisRaw("Vertical")) + Mathf.Abs(Input.GetAxisRaw("Horizontal")) > 0.5;
}

function Reset () {
    gameObject.tag = "Player";
}

function FaceCamera() {
    var cameraTransform = Camera.main.transform;

    // Forward vector relative to the camera along the x-z plane    
    var forward = cameraTransform.TransformDirection(Vector3.forward);
    forward.y = 0;
    forward = forward.normalized;

    moveDirection = -forward;
}

更新 2:用于创建动画的设置在这些屏幕截图中。他们是正确的吗? 动画导出设置 1 动画导出设置 2

4

2 回答 2

1

不是(还 :-) 一个答案,但我需要更多的空间和图像。

自 3.5 Unity3d 以来,基于 animation@model.fbx 表示法处理导入的方式发生了变化,一些人报告了问题(例如BIG Unity 3.5.0f1 与 Skinned Rig 的问题 - Major FAIL)。当第二根根骨骼开始播放时会出现问题,但我可以通过将动画拖动到模型文件预制件来解决这个问题(我的大部分动画都包含在模型中,剩下的 2 个没什么大不了的)。

只是为了确定我在评论部分正确理解了您的答案,您有以下内容:

在此处输入图像描述

这意味着:

  • 在角色模型中,动画数组包含所有动画
  • 骨骼数量和所有名称与原型角色完全相同
  • 如果打开动画视图,您可以看到在层次视图中选择的角色的所有动画的只读列表

假设这很好,还有一些建议:

  • 我在代码中看不到设置WrapMode.Loop或动画速度的任何地方。您确定它在检查器中配置为循环并且没有在某处被覆盖。
  • 无参数的 CrossFade 假定为 0.3 秒
  • 交叉淡入淡出调用后会发生什么?DefaulTake 是否停止播放?
  • 可以默认拖动 Walk 动画吗
  • 你使用不同的动画层吗?
  • 设置时得到什么输出Debug.Log ("Walk: " + player.animation.IsPlaying ("Walk"));

[更新]

  1. 让我们看看导入设置。您是否检查了拆分动画,是否在开始结束 时输入了错误的值,例如 1-1?

    动画导入设置

  2. 关于IsPlaying 的切换:请创建更多关于AnimationState属性的输出。最值得注意的速度、长度、启用、重量……我怀疑动画太短或播放太快。
  3. 没有骨骼动画的行走动画听起来有点奇怪。另一方面,如果骨骼名称不匹配,控制台会大声喊叫。所以我认为现在没有问题。
于 2012-05-31T16:54:30.027 回答
0

好奇怪,能发下源码和demo吗?

您可以尝试检查:

  1. 检查动画名称和属性是否在编辑器中正确设置。
  2. 创建一个没有控制器的对象并确保正确导入了动画。
于 2012-05-06T01:03:52.490 回答