【发布时间】:2016-09-11 11:32:49
【问题描述】:
所以当space 或mousebutton 被按下时,我需要我的角色上升。我将 rigbody 2d 和 box colider 2d 添加到我的gameobject(玩家)。问题是当我点击space 或mousebutton时,它只会跳跃而不是不断向上移动。
这是我的代码:
using UnityEngine;
using System.Collections;
public class PixelMovement : MonoBehaviour {
Vector3 velocity = Vector3.zero;
public Vector3 PressVelocity;
public float maxSpeed = 5f;
public float fowardSpeed = 1f;
public float jumpForce;
bool didPress = false;
// Use this for initialization
void Start () {
}
//Do Graphic & Input updates
void Update()
{
if (Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0))
{
didPress = true;
}
}
//Do physics engine updates here
void FixedUpdate()
{
velocity.x = fowardSpeed;
if (didPress == true)
{
didPress = false;
velocity += PressVelocity * Time.deltaTime *jumpForce;
}
velocity = Vector3.ClampMagnitude(velocity, maxSpeed);
transform.position += velocity * Time.deltaTime;
}
}
【问题讨论】:
-
我已经尝试了您的代码并且在我的计算机上运行良好。 Here I left a link with the video。你有没有附加到这个游戏对象的其他脚本?
-
这是
2d不是3d -
我附加了
box colider 2d并取消选中is triggered,我还添加了rigbody2d -
您是否将 RigitBody2D 质量设置为零?
-
不,我使用自动质量,我解决了这个问题,但现在我遇到了重力问题。如果我使用重力刻度为 1,我的物体会立即粗暴地坠落,不像圆形......
标签: c# visual-studio unity3d unityscript unity3d-2dtools