简体   繁体   English

我正在统一制作 FPS 游戏并不断收到 1 个错误

[英]I'm making an FPS game in unity and keep getting 1 error

My code works perfectly fine until I tried to run the game where I get this error with a vector3 If anybody knows the answer the help would be greatly appreciated thanks It seems like the code should work but whenever I use the vector 3 completely breaks Unity I'm new to the game development and I'm following along with Tutorial to learn how to use the unity我的代码工作得非常好,直到我尝试运行游戏时出现这个错误,并且使用 vector3是游戏开发的新手,我正在跟随教程学习如何使用统一

Assets\movement.cs(48,25): error CS0019: Operator '*' cannot be applied to operands of type 'Vector3' and 'Vector3' Assets\movement.cs(48,25):错误 CS0019:运算符“*”不能应用于“Vector3”和“Vector3”类型的操作数

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

public class Movement : MonoBehaviour
{
    [Header("Movement")]
    public float moveSpeed;

    public Transform orientation;

    float horizontalInput;
    float verticalInput;

    Vector3 moveDirection;

    Rigidbody rb;

    private void Start()
    {
        rb = GetComponent<Rigidbody>();
        rb.freezeRotation = true;

    }

    private void Update()
    {
        MyInput();
    }


    private void FixedUpdate()
    {
        MovePlayer();
    }



    private void MyInput()
    {
        horizontalInput = Input.GetAxisRaw("horizontal");
        verticalInput = Input.GetAxisRaw("Vertical");
    }

    private void MovePlayer()
    {

        moveDirection = orientation.forward * verticalInput * orientation.right * horizontalInput;

        rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);

    }
}

Add horizontal and vertical input, don't multiply it添加水平和垂直输入,不要相乘

private void MovePlayer()
{
    Vector3 moveDirectionY = orientation.forward * verticalInput;
    Vector3 moveDirectionX = orientation.right * horizontalInput;

    rb.AddForce((moveDirectionX + moveDirectionY).normalized * moveSpeed * 10f, ForceMode.Force);
}

Just as Geeky said "Add horizontal and vertical input, don't multiply it"正如 Geeky 所说的“添加水平和垂直输入,不要相乘”

using UnityEngine;

public class Movement : MonoBehaviour
{
    private Vector3 moveDir;
    public Rigidbody rb;
    public float moveSpeed;
    void Update()
    {
        moveDir = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")).normalized;
        // You can use either AddForce or velocity. AddForce feels kinda like space though.
        rb.AddForce(moveDir * moveSpeed * Time.deltaTime);// Time.deltaTime makes it move at the same speed on even 20fps
        //rb.velocity = moveDir * moveSpeed * Time.deltaTime;
    }
}

暂无
暂无

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

相关问题 我正在统一制作 FPS 游戏并不断收到这些错误 - I'm making an FPS game in unity and keep getting these errors 我已经遵循了关于为统一的fps游戏制作控件的教程。 控制工作但如果我离开控件然后我继续向左移动 - I have followed a tutorial on making controls for a fps game on unity. the controls work but if I leave the controls then I keep moving to the left 在 Unity 中制作现代 FPS 游戏健康再生系统 C# - Making a Modern FPS game health regeneration system in Unity C# 我在 Unity 中制作游戏时遇到了 C# 中的 OnCollisionEnter 函数问题 - I'm having a problem with the OnCollisionEnter function in C# while making a game in Unity 我正在用 Unity 程序制作节奏游戏。 这是一个关于长笔记的问题 - I'm making a rhythm game with Unity program. It's a question about long notes 在我统一制作的 2D 平台游戏中,我的玩家被困在墙内 - My player is getting stuck inside the wall in the 2D platforming game I am making in unity 在 unity2d 中尝试在屏幕上添加游戏时不断出现错误 - keep getting an error when trying to add a game over screen in unity2d 尝试用鼠标统一拖动对象,我不断收到相同的错误 - Trying to drag an object with mouse in unity, I keep getting the same error 我正在制作一个控制台应用程序游戏 (C#),但我遇到了堆栈溢出错误。 如何避免? - I'm making a Console Application Game (C#), and I'm running into Stack Overflow Error. How to avoid? 我正在制作ASP .NET MVC应用程序,在运行项目时出现此错误 - I'm making ASP .NET MVC application, I'm getting this error when running the project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM