【问题标题】:Unity jumping fails while going against a wall靠墙时 Unity 跳跃失败
【发布时间】:2017-12-12 16:27:58
【问题描述】:

好的,我试图让我的对象(玩家)跳跃一切都可以,直到我撞到墙并继续撞墙(仍然 W 向下)如果我停止行走,我不能跳我撞到墙上,他将被启用为了跳跃,我尝试让墙壁触碰以使玩家的速度 = 零,但它不起作用, 我试图在墙壁上添加刚体并将它们冻结在适当的位置,试图使它们运动学也不起作用。

我希望我能靠在墙上并继续靠着它们走,以便能够跳跃。 如果你知道我该怎么做,请分享谢谢。

这是移动脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveScript : MonoBehaviour {

private float speed;
private float jumpHight;
private float straffeSpeed;


private float fallMultiplier;
private Rigidbody rig;
private Collider coll;


// Use this for initialization
private void Awake()
{
    rig = GetComponent<Rigidbody>();
    coll = GetComponent<Collider>();
    straffeSpeed = 1.5f;
    fallMultiplier = 2.5f;
    speed = 10f;
    jumpHight = 4f;

}
void Start () {
    GroundCheck();
}

// Update is called once per frame
void Update () {
    Move();
    GroundCheck();
    BetterFall();

}
private void Move()
{
    float hAxis = Input.GetAxis("Horizontal") * straffeSpeed;
    float vAxis = Input.GetAxis("Vertical");
    Vector3 movement = new Vector3(hAxis, 0, vAxis) * speed * Time.deltaTime;
    rig.MovePosition(transform.position + movement);

    if (Input.GetKey(KeyCode.Space) && GroundCheck())
    {
        rig.velocity = Vector3.up * jumpHight;



    }
}
private bool GroundCheck()
{
    return Physics.Raycast(transform.position, -Vector3.up, coll.bounds.extents.y + 0.2f);
}

private void BetterFall()
{
    if(rig.velocity.y < 0)
    {
        rig.velocity += Vector3.up * Physics.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
    }
}

【问题讨论】:

  • 根据墙体材料/物理特性,您可能会遇到摩擦。如果你靠在墙上并试图从墙上跳下来,摩擦力可能会阻止你。根据您制作的游戏类型,您的关卡对象不应引起摩擦。

标签: c# unity3d


【解决方案1】:
if (Input.GetKeyDown(KeyCode.Space) && GroundCheck())
{
    rig.velocity = Vector3.up * jumpHight;
}

我认为你这样做不太对。试试这个:

if (Input.GetKeyDown(KeyCode.Space) && GroundCheck())
{
    rig.AddForce(Vector3.up * jumpHight, ForceMode.Impulse);
}

:-)

【讨论】:

  • 没关系,我试过了,即使我把跳跃高度改到最低,它也会爆炸,我只是要使用角色控制器:D
  • 糟糕,我想我明白为什么了!您应该使用GetKeyDown 而不是GetKey。这样它只会跳跃一次,而不是在按住空格键的同时连续跳跃。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多