【问题标题】:Gravity during player jumping on trampoline玩家在蹦床上跳跃时的重力
【发布时间】:2014-02-13 21:48:26
【问题描述】:

所以我设法修复了我的蹦床代码,让玩家可以像在蹦床上一样弹起。但是,我在使用此代码时遇到了 2 个问题。

首先,当在蹦床上弹跳时,它基本上会将玩家直接推入平流层,他们只是继续前进,永不停息,就好像没有重力将他们击落一样。即使我在播放器刚体设置中检查了重力。

其次,即使玩家不在蹦床附近,例如在没有附加蹦床脚本代码的平台上,将导致玩家在游戏开始时立即被抛到空中。

代码如下:

using UnityEngine;
using System.Collections;

public class small_trampoline_bounce : MonoBehaviour
{

bool willBounce = false;
float bounceHeight = 10;
public Transform Player;


// Use this for initialization
void Start()
{
}

// Update is called once per frame
void Update()
{
    Vector3 velocity = Player.rigidbody.velocity;

    if (willBounce)
    {

        Player.rigidbody.velocity = new Vector3(velocity.x, 0, velocity.z);

        Player.rigidbody.AddForce(0, bounceHeight, 0, ForceMode.Impulse);


        willBounce = false;
    }
}

void OnCollisionEnter(Collision Trampoline)
{

    if (Trampoline.gameObject.name == "Player")
    {
        willBounce = true;
    }

}

}

这也是蹦床和播放器的当前设置状态

播放器设置: http://gyazo.com/b4d924849a86e5158361f6081948e39f.png

蹦床设置: http://gyazo.com/ded2884b93dd53c9e1b7fabc57e4fe51.png

【问题讨论】:

  • 你的重力设置为多少(类似于编辑 --> 设置 --> 物理)
  • 我看到你检查了播放器上的“使用重力”选项,但你是否也在某处设置了Physics.gravity
  • @BlueRaja-DannyPflughoeft 我的重力设置如下:gyazo.com/4676fd1f6e8d4984ee371cb70dac1564.png
  • @StevenHansen 你看到的代码是我目前唯一的代码。对于我的重力设置,请参阅我刚刚为 BlueRaja 发布的链接

标签: unity3d gravity


【解决方案1】:

我不知道这是否能解决您的问题,但不妨试试这个:

using UnityEngine;
using System.Collections;

public class small_trampoline_bounce : MonoBehaviour
{

float bounceHeight = 10;


// Use this for initialization
void Start()
{
}

// Update is called once per frame
void Update()
{
}

void OnCollisionEnter(Collision Trampoline)
{

    if (Trampoline.gameObject.name == "Player")
    {
        Vector3 velocity = Trampoline.gameObject.rigidbody.velocity;
        Trampoline.gameObject.rigidbody.velocity = new Vector3(velocity.x, 0, velocity.z);

        Trampoline.gameObject.rigidbody.AddForce(0, bounceHeight, 0, ForceMode.Impulse);
    }

}

}

这样,addforce 只会应用于击中蹦床的玩家,并且应该只触发一次。它可能无法解决您的问题,因为您的代码理论上应该可以工作,但您永远不知道。

【讨论】:

    【解决方案2】:

    好吧,在弄乱了代码之后,我设法让它正常工作,最终结果是我结束了将代码附加到玩家角色马达,这样当玩家与蹦床发生碰撞时,它就会实现向下的力将播放器的反弹反转到统一检查器中规定的特定高度。下面的代码显示了最终结果。

    public float speed;
    public float jumpSpeed; 
    public float gravity;
    public Vector3 moveDirection = Vector3.zero;
    public float downwardForce;
    public float terminalVelocity;
    public float airSpeed;
    
    void Update() 
    {
        CharacterController controller = GetComponent<CharacterController>();
        // is the controller on the ground?
        if (controller.isGrounded) 
        {
            //Feed moveDirection with input.
            moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
            moveDirection = transform.TransformDirection (moveDirection);
            downwardForce = 0;
            //Multiply it by speed.
            moveDirection *= speed;
    
            //Jumping
            if (Input.GetButton ("Jump"))
            {
                downwardForce -= jumpSpeed;
            }   
        } 
        else 
        {
            if (downwardForce < terminalVelocity)
            {
                    moveDirection *= airSpeed;
                    downwardForce += gravity * Time.deltaTime;
            }
        }
        //Applying gravity to the controller
        moveDirection.y -= downwardForce * Time.deltaTime;
        //Making the character move
        controller.Move(moveDirection * Time.deltaTime);
    }
    void OnTriggerEnter(Collider collider)
    {           
        if (collider.tag == "smallTrampoline") 
        {
            downwardForce *= -1 * collider.gameObject.transform.GetComponent<SmallTrampolineBounce> ().jumpSpeed;
    
        }
        if (collider.tag == "largeTrampoline") 
        {
            downwardForce *= -2 * collider.gameObject.transform.GetComponent<SmallTrampolineBounce> ().jumpSpeed;
        }
    

    我还实现了 airSpeed 作为一种在玩家在空中时强制他们快速移动的方法,因为之前玩家根本无法移动,只是停留在上下跳跃

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多