0
votes

Im getting unity vector3 errors, but I've never mentioned that in my program. help?

using UnityEngine;

public class PlayerMovement : MonoBehaviour {


    [SerializeField] public Rigidbody rb;
    [SerializeField] public int speed = 10;
    // Update is called once per frame
    void FixedUpdate()
    {
        // These are the keys.
        if ( Input.GetKey("a") )
        {
            rb.AddForce(-transform.forward*speed, 0, 0);
        }
        if ( Input.GetKey("d") )
        {
            rb.AddForce(transform.forward*speed, 0, 0);
        }
        if ( Input.GetKey("w") )
        {
            rb.AddForce(0, 0, transform.forward*speed);
        }
        if ( Input.GetKey("s") )
        {
            rb.AddForce(0, 0, -transform.forward*speed);
        }
    }
}

errors:

ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at <437ba245d8404784b9fbab9b439ac908>:0) System.ThrowHelper.ThrowArgumentOutOfRangeException () (at <437ba245d8404784b9fbab9b439ac908>:0) System.Collections.Generic.List`1[T].get_Item (System.Int32 index) (at <437ba245d8404784b9fbab9b439ac908>:0) UnityEngine.UIElements.VisualElement+Hierarchy.get_Item (System.Int32 key) (at /Users/builduser/buildslave/unity/build/Modules/UIElements/VisualElementHierarchy.cs:461) UnityEngine.UIElements.EventDispatchUtilities.PropagateToIMGUIContainer (UnityEngine.UIElements.VisualElement root, UnityEngine.UIElements.EventBase evt) (at /Users/builduser/buildslave/unity/build/Modules/UIElements/Events/IEventDispatchingStrategy.cs:161) UnityEngine.UIElements.MouseEventDispatch

Assets/PlayerMovement.cs(14,25): error CS1503: Argument 1: cannot convert from 'UnityEngine.Vector3' to 'float'

Assets/PlayerMovement.cs(18,25): error CS1503: Argument 1: cannot convert from 'UnityEngine.Vector3' to 'float'

Assets/PlayerMovement.cs(22,31): error CS1503: Argument 3: cannot convert from 'UnityEngine.Vector3' to 'float'

Assets/PlayerMovement.cs(26,31): error CS1503: Argument 3: cannot convert from 'UnityEngine.Vector3' to 'float'

1
only because a method or class does not occur directly within your own code, doesn´t mean it isn´t called by it. - HimBromBeere
transform.forward is a Vector3, so it is indeed used by your code - Biesi Grr

1 Answers

3
votes

AddForce takes either 3 float values or 1 Vector3 value.

You are passing a mix between float and Vector3 values since

transform.forward * speed

is a Vector3!


It should probably rather be

    if ( Input.GetKey("a") )
    {
        rb.AddForce(-transform.right * speed);
    }
    if ( Input.GetKey("d") )
    {
        rb.AddForce(transform.right * speed);
    }
    if ( Input.GetKey("w") )
    {
        rb.AddForce(transform.forward * speed);
    }
    if (Input.GetKey("s") )
    {
        rb.AddForce(-transform.forward * speed);
    }

Or rather use AddRelativeForce

    if ( Input.GetKey("a") )
    {
        rb.AddRelativeForce(-speed, 0, 0);
    }
    if ( Input.GetKey("d") )
    {
        rb.AddRelativeForce(speed, 0, 0);
    }
    if ( Input.GetKey("w") )
    {
        rb.AddRelativeForce(0, 0, speed);
    }
    if (Input.GetKey("s") )
    {
        rb.AddRelativeForce(0 ,0 -speed);
    }

As was already suggested in your previous question


The first

ArgumentOutOfRangeException

comes from another script where you are trying to access an array or list with a too high index ...