DoomPriestK
Active Coder
I 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 using the inspector:
Anyone have any ideas on how to accomplish this without changing to a bool or something?
Anyone have any ideas on how to accomplish this without changing to a bool or something?
Code:
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);
// 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);
}
}
}