【发布时间】:2019-10-20 07:45:29
【问题描述】:
遇到一个问题,即 2D 游戏(空间)我试图使用带有 addforce 的 RB2D 来移动玩家。看来我可以一次选择两个方向。如果我按下播放器会下降,但按下时它不会上升但会停止。与左/右相同。重力设置为 0。两个对象都有一个刚体 2d,我需要动态才能允许“弹跳”和游戏中的其他物理。
- 始终存在对象(移动脚本)
- 玩家精灵(精灵)
我希望玩家能够上下左右移动。我真的很满意它有减慢效果,但不是必需的。 (如果你松开按键,它会因摩擦而减速)
我的播放器设置为分层格式,因为这是多人游戏,并且父对象始终在游戏中。
我尝试了以下方法 - 互换动态+运动学两者或一个。 - 增加力、变换、速度等。 -调整摩擦、阻力等。 -检查调试 x/y 力正在游戏中应用,但玩家在第一次移动后不会向相反方向移动。
我已经把文件上传到github here.
玩家移动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class PlayerUnit : NetworkBehaviour {
//float speed = .5F;
float rotationSpeed = 50.0F;
float xMin;
float xMax;
float yMin;
float yMax;
// configuration parameters
[Header("Player")]
[SerializeField] float moveSpeed = .5f;
[SerializeField] float padding = 1f;
[SerializeField] int health = 200;
[SerializeField] AudioClip deathSound;
[SerializeField] [Range(0, 1)] float deathSoundVolume = 0.75f;
[SerializeField] AudioClip shootSound;
[SerializeField] [Range(0, 1)] float shootSoundVolume = 0.25f;
Rigidbody2D rb;
void Start () {
rb = this.GetComponent<Rigidbody2D>();
}
void Update () {
if( hasAuthority == false )
{
return;
}
if ( Input.GetKeyDown(KeyCode.Space) )
{
this.transform.Translate( 0, 1, 0 );
}
}
private void FixedUpdate()
{
if (true)
{
float leftright = Input.GetAxis("Horizontal");
float updown = Input.GetAxis("Vertical");
float xForce = leftright * moveSpeed * Time.deltaTime;
float yForce = updown * moveSpeed * Time.deltaTime;
Vector2 force = new Vector2(xForce, yForce);
rb.AddForce(force);
Debug.Log("xForce : " + xForce + " yForce : " + yForce);
//float leftright = Input.GetAxis("Horizontal") * moveSpeed;
//float updown = Input.GetAxis("Vertical") * moveSpeed;
////rb.MovePosition(rb.position + new Vector2(1, 0) * leftright);
//rb.MovePosition(transform.position + (transform.right * leftright + transform.up * updown) * moveSpeed);
//rb.MovePosition(rb.position + new Vector2(0, 1) * updown);
}
}
}
【问题讨论】:
标签: unity3d physics multiplayer