#How to make a 2d projectile that shoots towards the mouse?
141 messages · Page 1 of 1 (latest)
Get mouse position
Get projectile position
CurrentVeclocity = (mousePos - projectilePos).normalised() * speed
Projectile.transform.position += currentVelocity * time.deltatime
i'll try it out right now
im assuming you get mouse position from
Camera.main.WorldToScreenPoint(Input.mousePosition);
You want the world point not screen point
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
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
What’s the add torque
its a bomb and its being thrown so its to make it spin while going through the air
maybe thats affecting it
Okay
let me remove it
Idk probably not
hm
whats up?
What is this script on?
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
Then what is this FireBomb thing you keep referencing?
FireBombProjectile is the firebomb prefab instantiated into the game
The object that the script is on?
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
hm?
myPos needs to be the position of the firebomb
You’re getting the position of the player
oooh
ok let me fix that real fast
no dice still
maybe its something from the outside
how can we debug this
Well we should first fix your architecture a bit
it doesnt let me send pictures
my roles are completely factually accurate
nice
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
okay
ill move to the firebomb script
should i just remove anything related to moving the firebomb on this script
I wouldn't touch this script in case it breaks stuff
just make 2 new scripts for now
ok
hold on also real fast im going to make a collision layer so the bomb doesnt touch the playert
then how do you expect it to update the position lmao
if you're setting the velocity
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
oh so you don't want it to constantly be moving towards the mouse?
yes
for reference
the player is meant to pick up a spherical bomb, and when he clicks he throws it
is this a sidescroller?
ill send a reference pic
with you now
so right now does it just constantly move towards that mouse position?
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
okay but what currently happens
nothing because i removed the velocuty
actually because i did the collision layer i just removed the offset
well you don't need to put it in 2 scripts now, I thought you wanted the fireball to pathfind/track
oh i see.
go back to the code as it was before just cntrl Z
done
okay so what is currently happening when you throw the ball
they look like they're going sideways too
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
okay
I mean, that at the very least, should make it start moving towards the mouse
have you tried removing the torque
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
I guess make sure it’s going through that part of the code and that nothing else is changing the velocity
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