繁体   English   中英

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

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

我的代码工作得非常好,直到我尝试运行游戏时出现这个错误,并且使用 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);

    }
}

添加水平和垂直输入,不要相乘

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

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

正如 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.

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