unity 3d 中的播放器控制器无法正确跳跃
player controller in unity 3d not jumping correctly
我正在制作一款带有重力切换机制的 3d 游戏。我让玩家在地图上四处走动,它适用于所有角度,但是跳跃一团糟。我想知道我怎样才能让跳跃成功。
目前的跳跃只是给局部向上向量加了一个值。我不知道如何解决它。我让它在某一时刻工作,但后来我不得不切换到物理运动而不是静态运动,现在它不起作用了。
代码如下:
void Update()
{
//think about moving
isRunning = (Input.GetKey(KeyCode.LeftControl));
isShifting = IntToBool((int)((BoolToInt(Input.GetKey(KeyCode.LeftAlt)))*BoolToInt(!isShiftable))+(BoolToInt(Input.GetKey(KeyCode.LeftShift))*BoolToInt(isShiftable)));
Vector3 forward = transform.forward;//transform.TransformDirection(Vector3.forward);
Vector3 right = transform.right;//transform.TransformDirection(Vector3.right);
if (((isShifting && isRunning)||!(isShifting && isRunning))) {
curSpeedX = walkingSpeed*Input.GetAxis("Vertical");
curSpeedY = walkingSpeed*Input.GetAxis("Horizontal");
}
if (isShifting) {
curSpeedX = ShiftSpeed*Input.GetAxis("Vertical");
curSpeedY = ShiftSpeed*Input.GetAxis("Horizontal");
}
if (isRunning) {
curSpeedX = runningSpeed*Input.GetAxis("Vertical");
curSpeedY = runningSpeed*Input.GetAxis("Horizontal");
}
//jump
if (!isGrounded) {
ypoa -= gravity;
}
if (Input.GetKeyDown(KeyCode.Space)) {
ypoa = jumpheight;
}
//actualy move
moveDir = (ypoa*transform.up)+(curSpeedX*transform.forward)+(curSpeedY*transform.right);
rb.velocity = (moveDir*speed);
moveDir = new Vector3(0,0,0);
}
void OnCollisionEnter() {
jumcount = 0;
ypoa = 0;
isGrounded = true;
}
void OnCollisionExit() {
isGrounded = false;
}
void OnCollisionStay() {
isGrounded = true;
}
我正在使用 unity 2019.4.28f 并在 windows 10
感谢帮助。
尝试将您的物理代码移至 FixedUpdate。如果这不起作用,请您详细说明一下,例如按 space 会发生什么?
void Update()
{
//think about moving
isRunning = (Input.GetKey(KeyCode.LeftControl));
isShifting = IntToBool((int)((BoolToInt(Input.GetKey(KeyCode.LeftAlt)))*BoolToInt(!isShiftable))+(BoolToInt(Input.GetKey(KeyCode.LeftShift))*BoolToInt(isShiftable)));
Vector3 forward = transform.forward;//transform.TransformDirection(Vector3.forward);
Vector3 right = transform.right;//transform.TransformDirection(Vector3.right);
if (((isShifting && isRunning)||!(isShifting && isRunning))) {
curSpeedX = walkingSpeed*Input.GetAxis("Vertical");
curSpeedY = walkingSpeed*Input.GetAxis("Horizontal");
}
if (isShifting) {
curSpeedX = ShiftSpeed*Input.GetAxis("Vertical");
curSpeedY = ShiftSpeed*Input.GetAxis("Horizontal");
}
if (isRunning) {
curSpeedX = runningSpeed*Input.GetAxis("Vertical");
curSpeedY = runningSpeed*Input.GetAxis("Horizontal");
}
//jump
if (!isGrounded) {
ypoa -= gravity;
}
if (Input.GetKeyDown(KeyCode.Space)) {
ypoa = jumpheight;
}
//actualy move
moveDir = (ypoa*transform.up)+(curSpeedX*transform.forward)+(curSpeedY*transform.right);
rb.velocity = (moveDir*speed);
moveDir = new Vector3(0,0,0);
}
void OnCollisionEnter() {
jumcount = 0;
ypoa = 0;
isGrounded = true;
}
void OnCollisionExit() {
isGrounded = false;
}
void OnCollisionStay() {
isGrounded = true;
}
我正在制作一款带有重力切换机制的 3d 游戏。我让玩家在地图上四处走动,它适用于所有角度,但是跳跃一团糟。我想知道我怎样才能让跳跃成功。
目前的跳跃只是给局部向上向量加了一个值。我不知道如何解决它。我让它在某一时刻工作,但后来我不得不切换到物理运动而不是静态运动,现在它不起作用了。
代码如下:
void Update()
{
//think about moving
isRunning = (Input.GetKey(KeyCode.LeftControl));
isShifting = IntToBool((int)((BoolToInt(Input.GetKey(KeyCode.LeftAlt)))*BoolToInt(!isShiftable))+(BoolToInt(Input.GetKey(KeyCode.LeftShift))*BoolToInt(isShiftable)));
Vector3 forward = transform.forward;//transform.TransformDirection(Vector3.forward);
Vector3 right = transform.right;//transform.TransformDirection(Vector3.right);
if (((isShifting && isRunning)||!(isShifting && isRunning))) {
curSpeedX = walkingSpeed*Input.GetAxis("Vertical");
curSpeedY = walkingSpeed*Input.GetAxis("Horizontal");
}
if (isShifting) {
curSpeedX = ShiftSpeed*Input.GetAxis("Vertical");
curSpeedY = ShiftSpeed*Input.GetAxis("Horizontal");
}
if (isRunning) {
curSpeedX = runningSpeed*Input.GetAxis("Vertical");
curSpeedY = runningSpeed*Input.GetAxis("Horizontal");
}
//jump
if (!isGrounded) {
ypoa -= gravity;
}
if (Input.GetKeyDown(KeyCode.Space)) {
ypoa = jumpheight;
}
//actualy move
moveDir = (ypoa*transform.up)+(curSpeedX*transform.forward)+(curSpeedY*transform.right);
rb.velocity = (moveDir*speed);
moveDir = new Vector3(0,0,0);
}
void OnCollisionEnter() {
jumcount = 0;
ypoa = 0;
isGrounded = true;
}
void OnCollisionExit() {
isGrounded = false;
}
void OnCollisionStay() {
isGrounded = true;
}
我正在使用 unity 2019.4.28f 并在 windows 10
感谢帮助。
尝试将您的物理代码移至 FixedUpdate。如果这不起作用,请您详细说明一下,例如按 space 会发生什么?
void Update()
{
//think about moving
isRunning = (Input.GetKey(KeyCode.LeftControl));
isShifting = IntToBool((int)((BoolToInt(Input.GetKey(KeyCode.LeftAlt)))*BoolToInt(!isShiftable))+(BoolToInt(Input.GetKey(KeyCode.LeftShift))*BoolToInt(isShiftable)));
Vector3 forward = transform.forward;//transform.TransformDirection(Vector3.forward);
Vector3 right = transform.right;//transform.TransformDirection(Vector3.right);
if (((isShifting && isRunning)||!(isShifting && isRunning))) {
curSpeedX = walkingSpeed*Input.GetAxis("Vertical");
curSpeedY = walkingSpeed*Input.GetAxis("Horizontal");
}
if (isShifting) {
curSpeedX = ShiftSpeed*Input.GetAxis("Vertical");
curSpeedY = ShiftSpeed*Input.GetAxis("Horizontal");
}
if (isRunning) {
curSpeedX = runningSpeed*Input.GetAxis("Vertical");
curSpeedY = runningSpeed*Input.GetAxis("Horizontal");
}
//jump
if (!isGrounded) {
ypoa -= gravity;
}
if (Input.GetKeyDown(KeyCode.Space)) {
ypoa = jumpheight;
}
//actualy move
moveDir = (ypoa*transform.up)+(curSpeedX*transform.forward)+(curSpeedY*transform.right);
rb.velocity = (moveDir*speed);
moveDir = new Vector3(0,0,0);
}
void OnCollisionEnter() {
jumcount = 0;
ypoa = 0;
isGrounded = true;
}
void OnCollisionExit() {
isGrounded = false;
}
void OnCollisionStay() {
isGrounded = true;
}