Home > Software engineering >  I'm making an FPS game in unity and keep getting 1 error
I'm making an FPS game in unity and keep getting 1 error

Time:08-04

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

Assets\movement.cs(48,25): error CS0019: Operator '*' cannot be applied to operands of type 'Vector3' and '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);

    }
}

CodePudding user response:

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);
}

CodePudding user response:

Just as Geeky said "Add horizontal and vertical input, don't multiply it"

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;
    }
}
  • Related