我正在制作一款安卓游戏,除了jump()
之外,我的整个运动机制都运行良好。
我的剧本:
VHandler.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;
}
}
MovePlayers.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;
}
}
}
GameManager.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)。
答案 0 :(得分:2)
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被修改两次,可能存在冲突值。