【问题标题】:Movements In Unity GameUnity 游戏中的动作
【发布时间】:2015-08-04 16:03:54
【问题描述】:

我有下面的 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
    }

【问题讨论】:

    标签: c# android input unity3d touch


    【解决方案1】:

    您可能希望将倾斜控件放入更新中。您应该主要使用固定更新来构建物理系统 IE 操纵刚体。

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

    【讨论】:

    • 以下代码已添加到 FixedUpdate 函数中:Vector3 dir = Vector3.zero; if (Input.deviceOrientation == DeviceOrientation.Portrait) { dir.x = -Input.acceleration.y; if(dir.sqrMagnitude &gt; 1) { dir.Normalize(); } dir *= Time.deltaTime; transform.Translate(dir * meshMoveSpeed); } 其作用是……用户将设备向他们倾斜,chartacer 向右移动但不能向左移动。 ..我需要向左倾斜以向左移动,向右倾斜以向右移动,因为应该将设备向玩家倾斜...有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    相关资源
    最近更新 更多