Unity 3D角色运动

时间:2015-11-24 10:59:32

标签: c# unity3d

我在Unity中创建了一个角色。我准备走路,转身和动画。我为这个角色创建了三个脚本。

我希望当我点击时,按钮我的角色有这个动画的动作。当我点击按钮字符时使用步行动画前进。我的向下,向左和向右按钮不起作用:

#pragma strict

internal var animator:Animator;

var v:float;
var h:float;
var sprint :float;

function Start ()
{

    animator=GetComponent(Animator);

}

function Update ()
{

    v=Input.GetAxis("Vertical");
    h=Input.GetAxis("Horizontal");
    //Sprinting();
}

function FixedUpdate()
{
    animator.SetFloat("walk",v);
    animator.SetFloat("Turn",h);
    animator.SetFloat("Sprint",sprint);
}

function Sprinting()
{
    if(Input.GetButton("Fire1"))
    {
        sprint=0.2;
    }
    else
    {
        sprint=0.0;
    }
}

function MoveUp() { v = 1.0; }
function MoveDown() { v = -1.0; }
function MoveLeft() { h = -1.0; }
function MoveRight() { h = 1.0; }
#pragma strict

//--- variables for button textures

var txLeftOff:Texture; 

var txLeftOn:Texture; 

var txRightOff:Texture; 

var txRightOn:Texture; 

var txUpOff:Texture;

var txUpOn:Texture;

var txDownOff:Texture;

var txDownOn:Texture;

var txCenter:Texture;

//--- variable for button skin & style

var mySkin:GUISkin; 

//--- variables to control main camera

var layermask:LayerMask;

var rotateSwipeSpeed:float = 4; 

var cameraHeight:float = 3;

var cameraDistance:float = 9;

static var cameraRoateSpeed:float = 4; // static for using at PlayerController.js script

//--- variable to get player object 

private var player:GameObject;

//--- touch swipe variables

private var viewY:float = 0;

private var viewX:float = 0;

private var invertViewY:int = 1; // 1 or -1 

private var invertviewX:int = 1; // 1 or -1 

private var minViewY:float = -5;

private var maxViewY:float = 8;

private var minViewX:float = -20;

private var maxViewX:float = 20;

//--- variables for calculating button positions. 

private var scrW:float; // screen width 

private var scrH:float; // screen height 

private var xgap:float;// gap from left side 

private var ygap:float;// gap from bottom side

private var bgap:float;// gap between buttons

private var w:float; // width of rect 

private var h:float; // height of rect

//--- variables for button rectangle size

private var rectLeft:Rect;

private var rectRight:Rect;

private var rectUp:Rect;

private var rectDown:Rect; 

private var rectCenter:Rect;

private var rectButtonArea:Rect;

//--- variables to check the clicked buttons

private var clickLeft:boolean;

private var clickRight:boolean;

private var clickUp:boolean;

private var clickDown:boolean;

function Start()
{ 
    player = GameObject.Find("Player");

    TouchNone();

    CameraFollow();
}

function LateUpdate()
{ 

    TouchNone();

    if(Input.touches.Length > 0)
    { 
        for(var i:int=0; i<Input.touchCount; i++)
        { 
            var touch:Vector2 = Vector2(Input.GetTouch(i).position.x,(Screen.height-Input.GetTouch(i).position.y)); 
            TouchButtonCheck(touch); 

            TouchRotate(touch, i);
        }

    } 

}

function OnGUI ()
{ 

    GUI.skin = mySkin; 

    GUI.color.a = 0.5f; 

    InitializeButtonPosition();

    DrawMoveButtonGUI(); 

} 

function TouchNone()
{ 

    clickUp = false; clickDown = false; clickLeft = false; clickRight = false; 

}

function TouchButtonCheck(touch:Vector2)
{ 

    // masking direction flag status ... 

    clickUp = clickUp || rectUp.Contains(touch); 

    clickDown = clickDown || rectDown.Contains(touch); 

    clickLeft = clickLeft || rectLeft.Contains(touch); 

    clickRight = clickRight || rectRight.Contains(touch); 

    // send event to player script ...

    if(clickUp) { player.SendMessage("MoveUp"); }

    if(clickDown) { player.SendMessage("MoveDown"); }

    if(clickLeft) { player.SendMessage("MoveLeft"); }

    if(clickRight) { player.SendMessage("MoveRight"); }

    CameraFollow(); 

}

function TouchRotate(touch:Vector2, i:int)
{

    if(!rectLeft.Contains(touch) && !rectRight.Contains(touch) && !rectUp.Contains(touch) && !rectDown.Contains(touch))
   { 

        if(!rectButtonArea.Contains(touch))
        { 

            viewX += Input.GetTouch(i).deltaPosition.x * rotateSwipeSpeed * Time.deltaTime *invertviewX * cameraRoateSpeed;
            viewY -= Input.GetTouch(i).deltaPosition.y * rotateSwipeSpeed * Time.deltaTime *invertViewY; 
            CameraFollow();

        }

    }

}

