2D Platformer Projectile wont face mouse position?
Tried this a hundred different ways but cant seem to get it working, projectile should face the mouse cursor position the moment it spawns, then add initial force (closest game comparison is Worms, i'm making a physics based system).
So after the initial burst of momentum gravity kicks in, like a grenade
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);
}
}
}
Was thinking about trying to make the ThrowPoint on my character constantly face the cursor then just figure out how to apply instant force to the projectile, but not sure if that works.
Any help would be appreciated!
Your answer
Follow this Question
Related Questions
Player controlled platforms 0 Answers
[2D] Rigid body snaps through floor and back 1 Answer
2d platformer physics 0 Answers
What is the Easiest Way to Check if Two Time Periods are Overlapping 0 Answers
Collision Not regestering 1 Answer