运动在统一的游戏

本文关键字:游戏 运动 | 更新日期: 2023-09-27 17:50:34

我有下面的c#脚本,它与Unity中的动画器系统并行工作。用户按左右方向键向左或向右移动。我想把这个放到设备上。为了做到这一点,我尝试使用新的unity4.6+ UI系统,在屏幕底部使用按钮,但无法让它工作。我认为最好的选择是使用倾斜或滑动,最好是倾斜。谁能看看那个脚本,并指出我在正确的方向倾斜功能代码将被放置?非常感谢你的帮助。

using UnityEngine;
using System.Collections;
// Require these components when using this script
[RequireComponent(typeof (Animator))]
[RequireComponent(typeof (CapsuleCollider))]
[RequireComponent(typeof (Rigidbody))]
public class PlayerControl_S : MonoBehaviour
{
    [System.NonSerialized]                  
    public float meshMoveSpeed = 4.0f;
[System.NonSerialized]
public float animSpeed = 1.5f;              // a public setting for overall animator animation speed
private Animator anim;                          // a reference to the animator on the character
private AnimatorStateInfo currentBaseState;         // a reference to the current state of the animator, used for base layer
private AnimatorStateInfo layer2CurrentState;   // a reference to the current state of the animator, used for layer 2
static int reloadState = Animator.StringToHash("Layer2.Reload");                // and are used to check state for various actions to occur
static int switchWeaponState = Animator.StringToHash("Layer2.WeaponSwap");

void Start ()
{
    // initialising reference variables
    anim = GetComponent<Animator>();                                        
    if(anim.layerCount ==2)
        anim.SetLayerWeight(1, 1);
}

/*void OnAnimatorMove() //Tells Unity that root motion is handled by the script
{
    if(anim)
    {
        Vector3 newPosition = transform.position;
        newPosition.z += anim.GetFloat("Speed")* meshMoveSpeed * Time.deltaTime;
        newPosition.x += anim.GetFloat("Direction") * meshMoveSpeed * Time.deltaTime;
        transform.position = newPosition;
    }
} */
void OnAnimatorMove() //Tells Unity that root motion is handled by the script
{
    if(anim)
    {
        Vector3 newPosition = transform.position;
        newPosition.z += anim.GetFloat("Speed")* meshMoveSpeed * Time.deltaTime;
        newPosition.x += anim.GetFloat("Direction") * meshMoveSpeed * Time.deltaTime;
        transform.position = newPosition;
    }
} 
void FixedUpdate ()
{
    float h = Input.GetAxis("Horizontal");              // setup h variable as our horizontal input axis
    float v = Input.GetAxis("Vertical");                // setup v variables as our vertical input axis
    anim.SetFloat("Speed", 1f);                         // set our animator's float parameter 'Speed' equal to the vertical input axis              
    anim.SetFloat("Direction", h);                      // set our animator's float parameter 'Direction' equal to the horizontal input axis        
    anim.speed = animSpeed;                             // set the speed of our animator to the public variable 'animSpeed'
    //anim.SetLookAtWeight(lookWeight);                 // set the Look At Weight - amount to use look at IK vs using the head's animation
    currentBaseState = anim.GetCurrentAnimatorStateInfo(0); // set our currentState variable to the current state of the Base Layer (0) of animation
    //Controls the movement speed
    if(v <= 0.0f)
    {
        meshMoveSpeed = 4;  
    }
    else
    {
        meshMoveSpeed = 6;
    }
    if(anim.layerCount ==2)
    {
        layer2CurrentState = anim.GetCurrentAnimatorStateInfo(1);   // set our layer2CurrentState variable to the current state of the second Layer (1) of animation
    }

运动在统一的游戏

你会想要在Update中放置倾斜控件。你应该主要使用修复更新在物理系统IE操纵刚体的构建。

现在你的代码只寻找移动速度的垂直轴变化。你可以在操纵杆设置中查看GetAxis处理的内容