#How to make a 2d projectile that shoots towards the mouse?

141 messages · Page 1 of 1 (latest)

agile inlet
#

So far everything i have tried has been very janky, either vaguely shooting in the GENERAL direction of the mouse or not working at all. Can anyone help me with this?

chilly seal
#

Get projectile position

#

CurrentVeclocity = (mousePos - projectilePos).normalised() * speed

#

Projectile.transform.position += currentVelocity * time.deltatime

agile inlet
#

i'll try it out right now

#

im assuming you get mouse position from

Camera.main.WorldToScreenPoint(Input.mousePosition);
chilly seal
#

You want the world point not screen point

agile inlet
#

oh ok

#

and currentvelocity is a vector2?

chilly seal
#

Camera.main.WorldToScreenPos(input mouse pos)

#

That gives you the world pos of the mouse (vector 3)

#

You basically wanna make a vector2 out of that using the x and y

#

And then that’s your mouse pos and it should work

agile inlet
#

hm

#

maybe i coded mine wrong ill try to figure this out

#
    FireBombProjectile.transform.position = new Vector2(transform.position.x - .5f, transform.position.y + .5f);
    FireBombProjectile.GetComponent<Rigidbody2D>().AddTorque(50);  
    Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    Vector2 myPos = new Vector2(transform.position.x - .5f, transform.position.y + .5f);

    Vector2 currentVelocity = (mousePos - myPos).normalized * 10;

    FireBombProjectile.GetComponent<Rigidbody2D>().velocity = currentVelocity;
#

oops

#

thats what i have right now the myPos is offset from the player to prevent the projectile from colliding with the player

#

its only shooting upwards

chilly seal
#

What’s the add torque

agile inlet
#

its a bomb and its being thrown so its to make it spin while going through the air

#

maybe thats affecting it

chilly seal
#

Okay

agile inlet
#

let me remove it

chilly seal
#

Idk probably not

agile inlet
#

hm

chilly seal
#

You can’t make the vec 2 like that

#

Wait maybe you can

#

Yeah I bet you can actually

agile inlet
#

whats up?

chilly seal
#

What is this script on?

agile inlet
#

unity c#

#

visual basic studio

chilly seal
#

No what object

#

The script is attached to an object right, what object

agile inlet
#

it's attached to a projectile object with a rigidbody 2d, collider, particle system

#

when the player throws a bomb it instantiates

#

i give it an offset so it doesnt collide with the player when being thrown

chilly seal
#

Then what is this FireBomb thing you keep referencing?

agile inlet
#

FireBombProjectile is the firebomb prefab instantiated into the game

chilly seal
#

The object that the script is on?

agile inlet
#

yes

#

no

#

oops

#

ok my bad

#

the script that adds the firebomb velocity is attached to the player itself

#

because it is a function of the player

chilly seal
#

Right

#

Well that’s the problem

agile inlet
#

hm?

chilly seal
#

myPos needs to be the position of the firebomb

#

You’re getting the position of the player

agile inlet
#

oooh

#

ok let me fix that real fast

#

no dice still

#

maybe its something from the outside

#

how can we debug this

chilly seal
#

Well we should first fix your architecture a bit

agile inlet
#

it doesnt let me send pictures

chilly seal
#

One sec I’ll move to pc

#

#get-a-role

agile inlet
#

my roles are completely factually accurate

chilly seal
#

nice

agile inlet
chilly seal
#

okay so, basically you want 2 scripts
FireballProjector:
-This goes on your player
-It handles instantiating the fireball when you press the fire button

FireballMovement:
-This goes on the fireball prefab
-It handles moving the fireball

agile inlet
#

okay

#

ill move to the firebomb script

#

should i just remove anything related to moving the firebomb on this script

chilly seal
#

I wouldn't touch this script in case it breaks stuff

#

just make 2 new scripts for now

agile inlet
#

ok

#

hold on also real fast im going to make a collision layer so the bomb doesnt touch the playert

chilly seal
#

wait

#

does that code only run once

agile inlet
#

yes

#

it is meant to give the bomb an arc

#

its a physics based projectile

chilly seal
#

then how do you expect it to update the position lmao

#

if you're setting the velocity

agile inlet
#

what im trying to achieve is shooting the firebomb out and let it fall towards

#

not shooting it straight forward consistantly

#

like actually throwing a ball

chilly seal
#

oh so you don't want it to constantly be moving towards the mouse?

agile inlet
#

yes

#

for reference

#

the player is meant to pick up a spherical bomb, and when he clicks he throws it

chilly seal
#

is this a sidescroller?

agile inlet
#

yes

#

2d platformer

chilly seal
#

riiiight eight

#

right

agile inlet
#

ill send a reference pic

chilly seal
#

with you now

agile inlet
chilly seal
#

so right now does it just constantly move towards that mouse position?

agile inlet
#

these are the bombs

#

it throws it straight upward

#

and it falls due to physics

#

and depending on which way the player is facing it goes the other direction

#

each image shows which respective direction the bomb goes

#

i have updated the code

chilly seal
#

okay but what currently happens

agile inlet
#

nothing because i removed the velocuty

#

actually because i did the collision layer i just removed the offset

chilly seal
#

well you don't need to put it in 2 scripts now, I thought you wanted the fireball to pathfind/track

agile inlet
#

oh i see.

chilly seal
#

go back to the code as it was before just cntrl Z

agile inlet
#

done

chilly seal
#

okay so what is currently happening when you throw the ball

agile inlet
#

the bomb is thrown straight upwards

chilly seal
#

they look like they're going sideways too

agile inlet
#

at a slight arc forward depending on the direction the player is facing

#

not at all related to the mouse's position

#

no matter where the mouse is

chilly seal
#

okay

agile inlet
#

current code

chilly seal
#

I mean, that at the very least, should make it start moving towards the mouse

#

have you tried removing the torque

agile inlet
#

yeah i did, not much changed

#

because torque doesnt affect velocity unless your going off of transform.up or transform.right which are relative to the rotation

#

sorry was eating lunch, im back now

#

first i'll try some debugging methods

chilly seal
#

I guess make sure it’s going through that part of the code and that nothing else is changing the velocity

agile inlet
#

top one is mousepos and the bottome one is current velocity

#

how about getting the angle from the position of the projectile and the mouse position, and adding force in that direction

#

i see what the issue is

#

since im using perspective view of the scene, and not orthographic the position of the mouse was screwed up.

#

im kinda sad

#

i was using perspective view to have a parallax effect on the background

#

i have an idea on how to overcome this

#

maybe i can use a position multiplier in a script on the background objects to recreate it

#

ok, problem solved

#

do i need to close this forum post?

#

thank you so much

#

@chilly seal