简体   繁体   English

我怎样才能让玩家统一跳跃 3d

[英]How can I make the player jump in unity 3d

Hello can you help to make the player jump你好,你能帮忙让玩家跳起来吗

also how to make the player crouch?还有如何让玩家蹲下?

this is my move script:这是我的移动脚本:

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

public class Movement : MonoBehaviour
{
    public CharacterController controller;
    public Transform cam;
    public float speed = 6f;
    public float turnsmoothT = 0.1f;
    float turnsmoothV;

    void Update()
    {
        float horizontal = Input.GetAxisRaw("Horizontal");
        float vertical = Input.GetAxisRaw("Vertical");
        Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;

        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 turnsmoothV, turnsmoothT);
            transform.rotation = Quaternion.Euler(0f, angle, 0f);
            Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
            controller.Move(moveDir.normalized * speed * Time.deltaTime);
        }
    }
}

.................................................................................................................................................................................................................................................................. ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... …………

See if this helps:看看这是否有帮助:

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

public class Movement : MonoBehaviour
{
    public CharacterController controller;
    public Transform cam;
    public float speed = 6f;
    public float turnsmoothT = 0.1f;
    private float turnsmoothV;
    private bool jumping = false;
    private float moveDirectionY = 0;
    private float gravity = 9.81f;

    private void Update()
    {
        float horizontal = Input.GetAxisRaw("Horizontal");
        float vertical = Input.GetAxisRaw("Vertical");
        Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
        Vector3 moveDir = Vector3.zero;
        if (jumping && controller.isGrounded)
        {
            jumping = false;
        }
        if (Input.GetKeyDown(KeyCode.Space) && !jumping && controller.isGrounded)
        {
            moveDirectionY = 3f;
            jumping = true;
        }
        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 turnsmoothV, turnsmoothT);
            transform.rotation = Quaternion.Euler(0f, angle, 0f);
            moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
        }
        moveDirectionY -= gravity * Time.deltaTime;
        moveDir.y = moveDirectionY;
        controller.Move(moveDir * speed * Time.deltaTime);
    }
}

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

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