beginningdevloper
beginningdevloper

Reputation: 21

why can't integer convert from unity?

I have just encountered error CS1504: argument 4: cannot convert from 'int' to 'UnityEngine.ForceMode'in Unity.

This is the coding that I have written

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{

    // This is a reference to the Rigidbody component called "rb"
    public Rigidbody rb;

    public float forwardForce = 2000f;  // Variable that determines the forward force
    public float sidewaysForce = 500f;  // Variable that determines the sideways force

    // We marked this as "Fixed"Update because we
    // are using it to mess with physics.
    void FixedUpdate()
    {
        // Add a forward force
        rb.AddForce(0, 0, forwardForce * Time.deltaTime);

        if (!Input.GetKey("d"))  // If the player is pressing the "d" key
        {
        }
        else
        {
            // Add a force to the right
            global::System.Object value = rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, 50);
        }

        if (Input.GetKey("a"))  // If the player is pressing the "a" key
        {
            // Add a force to the left
            global::System.Object value = rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, 50);
        }
    }
}

Can Somebody help me out and point out what I have done wrong?

Upvotes: 1

Views: 132

Answers (1)

Allenchew
Allenchew

Reputation: 38

The Addforce from rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, 50);, this last value should be something like ForceMode.Impulse.

For eg. rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.Impulse);

reference : https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html

And rb.AddForce(0, 0, forwardForce * Time.deltaTime); working is because it already has the default value for ForceMode, which is ForceMode.Force.

public void AddForce(float x, float y, float z, ForceMode mode = ForceMode.Force);

Upvotes: 1

Related Questions