function CameraFollow()
{ 

    viewY = Mathf.Clamp(viewY, minViewY, maxViewY); 

    //--- viewX = Mathf.Clamp(viewX, minViewX, maxViewX);

    player.SendMessage("MoveRotation", Vector3(viewY, viewX, 0.0f));

    var rot = Quaternion.Euler(viewY, viewX*cameraRoateSpeed, 0.0f);

    Camera.main.transform.rotation = rot;

    Camera.main.transform.position = rot * Vector3(0, cameraHeight, -cameraDistance) + player.transform.position;

    //--- Debug.DrawLine(player.transform.position, Camera.main.transform.position, Color.green, 1, false);

    var hit : RaycastHit;

    if(Physics.Linecast(player.transform.position, Camera.main.transform.position, hit, layermask))
    { 

        var tempDistance = Vector3.Distance(player.transform.position, hit.point); 

        Camera.main.transform.position = rot * Vector3(0, cameraHeight, -tempDistance) + 

player.transform.position; 

    }

} 

function InitializeButtonPosition()
{ 

    scrW = Screen.width;

    scrH = Screen.height;

    xgap = scrW / 70;

    ygap = scrH / 20;

    bgap = scrH / 80;

    w = h = scrW / 15;

    rectLeft = Rect(xgap, scrH-ygap-bgap-(h*2), w, h);

    rectRight = Rect(xgap+(bgap*2)+(w*2), scrH-ygap-bgap-(h*2), w, h);

    rectUp = Rect(xgap+w+bgap, scrH-ygap-(bgap*2)-(h*3), w, h);

    rectDown = Rect(xgap+w+bgap, scrH-ygap-h, w, h);

    rectCenter = Rect(xgap+w+bgap, scrH-ygap-bgap-(h*2), w, h);

    rectButtonArea = Rect(0, scrH-ygap-(bgap*3)-(h*3), w*3+xgap+(bgap*3), h*3+ygap+(bgap*3));

}

function DrawMoveButtonGUI()
{

    GUI.Button(rectLeft, ""); GUI.Button(rectRight, ""); 

    GUI.Button(rectUp, ""); GUI.Button(rectDown, ""); 

    GUI.Button(rectCenter, txCenter);

    // left button clicked

    if(clickLeft)
    {
        GUI.DrawTexture(rectLeft, txLeftOn, ScaleMode.StretchToFill, false, 0);
    } 

    else
    {
        GUI.DrawTexture(rectLeft, txLeftOff, ScaleMode.StretchToFill, false, 0);
    }

    // right button clicked 

    if(clickRight)
    {
       GUI.DrawTexture(rectRight, txRightOn, ScaleMode.StretchToFill, false, 0);
    } 

    else
    {
        GUI.DrawTexture(rectRight, txRightOff, ScaleMode.StretchToFill, false, 0);
    }

    // up button clicked 

    if(clickUp)
    {
        GUI.DrawTexture(rectUp, txUpOn, ScaleMode.StretchToFill, false, 0);
    } 

    else
    {
        GUI.DrawTexture(rectUp, txUpOff, ScaleMode.StretchToFill, false, 0);
    }

    // down button cliked 

    if(clickDown)
    {
        GUI.DrawTexture(rectDown, txDownOn, ScaleMode.StretchToFill, false, 0);
    }

    else
    {
        GUI.DrawTexture(rectDown, txDownOff, ScaleMode.StretchToFill, false, 0);
    } 

    //--- GUI.Button(rectButtonArea, "");

}
#pragma strict

var walkSpeed:float = 9;

private var Controller:CharacterController;

private var motion:Vector3;//motion=hareket

function Start()
{ 

    Controller = GetComponent(CharacterController);

}

function Update()
{ 

    if(Input.GetKeyDown(KeyCode.Escape))
    { 
        Application.Quit();
    }

}

function MoveLeft()
{

    motion = transform.TransformDirection(Vector3.left) * walkSpeed * Time.deltaTime; 

    Controller.Move(motion);

}

function MoveRight()
{

    motion = transform.TransformDirection(Vector3.left) * walkSpeed * Time.deltaTime*-1;

    Controller.Move(motion);

}

function MoveUp()
{ 

    motion = transform.TransformDirection(Vector3.forward) * walkSpeed * Time.deltaTime;

    Controller.Move(motion);

}

function MoveDown()
{

    motion = transform.TransformDirection(Vector3.forward) * walkSpeed * Time.deltaTime*-1;

    Controller.Move(motion);

}

function MoveRotation(angle:Vector3)
{ 

    transform.rotation = Quaternion.Euler(0, angle.y * ButtonController.cameraRoateSpeed, 0); 

}

1 个答案:

答案 0 :(得分:1)

您可能想使用动画师的Blendtree并使用方向和速度变量来定义要播放的动画。 这个Unity教程将帮助您: https://unity3d.com/learn/tutorials/modules/beginner/animation/blend-trees?playlist=17099

祝你好运;)