球员坚持墙壁Unity

时间:2014-10-24 14:18:06

标签: unity3d controls isometric

我正在使用Unity中的下一个脚本来通过等距场景移动播放器。问题是,当我跳到墙上时,玩家会被卡在墙上,而每次我按下跳键都会跟着跳跃。

这是脚本:

#pragma strict
var speed = 2.0;
var jumpSpeed = 8.0;
var gravity = 20.0;

var smoothSpeed = 10.0;
var smoothDirection = 10.0;

private var moveDirection = Vector3.zero;
private var verticalSpeed = 0.0;
private var moveSpeed = 2.0;

private var grounded : boolean = false;
private var jumping : boolean = false;

private var targetAngle = 0.0;
var anim : Animator;



var dontJump = "false";

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

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

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);

    // Target direction relative to the camera
    var targetDirection = Input.GetAxis("Horizontal") * right + Input.GetAxis("Vertical") * forward;

    // 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)
    {
        moveDirection = Vector3.Lerp(moveDirection, targetDirection, smoothDirection * Time.deltaTime);
        moveDirection = moveDirection.normalized;
        if(grounded){
            anim.Play("walk",0);
        }
    }else{
        if(grounded){
            anim.StopPlayback();
            anim.Play("idle2",0);
        }
    }

    // Smooth the speed based on the current target direction
    var curSmooth = smoothSpeed * Time.deltaTime;
    // When in air we accelerate slower
    if (!grounded)
    {
        curSmooth *= 0.1;
    }

    moveSpeed = Mathf.Lerp(moveSpeed, targetDirection.magnitude * speed, curSmooth);
}

function Start(){
    anim = GetComponent("Animator") as Animator;
}

function Update() {

    UpdateSmoothedMovementDirection();

    if (grounded) {

        verticalSpeed = 0.0;

        // Jump    
        if (grounded && Input.GetButton ("Jump")) {
            verticalSpeed = jumpSpeed;
            jumping = true;
            var move : float = Input.GetAxis  ("Vertical");
            anim.Play("jump",0);
            SendMessage("DidJump", SendMessageOptions.DontRequireReceiver);
        }

    }
    // Apply gravity
    verticalSpeed -= gravity * Time.deltaTime;

    var movement = moveDirection * moveSpeed + Vector3 (0, verticalSpeed, 0);
    movement *= Time.deltaTime;

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

    grounded = (flags && CollisionFlags.CollidedBelow) != 0;

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

    // We are in jump mode but just became grounded
    if (grounded && jumping)
    {
        jumping = false;
        SendMessage("DidLand", SendMessageOptions.DontRequireReceiver);
    }


}

function GetSpeed () {
    return moveSpeed;
}

function IsJumping () {
    return jumping;
}

function GetDirection () {
    return moveDirection;
}

玩家是使用刚体和胶囊碰撞器的模型。

1 个答案:

答案 0 :(得分:0)

最后我解决了。 我已经声明了变量:

var character : CharacterController;

我已经取代了这个条件:

if (grounded) 

与那一个:

if (character.isGrounded)

在Start函数中我添加了:

character = GetComponent( CharacterController );

多数人。