Apply force to RigidBody using Float?

When this object is spawned I need it to look in the direction of the mouse pointer then apply an initial burst of speed (after which gravity sets in), I especially need to be able to set an initial burst of speed on an item using a Float, I can probably get this to work without the float input but it’s quite important the player is able to set the force applied using the inspector:

Anyone have any ideas on how to accomplish this with a float?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerThrowingNut : MonoBehaviour
{
    public float nutSpeed;
    public GameObject nutHitEffect;
    private int myLayer = 8;

    private Rigidbody2D theRB;

    // Start is called before the first frame update
    void Start()
    {
        theRB = GetComponent<Rigidbody2D>();
        gameObject.layer = myLayer;
        Physics2D.IgnoreLayerCollision(myLayer, myLayer, true);

        // GETS WHERE THE CURSOR IS ON THE SCREEN
        Vector3 worldMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        // LOOK TOWARDS THE CURSOR
        transform.LookAt(Vector3.zero); // IF DOES NOT WORK TRY worldMousePos

        // THEN ADD INITIAL VELOCITY
        theRB = AddForceAtPosition(nutSpeed);

    }

    // Update is called once per frame
    void Update()
    {




    }

    // WHEN IT COLLIDES WITH SOMETHING....
    void OnTriggerEnter2D(Collider2D other)
    {
        // IF THE OTHER OBJECT IS SET TO THE "Killable" LAYER
        if (other.gameObject.layer == LayerMask.NameToLayer("Killable"))
        {
            // SPAWN A HIT GRAPHIC THEN DESTROY THE PROJECTILE
            Instantiate(nutHitEffect, transform.position, transform.rotation);
            Destroy(gameObject, 1.0f);
        }
    }
}
transform.LookAt(Vector3.zero);

This code makes the transform point the Z axis towards (0,0,0), which is the world origin.

In 2D, the Z axis usually points forwards and doesn’t change. For this reason, LookAt is mainly useful for 3D uses.

Commonly in 2D, the X axis will be “forward” for a 2D object. So you need to set the X axis direction.

You can set “transform.right” to a new vector. That will automatically rotate the transform so that the X axis points in the direction you give it.

The direction you want you can get like this:
transform.right = worldMousePos - transform.position;

This should work for most use cases.

Yeah i’m making a 2d platformer in 3D space so it’s tricky

I replaced transform.LookAt(Vector3.zero); with
transform.right = worldMousePos - transform.position;

And apparently it’s now saying theRB = AddForceAtPosition(nutSpeed); does not exist in the current context…

Damn code…im only new to this

theRB = AddForceAtPosition(nutSpeed);
This is assigning the variable “theRB” which is not what you want.

I think it should be
theRB.AddForce(nutSpeed * transform.right);
so that you’re calling a function in “theRB” and passing a force vector in the direction that the object is now facing.

Thanks for the help, it’s easy to miss that type of syntax for a newbie :frowning:

i’m almost there!

I think the problem im having is that instead of setting the direction with the player i’m trying to set it up per-item.

Basically I just wanted a “throw on click” so once the item spawns it gets lobbed in the direction of your click (the closest comparable game is Worms, where the items act with gravity and bounce around) but I want to make prefab items where you can set the velocity in the inspecter on each item (to make the building process a bit easier and more modular as I create content).

The script runs now, although the projectiles just go straight upwards and dont follow the trajectory of the mouse click, i’ll have to try and see why that’s happening, the full script now looks like:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerThrowingNut : MonoBehaviour
{
    public float nutSpeed;
    public GameObject nutHitEffect;
    private int myLayer = 8;

    private Rigidbody2D theRB;


   
    // Start is called before the first frame update
    void Start()
    {
        theRB = GetComponent<Rigidbody2D>();
        gameObject.layer = myLayer;
        Physics2D.IgnoreLayerCollision(myLayer, myLayer, true);

        // GETS WHERE THE CURSOR IS ON THE SCREEN
        Vector3 worldMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        // LOOK TOWARDS THE CURSOR
        transform.right = worldMousePos - transform.position;

        // THEN ADD INITIAL VELOCITY
        theRB.AddForce(nutSpeed * transform.right);
    }

    // Update is called once per frame
    void Update()
    {




    }

    // WHEN IT COLLIDES WITH SOMETHING....
    void OnTriggerEnter2D(Collider2D other)
    {
        // IF THE OTHER OBJECT IS SET TO THE "Killable" LAYER
        if (other.gameObject.layer == LayerMask.NameToLayer("Killable"))
        {
            // SPAWN A HIT GRAPHIC THEN DESTROY THE PROJECTILE
            Instantiate(nutHitEffect, transform.position, transform.rotation);
            Destroy(gameObject, 1.0f);
        }
    }
}