Unity3d charactercontroller在跳跃时在平台上弹跳,但在下落时不弹跳。

huangapple go评论45阅读模式
英文:

Unity3d charactercontroller bouncing on platforms while jumping but not when falling

问题

以下是你提供的代码的翻译:

我在我的游戏中遇到了一些奇怪的问题。我使用3D模型和一个固定的摄像机来制作2D平台游戏。我有一个角色控制器,带有以下用于移动和跳跃的脚本:

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

public class PlayerMovement : MonoBehaviour
{
    private CharacterController controller;
    private Vector3 playerVelocity;
    private bool groundedPlayer;

    [SerializeField] private Animator _animator;
    [SerializeField] private float playerSpeed = 2.0f;
    [SerializeField] private float jumpHeight = 4.0f;
    [SerializeField] private float gravityValue = -1f;

    [SerializeField] private AudioSource _audioSource;
    [SerializeField] private AudioClip _stepStone;

    private void Start()
    {
        controller = GetComponent<CharacterController>();
        _animator = GetComponent<Animator>();
        _audioSource = GetComponent<AudioSource>();
    }

    void Update()
    {
        MovePlayer();
    }

    private void MovePlayer()
    {
        float x = Input.GetAxis("Horizontal");
        groundedPlayer = controller.isGrounded;

        if (groundedPlayer && playerVelocity.y < 0)
        {
            playerVelocity.y = 0f;
            _animator.SetBool("Jumping", false);
        }

        Vector3 move = new Vector3(x, 0, 0);
        controller.Move(move * Time.deltaTime * playerSpeed);

        if (move != Vector3 zero)
        {
            gameObject.transform.forward = move;
        }

        // 改变玩家的高度位置
        if (Input.GetButtonDown("Jump") && groundedPlayer)
        {
            _animator.SetBool("Jumping", true);
            playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
        }

        playerVelocity.y += gravityValue * Time.deltaTime;
        controller.Move(playerVelocity * Time.deltaTime);

        if (groundedPlayer)
        {
            _animator.SetInteger("Speed", Convert.ToInt32(x));
        }
    }

    public void playStoneStep()
    {
        _audioSource.PlayOneShot(_stepStone);
    }
}

关于你的角色在跳跃到平台上时弹跳,但在落下到平台上时不弹跳的问题,你已经尝试了一个不具有弹性的材质。你可以尝试以下方法来解决这个问题:

  1. 检查碰撞器:确保你的角色碰撞器配置正确,以便在与平台接触时触发弹跳。

  2. 物理材质:确保你的角色和平台之间的物理材质配置正确。你可以尝试调整摩擦力和弹性属性以获得所需的行为。

  3. 检查代码:检查你的脚本,确保在跳跃时适当地应用了力和速度。

如果问题仍然存在,你可能需要提供更多关于你的游戏和角色控制器设置的信息,以便更详细地分析问题。

英文:

I have something strange happening in my game.I use 3d models and a fixed camera to make a 2D platformer. I have a charactercontroller with the following script for movement and jumping:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private CharacterController controller;
private Vector3 playerVelocity;
private bool groundedPlayer;
[SerializeField] private Animator _animator;
[SerializeField] private float playerSpeed = 2.0f;
[SerializeField] private float jumpHeight = 4.0f;
[SerializeField] private float gravityValue = -1f;
[SerializeField] private AudioSource _audioSource;
[SerializeField] private AudioClip _stepStone;
private void Start()
{
controller = GetComponent&lt;CharacterController&gt;();
_animator = GetComponent&lt;Animator&gt;();
_audioSource = GetComponent&lt;AudioSource&gt;();
}
void Update()
{
MovePlayer();
}
private void MovePlayer()
{
float x = Input.GetAxis(&quot;Horizontal&quot;);
groundedPlayer = controller.isGrounded;
if (groundedPlayer &amp;&amp; playerVelocity.y &lt; 0)
{
playerVelocity.y = 0f;
_animator.SetBool(&quot;Jumping&quot;, false);
}
Vector3 move = new Vector3(x, 0,0);
controller.Move(move * Time.deltaTime * playerSpeed);
if (move != Vector3.zero)
{
gameObject.transform.forward = move;
}
// Changes the height position of the player..
if (Input.GetButtonDown(&quot;Jump&quot;) &amp;&amp; groundedPlayer)
{
_animator.SetBool(&quot;Jumping&quot;, true);
playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
}
playerVelocity.y += gravityValue * Time.deltaTime;
controller.Move(playerVelocity * Time.deltaTime);
if (groundedPlayer)
{
_animator.SetInteger(&quot;Speed&quot;, Convert.ToInt32(x));
}
}
public void playStoneStep()
{
_audioSource.PlayOneShot(_stepStone);
}
}

