首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在使用Cinemachine时用wasd修复移动

如何在使用Cinemachine时用wasd修复移动
EN

Stack Overflow用户
提问于 2022-05-04 09:26:13
回答 1查看 91关注 0票数 0

图片:https://i.stack.imgur.com/O9T5t.png

嗨,我最近刚试过“团结中的一个球”的教程。在本教程中,您只教了摄像机如何从1个定点跟踪球。但我想编辑,这样相机将始终后面的球跟随,他们的关键,WASD总是一样的。它类似于openworld游戏,如GTA V、RDR2等。

我尝试使用Cinemachine的vcam和免费摄像头,它的工作为相机跟随,但关键的WASD混乱,例如W应该是前进,但现在它正在向后移动。

这是我移动播放器的PlayerController代码,我相信是在OnMove函数上。

代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using UnityEngine.SceneManagement;

// Include the namespace required to use Unity UI and Input System
using UnityEngine.InputSystem;
using TMPro;

public class PlayerController : MonoBehaviour
{

    // Create public variables for player speed, and for the Text UI game objects
    public float speed;
    public TextMeshProUGUI countText;
    public GameObject winTextObject;

    private float movementX;
    private float movementY;

    private Rigidbody rb;
    private int count;

    // At the start of the game..
    void Start()
    {
        // Assign the Rigidbody component to our private rb variable
        rb = GetComponent<Rigidbody>();

        // Set the count to zero 
        count = 0;

        SetCountText();

        // Set the text property of the Win Text UI to an empty string, making the 'You Win' (game over message) blank
        winTextObject.SetActive(false);


    }

    void FixedUpdate()
    {
        // Create a Vector3 variable, and assign X and Z to feature the horizontal and vertical float variables above
        Vector3 movement = new Vector3(movementX, 0.0f, movementY);

        rb.AddForce(movement * speed);
    }

    void OnTriggerEnter(Collider other)
    {
        // ..and if the GameObject you intersect has the tag 'Pick Up' assigned to it..
        if (other.gameObject.CompareTag("PickUp"))
        {
            other.gameObject.SetActive(false);

            // Add one to the score variable 'count'
            count = count + 1;

            // Run the 'SetCountText()' function (see below)
            SetCountText();
        }
    }

    void OnMove(InputValue value)
    {
        Vector2 v = value.Get<Vector2>();

        movementX = v.x;
        movementY = v.y;
    }

    void SetCountText()
    {
        countText.text = "Count: " + count.ToString();

        if (count >= 12)
        {
            // Set the text value of your 'winText'
            winTextObject.SetActive(true);
            Time.timeScale = 0;




        }

    }





}
EN

回答 1

Stack Overflow用户

发布于 2022-05-04 09:36:50

为了解决这个问题,你必须根据相机调整输入轴。

代码语言:javascript
复制
Transform.TransformDirection("// input vector..");

如果改正率低,这个问题就可以解决。

代码语言:javascript
复制
void FixedUpdate()
{
    Vector3 movement = new Vector3(movementX, 0.0f, movementY);
    
    // affect base on camera direction
    movement = Camera.main.transform.TransformDirection(movement);

    movement.y = 0;

    rb.AddForce(movement.normalized * speed);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72110678

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档