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