矢量 2 速度在 Unity 2018.1 中不起作用



我正在制作一个安卓游戏,除了jump()之外,我所有的运动机制都运行良好。

我的脚本:

范德勒.cs

public class VJHandler : MonoBehaviour,IDragHandler, IPointerUpHandler, IPointerDownHandler {
private Image jsContainer;
private Image joystick;
public Vector3 InputDirection ;
void Start(){
jsContainer = GetComponent<Image>();
joystick = transform.GetChild(0).GetComponent<Image>(); //this command is used because there is only one child in hierarchy
InputDirection = Vector3.zero;
}
public void OnDrag(PointerEventData ped){
Vector2 position = Vector2.zero;
//To get InputDirection
RectTransformUtility.ScreenPointToLocalPointInRectangle
(jsContainer.rectTransform, 
ped.position,
ped.pressEventCamera,
out position);
position.x = (position.x/jsContainer.rectTransform.sizeDelta.x);
position.y = (position.y/jsContainer.rectTransform.sizeDelta.y);
float x = (jsContainer.rectTransform.pivot.x == 1f) ? position.x *2 + 1 : position.x *2 - 1;
float y = (jsContainer.rectTransform.pivot.y == 1f) ? position.y *2 + 1 : position.y *2 - 1;
InputDirection = new Vector3 (x,y,0);
InputDirection = (InputDirection.magnitude > 1) ? InputDirection.normalized : InputDirection;
//to define the area in which joystick can move around
joystick.rectTransform.anchoredPosition = new Vector3 (InputDirection.x * (jsContainer.rectTransform.sizeDelta.x/3)
,InputDirection.y * (jsContainer.rectTransform.sizeDelta.y)/3);
}
public void OnPointerDown(PointerEventData ped){
OnDrag(ped);
}
public void OnPointerUp(PointerEventData ped){
InputDirection = Vector3.zero;
joystick.rectTransform.anchoredPosition = Vector3.zero;
}
}

移动播放器.cs

public class MovePlayers : MonoBehaviour
{
public float moveSpeed = 05f;
public float jumpForce = 3f;
public VJHandler jsMovement;
public bool isGrounded = false;
public Vector3 direction;
[SerializeField]
private float xMin, xMax, yMin, yMax;
private Rigidbody2D rb;
void Update()
{
direction = jsMovement.InputDirection; //InputDirection can be used as per the need of your project
rb.velocity = new Vector2(direction.x * moveSpeed, 0.0f);

}
void Start()
{
//Initialization of boundaries
xMax = Screen.width - 50; // I used 50 because the size of player is 100*100
xMin = 50;
yMax = Screen.height - 50;
yMin = 50;
rb = GetComponent<Rigidbody2D>();
}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.CompareTag("Ground"))
{
isGrounded = true;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if(collision.CompareTag("Ground"))
{
isGrounded = false;
}
}
}

游戏管理器.cs

public class GameManager : MonoBehaviour {
public Rigidbody2D player;
[SerializeField]
private MovePlayers movePlayers;
void Awake()
{
movePlayers = player.GetComponent<MovePlayers>();
}
public void Jump()
{
if(movePlayers.isGrounded)
{
Debug.Log("Jumping!");
player.velocity = new Vector2(movePlayers.direction.x, movePlayers.jumpForce);
}
}
}

这些是我的脚本。我添加了一个debug.log来检查按钮是否正常工作,具有讽刺意味的是它工作正常,所以我相信有些东西是Vector2()错误.我使用Unity Remote,所以首先我猜它是仅限移动设备的问题,但它也发生在PC中。我不知道怎么了,我什至试图改变jumpFore o 300f但没有运气。Physics.gravity设置为 10(估计实际重力 9.8(。

MovePlayers.cs 不应该是:

void Update()
{
direction = jsMovement.InputDirection; //InputDirection can be used as per the need of your project
rb.velocity = new Vector2(direction.x * moveSpeed, direction.y * moveSpeed);
}

甚至,简单地rb.velocity = direction * moveSpeed;

另一点可能不正确,是GameManager正在修改播放器RigidBody,您的MovePlayers类也是如此。您的 RigidBody.velocity 被修改了两次,其值可能相互冲突。

最新更新