Unity 3D 跳跃和运动冲突

问题描述

我想制作一个无尽的跑步游戏,玩家可以跳跃并且他会自动向前移动,因此,对于向前移动,我想使用刚体速度函数以使移动更流畅,而对于跳跃,我做了一个自定义功能。我的问题是,当我在我的更新方法中单独使用速度函数时,它可以工作,当我将它与我的跳转函数一起使用时,它停止工作。我能做什么?。我尝试将地板和玩家盒子碰撞器的材料更改为非常滑的材料,但没有

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

public class MovePrima : MonoBehavIoUr
{
    [Serializefield]
    private float _moveSpeed = 0f;              
    [Serializefield]
    private float _gravity = 9.81f;             
    [Serializefield]
    private float _jumpspeed = 3.5f;           

    public float speedAccel = 0.01f;            

    private CharacterController _controller;    

    private float _directionY;                 

    public float startSpeed;                  

    public float Yspeed = 10f;                       
        
    private int i = 0;                          

    public float touchSensibility = 50f;              

    public int accelTime = 600;
    [Serializefield]
    Rigidbody rb;
    // Start is called before the first frame update
    void Start()
    {
        startSpeed = _moveSpeed;
        _controller = GetComponent<CharacterController>();      
        rb = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        rb.veLocity = new Vector3(0f,0f,1f) * _moveSpeed;
        touchControls();
    }


    public void touchControls()
    {
       


        if (SwipeManager.swipeUp && _controller.isGrounded)
        {
            _directionY = _jumpspeed;
            print("Entrato");
        }
        _directionY -= _gravity * Time.fixedDeltaTime;                      

        _controller.Move((new Vector3(0,_directionY,0)) * Time.fixedDeltaTime * Yspeed);      



        i += 1;                                                             
        if (i % accelTime == 0)                                                   
        {
            _moveSpeed += startSpeed * speedAccel;
            i = 0;
        }
    }

    private void OnControllerColliderHit(ControllerColliderHit hit) //Checks if it collided with obstacles
    {
        if(hit.transform.tag == "Obstacle")
        {
            GameOverManager.gameOver = true;
        }
    }
}
 

解决方法

您应该使用 CharacterController 或 Rigidbody。使用 CharacterController,您可以告诉 Unity 放置角色的位置,并且您必须检查自己是否是一个有效的位置。使用刚体,您可以将其推向您想要的方向,但角色周围的物理也会产生干扰。

,

你可以在跳跃时使用 Rigidbody.AddForce 代替 _controller.Move

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...