简体   繁体   English

需要更好的动作脚本?

[英]Better movement script needed?

I got an issue that I've been trying to figure out for some time now and still haven't managed.我遇到了一个问题,我已经尝试解决了一段时间,但仍然没有解决。 I created a script for movement which besides using the WASD keys also uses two more buttons to go up and down.我创建了一个移动脚本,除了使用 WASD 键外,它还使用了两个按钮来上下移动 go。 The thing is - because of the way I added those buttons for some reason no other function regarding position of the player doesn't work well.问题是 - 由于我出于某种原因添加这些按钮的方式,没有其他 function 关于播放器的 position 不能正常工作。 For example if I put a collider with a simple on trigger transform.position function for the player to hit - the player is placed on that position but then instantly returned back like there was nothing.例如,如果我放置一个带有简单触发转换的对撞机。position function 供玩家击中 - 玩家被放置在 position 上,但随后立即返回,就像什么都没有一样。

Here is my code.这是我的代码。 I had tons of iterations how this movement can be done.我有大量的迭代如何完成这个动作。 I did it through physics and controller in several ways but nothing helped.我通过物理学和 controller 以多种方式做到了这一点,但没有任何帮助。 Can you guys tell me if it's the code or some hidden Unity synergy that I don't know about?你们能告诉我这是代码还是我不知道的一些隐藏的 Unity 协同作用吗?

(this version works around addForce. Regardless, whatever way I make the up and down functions I cannot move the player with script after that.) (这个版本围绕 addForce 工作。无论如何,无论我用什么方式制作上下函数,我都不能在那之后用脚本移动播放器。)

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

public class Mov : MonoBehaviour
{
    private CharacterController controller;
    private Rigidbody Rb;
    private Vector3 playerVelocity;
    private float playerSpeed =12;
    
    void Start()
    {
        controller = gameObject.AddComponent<CharacterController>();
        Rb = gameObject.GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        Vector3 add =new Vector3 (0, playerSpeed, 0);
        controller.Move(move * Time.deltaTime * playerSpeed);

       if (Input.GetKey(KeyCode.Mouse1))
        {
            Rb.AddForce(0, 1200, 0, ForceMode.Acceleration);
        }
        if (Input.GetKey(KeyCode.Mouse0))
        {
            Rb.AddForce(0, -1200, 0, ForceMode.Acceleration);
        }
    }
}

I'm not sure what you mean by "no other function regarding position of the player doesn't work well" but from have you turning "Collision Detection" in your Rigidbody's settings to "Continuous" and "Interpolate" to "Interpolate"?我不确定你所说的“没有其他 function 关于播放器的 position 不能正常工作”是什么意思,但是你是否将 Rigidbody 设置中的“碰撞检测”改为“连续”并将“插值”改为“插值”? If you don't do so, Unity's collision's may be a bit funky especially if you are going at high speeds.如果您不这样做,Unity 的碰撞可能会有点奇怪,尤其是在您高速行驶时。

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

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