简体   繁体   English

c#Unity跳转问题

[英]c# Unity Jump Issue

I'm new to programming in C#. 我是C#编程的新手。

I have a sphere rigid body on a plane in unity. 我在一个平面上有一个球体刚体。

I want to make this sphere jump when the spacebar is pressed and then bounce twice before coming to rest again when the key is released. 我想使该球体在按下空格键时跳起来,然后反弹两次,然后在释放键时再次停下来。

This will not always be on the plane it will sometimes be on an object and I would like the height of the bounce to reflect the distance dropped from the initial jump. 这并不总是在飞机上,有时会在物体上,我希望弹跳的高度反映出从初始跳跃下落的距离。

I currently have a camera facing onto the sphere from a side and have it following the ball from a set distance. 我目前有一个摄像头,从侧面朝向球体,并使其在一定距离后跟随球。 The sphere can move in any direction but the camera always stays on the same side and distance from the sphere. 球体可以沿任何方向移动,但相机始终位于同一侧,并且与球体的距离相同。

The issue is that my code currently says (to my knowledge) IF the spacebar is not pressed try to move the sphere's Y position down, then if the spacebar is pressed make the sphere jump and then come back down on spacebar release. 问题是我的代码当前(据我所知)说,如果未按下空格键,则尝试将球体的Y位置向下移动,然后如果按下空格键,则使球体跳转,然后在释放空格键时再放下。

The way this code is written makes my plane jump as the ball is constantly bouncing into it when spacebar is not being pressed but if I take that part out the ball refuses to drop. 编写此代码的方式使我的飞机跳起来,因为在不按下空格键的情况下,球不断地弹跳进去,但是如果我取出那部分,球就不会掉落。

Example Code : 示例代码:

public class RollAdvancedScript : MonoBehaviour 
{   
    public Transform camera;
    public float posY;

    void lol()
    {
        posY =  transform.position.y;
    }

    void Update() 
    {
       if (Input.GetKey(KeyCode.D))
        { 
            Vector3 vectorToUse = camera.right;
            vectorToUse.y = 0;
            transform.position += vectorToUse * Time.deltaTime * 5;
        }
        if (Input.GetKey(KeyCode.A))
        { 
            Vector3 vectorToUse = -camera.right;
            vectorToUse.y = 0;
            transform.position += vectorToUse * Time.deltaTime * 5;
        }
        if (Input.GetKey(KeyCode.S))
        { 
            Vector3 vectorToUse = -camera.forward;
            vectorToUse.y = 0;
            transform.position += vectorToUse * Time.deltaTime * 5;
        }
        if (Input.GetKey(KeyCode.W))
        { 
            Vector3 vectorToUse = camera.forward;
            vectorToUse.y = 0;
            transform.position += vectorToUse * Time.deltaTime * 5;
        }
        if (Input.GetKey(KeyCode.Space))
        {
            Vector3 vectorToUse = camera.up;
            transform.position += vectorToUse * Time.deltaTime * 10;    
        }
        else
        {   
            if ( posY >= 0) 
                {Vector3 vectorToUse = camera.up;
                transform.position -= vectorToUse * Time.deltaTime * 10;
                }
                else
            {
                Vector3 vectorToUse = camera.up;
                vectorToUse.y = 0;
                transform.position -= vectorToUse * Time.deltaTime * 10;
            }
        }
    }
}

Example Image: 示例图片:

在此处输入图片说明 Please ignore the shadow as it's not working correctly just now. 请忽略阴影,因为它暂时无法正常工作。

TLDR; TLDR; The best way to make an object jump and fall with a bounce on Keypress when moving in relation to a fixed camera position? 相对于固定摄像机位置移动时,使物体在Keypress上弹跳并跳下的最佳方法是什么?

Are you trying to handle this in animation or in physics? 您要尝试在动画还是物理学中解决这个问题? Looking at the code it looks like pure controller animation, so I'd suggest that you track your jump state explicitly instead of just using the y > 0 check every update. 查看代码看起来就像是纯控制器动画,所以我建议您显式跟踪跳转状态,而不是仅使用y> 0检查每次更新。 It's a lot easier to see what's going on if you know the state of the object. 如果知道对象的状态,则更容易看到正在发生的事情。

Your code makes it look like you pop up by (10 * deltaTime) when you hit space and then immediately pop down by 10 until you are below zero. 您的代码使您看起来像是在碰到空格时以(10 * deltaTime)弹出,然后立即以10弹出直到您小于零。 Are you expecting the player to hold down space to fly, jetpack-style? 您是否希望玩家保持喷气背包式的飞行空间? Otherwise you should give an initial impulse up and then subtract from it to give a smooth fall. 否则,您应该给一个初始冲动,然后从中减去以得到一个平稳的下降。

Here's a very simple example. 这是一个非常简单的示例。 The up vector is world up (not Camera up as in your example) but if you're camera is not rolling the result would be the same. 向上矢量是世界向上的(而不是示例中的“相机向上”),但是如果相机不滚动,则结果将相同。

public class DoJump : MonoBehaviour {

    public float JumpSpeed = .25f;
    public bool Jumping;
    Vector3 jumpVec;

    void Start () {
    Jumping = false;
    }

    void Update () {

        if (Jumping)
        {
            transform.position += jumpVec * Time.deltaTime;
            jumpVec += (Vector3.down * Time.deltaTime);
            //accelerate downward one unit per second per second                           
        }
        else
        {
            if (Input.anyKeyDown)
            {
                Jumping = true;
                jumpVec = Vector3.up * (JumpSpeed * Time.SmoothDeltaTime);
                // impulse up @ jumpSpeed units per second
            }
        }
    }

    void LateUpdate()
    {
        if (transform.position.y < 0)
        {
            transform.position = new Vector3(transform.position.x, 0, transform.position.z);
            Jumping = false;
        }
    }
}

If you're going to have uneven terrain, you should probably use physics colliders or raycast to know when to stop falling. 如果您将要在崎uneven不平的地形上行驶,则可能应该使用物理对撞机或光线投射仪来了解何时停止下降。

This is my code, hope it can help you: 这是我的代码,希望它可以为您提供帮助:

public class Player : MonoBehaviour
{ 
    public float m_gravity=10f;          
    void Jump()
    {
        if (m_ch.isGrounded) //this is the point
        {
            m_Gravity = 10;
            if (Input.GetKeyDown(KeyCode.Space))
            {
                m_Gravity = -8;
            }
        }
        else
        {
            m_Gravity += 10f * Time.deltaTime;
            if (m_Gravity >= 10) { m_Gravity = 10; }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM