C# Android设备上的Unity2D rigidbody2D.velocity
我有一个2D游戏,当设备倾斜时,我需要在x轴上移动玩家的刚体。我下面的代码是我认为应该有效的,但它没有。这名球员一动不动。我做错了什么?为什么C# Android设备上的Unity2D rigidbody2D.velocity,c#,android,unity3d,game-physics,C#,Android,Unity3d,Game Physics,我有一个2D游戏,当设备倾斜时,我需要在x轴上移动玩家的刚体。我下面的代码是我认为应该有效的,但它没有。这名球员一动不动。我做错了什么?为什么 Vector3 dir = Vector3.zero; dir.x = Input.acceleration.y; if (dir.sqrMagnitude > 1) dir.Normalize(); dir *= Time.deltaTime; anim.S
Vector3 dir = Vector3.zero;
dir.x = Input.acceleration.y;
if (dir.sqrMagnitude > 1)
dir.Normalize();
dir *= Time.deltaTime;
anim.SetFloat("Speed", Mathf.Abs(dir.x));
rigidbody2D.velocity = new Vector2 (dir.x * tiltSpeed, rigidbody2D.velocity.y);
完整代码如下
using UnityEngine;
using System.Collections;
public class CharacterControllerScript : MonoBehaviour {
public float maxSpeed = 10f;
bool facingRight = true;
public float tiltSpeed = 10f;
Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void FixedUpdate () {
// Set animation
if ((Application.platform != RuntimePlatform.Android) || (Application.platform != RuntimePlatform.IPhonePlayer)){
float move = Input.GetAxis ("Horizontal");
anim.SetFloat("Speed", Mathf.Abs(move));
rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
// Decide what way animation moving
if (move > 0 && !facingRight)
Flip ();
else if (move < 0 && facingRight)
Flip ();
}
else {
Vector3 dir = Vector3.zero;
dir.x = Input.acceleration.y;
if (dir.sqrMagnitude > 1)
dir.Normalize();
dir *= Time.deltaTime;
anim.SetFloat("Speed", Mathf.Abs(dir.x));
rigidbody2D.velocity = new Vector2 (dir.x * tiltSpeed, rigidbody2D.velocity.y);
// Decide what way animation moving
if (move > 0 && !facingRight)
Flip ();
else if (move < 0 && facingRight)
Flip ();
}
}
void Flip () {
facingRight = !facingRight;
Vector3 thescale = transform.localScale;
thescale.x *= -1;
transform.localScale = thescale;
}
}
使用UnityEngine;
使用系统集合;
公共类CharacterControllerScript:MonoBehavior{
公共浮点数maxSpeed=10f;
bool facingRight=正确;
公众浮子倾斜速度=10f;
动画师;
//用于初始化
无效开始(){
anim=GetComponent();
}
//每帧调用一次更新
无效固定更新(){
//设置动画
if((Application.platform!=RuntimePlatform.Android)| |(Application.platform!=RuntimePlatform.IPhonePlayer)){
浮动移动=Input.GetAxis(“水平”);
动画设置浮动(“速度”,数学Abs(移动));
rigidbody2D.velocity=新矢量2(移动*最大速度,rigidbody2D.velocity.y);
//决定动画移动的方式
如果(移动>0&!朝向右侧)
翻转();
else if(移动<0&&朝向右侧)
翻转();
}
否则{
Vector3 dir=Vector3.0;
dir.x=输入加速度y;
如果(方向sqrMagnitude>1)
dir.Normalize();
dir*=Time.deltaTime;
动画设置浮动(“速度”,数学Abs(dir.x));
rigidbody2D.velocity=新矢量2(方向x*倾斜速度,rigidbody2D.velocity.y);
//决定动画移动的方式
如果(移动>0&!朝向右侧)
翻转();
else if(移动<0&&朝向右侧)
翻转();
}
}
无效翻转(){
facingRight=!facingRight;
Vector3 thescale=transform.localScale;
比例x*=-1;
transform.localScale=比例;
}
}
我希望这个答案对你仍然有用,因为你几个月前发了帖子
我认为您的代码将始终通过第一个if案例。你应该替换
(Application.platform != RuntimePlatform.Android) || (Application.platform != RuntimePlatform.IPhonePlayer)
借
因为它们不可能同时都是真的相同的代码,相同的问题,我真的不知道我做错了什么。
(Application.platform != RuntimePlatform.Android) && (Application.platform != RuntimePlatform.IPhonePlayer)