【问题标题】:Unity 3D jump and movement conflictunity 3D 跳跃和运动冲突
【发布时间】:2021-03-09 11:09:09
【问题描述】:

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

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;
        }
    }
}
 

【问题讨论】:

    标签: unity3d input touch conflict


    【解决方案1】:

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

    【讨论】:

    • 那么,你的意思是我应该在这两个解决方案中只选择一个,否则它们会发生冲突?
    • 是的,如果你同时使用两者,你​​必须非常小心。就个人而言,我不知道什么时候同时使用两者的好例子——你可能想要对 CharachterController 的精确控制,但有时会添加一个投标物理,但是如果你在物理播放时不打开 CharacterController,奇怪的事情会发生的。
    • 有很多关于这个的好博客,但经过非常快速的谷歌我会推荐:medium.com/ironequal/…
    【解决方案2】:

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

    【讨论】:

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