Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我如何通过按住空格键来增加让玩家跳得更高的功能?_C#_Unity3d - Fatal编程技术网

C# 我如何通过按住空格键来增加让玩家跳得更高的功能?

C# 我如何通过按住空格键来增加让玩家跳得更高的功能?,c#,unity3d,C#,Unity3d,我对编码和制作游戏很陌生。我在一些YT教程的帮助下创建了这个脚本,我想知道如何在按住空格键的同时让我的角色跳得更高。我尝试了不同的方法,但没有成功。这是我的字符代码。谢谢 using System; using UnityEngine; using UnityEngine.UI; public class TPMovementScript : MonoBehaviour { public CharacterController controller; public Trans

我对编码和制作游戏很陌生。我在一些YT教程的帮助下创建了这个脚本,我想知道如何在按住空格键的同时让我的角色跳得更高。我尝试了不同的方法,但没有成功。这是我的字符代码。谢谢

using System;
using UnityEngine;
using UnityEngine.UI;

public class TPMovementScript : MonoBehaviour
{

    public CharacterController controller;
    public Transform cam;

    public float speed = 6f;
    public float jump = 10f;
    public float gravity = -9.81f;
    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;
    public float jumpHeight = 3f;



    Vector3 velocity;
    bool isGrounded;

    public float turnSmoothTime = 0.1f;

    float turnSmoothVelocity;

    // Update is called once per frame
    void Update()
    {
        //Checks if player is grounded & resets velocity
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

        if (isGrounded && velocity.y < 0)
        {
            velocity.y = -2f;
        }

        velocity.y += gravity * Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);

        //Makes Player move Using WASD or upDownLeftRight
        float horizontal = Input.GetAxisRaw("Horizontal");
        float vertical = Input.GetAxisRaw("Vertical");
        Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized.normalized;


        if (isGrounded && Input.GetKey(KeyCode.Space))
        {
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
            
        }

        //Makes Character face direction and makes forward to camera position
        if (direction.magnitude >= 0.1f)
        {
            float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
            float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
            transform.rotation = Quaternion.Euler(0f, angle, 0f);

            Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
            controller.Move(moveDir.normalized * speed * Time.deltaTime);

        }

    }
}
使用系统;
使用UnityEngine;
使用UnityEngine.UI;
公共类TPMovementScript:MonoBehavior
{
公共字符控制器;
公共变换凸轮;
公共浮子速度=6f;
公共浮动跳跃=10f;
公共浮子重力=-9.81f;
公开审查;
公共浮地距离=0.4f;
公共层掩码地面掩码;
公共浮子跳线高度=3f;
矢量3速度;
布尔被禁足;
公共浮动时间=0.1f;
浮动速度;
//每帧调用一次更新
无效更新()
{
//检查播放机是否接地并重置速度
isground=physical.CheckSphere(groundCheck.position、groundDistance、groundMask);
如果(isGrounded&&velocity.y<0)
{
速度y=-2f;
}
速度.y+=重力*时间增量;
控制器。移动(速度*时间。增量时间);
//使用WASD或upDownLeftRight使玩家移动
float horizontal=Input.GetAxisRaw(“水平”);
float vertical=Input.GetAxisRaw(“垂直”);
Vector3方向=新的Vector3(水平、0f、垂直)。normalized.normalized;
if(isground&&Input.GetKey(KeyCode.Space))
{
速度y=数学Sqrt(跳跃高度*-2f*重力);
}
//使角色朝向并向前移动到摄影机位置
如果(方向大小>=0.1f)
{
浮动目标角=Mathf.Atan2(方向x,方向z)*Mathf.Rad2Deg+cam.eulerAngles.y;
浮动角度=数学平滑阻尼角度(transform.eulerAngles.y、targetAngle、ref turnsoothVelocity、turnsoothTime);
transform.rotation=四元数.Euler(0f,角度,0f);
Vector3 moveDir=四元数.Euler(0f,targetAngle,0f)*Vector3.forward;
controller.Move(moveDir.normalized*速度*时间.deltaTime);
}
}
}

这看起来像是您问题的答案,代码也类似。

您能添加链接的内容而不是只添加链接吗?链接可能会随着时间的推移而中断。除了答案之外,请留下链接。