[英]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.