CharacterController 移动不正确 [英] CharacterController is not moving correctly

查看:71
本文介绍了CharacterController 移动不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Unity 中开始了一个新项目,我希望我的角色同时跳跃和移动,但代码不能很好地协同工作.角色发现难以移动.两者完美地分开工作

im starting a new proyect in Unity and I want my character to jump and move at the same time but the codes don't work well together. the character finds it difficult to move. both work perfectly separately

这是我用来移动的代码:

this is the code im using to move around:

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

public class PlayerMovement : MonoBehaviour  
{  
    public float speed = 30f;              
    public float turnSpeed = 80f;  
    
    private float horizontalInput;          
    private float verticalInput;
    
    
    
    // Update is called once per frame
    void Update()
    {
        horizontalInput = Input.GetAxis("Horizontal");   //teclas
        verticalInput = Input.GetAxis("Vertical");
        
        transform.Translate(speed*Time.deltaTime*Vector3.forward*verticalInput);    //movimiento
        transform.Rotate(turnSpeed*Time.deltaTime*Vector3.up*horizontalInput);      //rotar
    }
}

这是我找到的跳跃教程:

and this one is a tutorial i found for jumping:

using System.Collections;  
using System.Collections.Generic;  
using UnityEngine;   
  
public class JumpControl : MonoBehaviour  
{  
    private CharacterController controller;  
    private float verticalVelocity;  
    private float gravity = 14.0f;  
    private float jumpForce = 10.0f;  
    
    void Start()  
    {  
        controller = GetComponent<CharacterController>();  
    }  
  
    private void Update(){  
        if (controller.isGrounded)  
        {  
            verticalVelocity = -gravity * Time.deltaTime;  
            if (Input.GetKeyDown(KeyCode.Space))  
            {  
                verticalVelocity = jumpForce;  
            }  
        }  
        else  
        {  
            verticalVelocity -= gravity * Time.deltaTime;  
        }  
  
        Vector3 moveVector = new Vector3(0, verticalVelocity, 0);  
        controller.Move(moveVector * Time.deltaTime);  
    }  
}  

推荐答案

CharacterController基于物理的.您永远不想通过 Transform 组件将基于物理的运动与应用变换混合在一起.这会破坏物理、碰撞检测并导致各种意外行为和奇怪的动作.

The CharacterController is physics-based. You never want to mix a physics-based movement with applying transformations via the Transform component. This breaks the physics, collision detection and causes all kind of unexpected behaviour and strange looking movements.

所以代替

transform.Translate(speed*Time.deltaTime*Vector3.forward*verticalInput); 

你更愿意做

[SerializeField] private CharacterController controller;

private void Awake()
{
    if(!controller) controller = GetComponent<CharacterController>();
}

void Update()
{
    horizontalInput = Input.GetAxis("Horizontal");   //teclas
    verticalInput = Input.GetAxis("Vertical");
    
    transform.Rotate(turnSpeed * Time.deltaTime * Vector3.up * horizontalInput);

    controller.Move(speed * Time.deltaTime * Vector3.forward * verticalInput);  
}

这篇关于CharacterController 移动不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