My character bounces if it jumps on platforms but not when falling on them. I tried a non bouncy material. how can I fix this?
Here is a video showing it : <https://youtu.be/ZhV8nHklsH8>

答案1

得分: 1

以下是您要翻译的内容:

我会通过添加一个检查角色是否着陆在地面上的功能来实现这一点。我已经将您的功能拆分成不同的、更易管理的部分。我添加了一个层遮罩,您需要配置它以匹配地面的层。

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

public class PlayerMovement : MonoBehaviour
{
    private CharacterController controller;
    private Vector3 playerVelocity;
    private bool groundedPlayer;

    [SerializeField] private Animator _animator;
    [SerializeField] private float playerSpeed = 2.0f;
    [SerializeField] private float jumpHeight = 4.0f;
    [SerializeField] private float gravityValue = -1f;

    [SerializeField] private LayerMask groundMask;

    [SerializeField] private AudioSource _audioSource;
    [SerializeField] private AudioClip _stepStone;

    private void Start()
    {
        controller = GetComponent<CharacterController>();
        _animator = GetComponent<Animator>();
        _audioSource = GetComponent<AudioSource>();
    }

    void Update()
    {
        HandleMovement();
        HandleJump();
        HandleGravity();
    }

    private void HandleMovement()
    {
        controller.Move(new Vector3(Input.GetAxis("Horizontal"), 0, 0) * Time.deltaTime * playerSpeed);
        if (groundedPlayer)
        {
            _animator.SetInteger("Speed", Convert.ToInt32(x));
        }
    }

    private void HandleJump()
    {
        if (Input.GetButtonDown("Jump") && groundedPlayer)
        {
            _animator.SetBool("Jumping", true);
            playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
        }
    }

    private void HandleGravity()
    {
        playerVelocity.y += gravityValue * Time.deltaTime;
        controller.Move(playerVelocity * Time.deltaTime);
    }

    void OnCollisionEnter(Collision col)
    {
        if (IsInLayerMask(col.gameObject, groundMask))
        {
            playerVelocity.y = 0f;
            _animator.SetBool("Jumping", false);
        }
    }

    private bool IsInLayerMask(GameObject obj, LayerMask layerMask)
    {
        return ((layerMask.value & (1 << obj.layer)) > 0);
    }

    public void playStoneStep()
    {
        _audioSource.PlayOneShot(_stepStone);
    }
}

请注意,我已将代码部分保留为原文。

英文:

I would do this by adding something that checks if you land on the floor. I did below split your functionality into different, more manageable parts. I've added a layer mask which you will need to configure with the layer of the ground.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement: MonoBehaviour
{
private CharacterController controller;
private Vector3 playerVelocity;
private bool groundedPlayer;
[SerializeField] private Animator _animator;
[SerializeField] private float playerSpeed = 2.0f;
[SerializeField] private float jumpHeight = 4.0f;
[SerializeField] private float gravityValue = -1f;
[SerializeField] privateLayerMask groundMask;
[SerializeField] private AudioSource _audioSource;
[SerializeField] private AudioClip _stepStone;
private void Start()
{
controller = GetComponent&lt;CharacterController&gt;();
_animator = GetComponent&lt;Animator&gt;();
_audioSource = GetComponent&lt;AudioSource&gt;();
}
void Update()
{
HandleMovement();
HandleJump();
HandleGravity();
}
private void HandleMovement()
{
controller.Move(new Vector3(Input.GetAxis(&quot;Horizontal&quot;), 0,0) * Time.deltaTime * playerSpeed);
if (groundedPlayer)
{
_animator.SetInteger(&quot;Speed&quot;, Convert.ToInt32(x));
}
}
private void HandleJump()
{
if (Input.GetButtonDown(&quot;Jump&quot;) &amp;&amp; groundedPlayer)
{
_animator.SetBool(&quot;Jumping&quot;, true);
playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
}
}
private void HandleGravity()
{
playerVelocity.y += gravityValue * Time.deltaTime;
controller.Move(playerVelocity * Time.deltaTime);
}
void OnCollisionEnter(Collision col)
{
if (IsInLayerMask(col.gameObject, groundMask))
{
playerVelocity.y = 0f;
_animator.SetBool(&quot;Jumping&quot;, false);
}
}
private bool IsInLayerMask(GameObject obj, LayerMask layerMask)
{
return ((layerMask.value &amp; (1 &lt;&lt; obj.layer)) &gt; 0);
}
public void playStoneStep()
{
_audioSource.PlayOneShot(_stepStone);
}
}

huangapple
  • 本文由 发表于 2023年2月9日 03:08:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390613.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定