#Pooling bullets from gun

1 messages · Page 1 of 1 (latest)

blissful veldt
#

I'd like to request a screenshot.

Enter Play Mode and Pause it while a bullet is moving towards a wall.
Screenshot so I can see the bullet and the wall in the scene, while showing me the actual script component of the inspector for the bullet.

echo kestrel
#

oh

#

i just found my issue

blissful veldt
#

Nice. What was it?

#

CTRL+SHIFT+P btw

echo kestrel
#

the bullets fly in the air in such a way that it looks like there touching but there not

#

there way up

#

so the real issue

#

is how tf i make them go where i shoot

#

rn they just fly off in a weird direction

#

and spawn in a weird spot

blissful veldt
#

Best make those into pastebin links

echo kestrel
#

alr

blissful veldt
#
// This will make the bullet constantly move towards an arbitrary direction (always the same)
rb.velocity = Vector3.right * speed;

What you probably want to do when getting a new bullet from the pool, is position it in front of your player, inherit the player's rotation, and move in direction of transform.forward

echo kestrel
#

right so starting off getting the players pos

#

how do i do that

blissful veldt
#

it looks like that's already what you are doing in the Gun script

echo kestrel
#

where

#

oh like the gameobject thing

#

ye i did that

#

it didnt work

blissful veldt
#

the only thing missing then, is the direction.
Read the comment I wrote in the previous code segment.

transform.right is relative to the game object
Vector3.right will always be the same direction.

#

so with Vector3.right you're telling it to go east,
or rather, increase it's X position, rather than decreasing it.

echo kestrel
#

rb.velocity = transform.right * speed; this?

blissful veldt
#

That's relative instead of absolute, yes.
But I believe you want it to move forward, not sideways.

echo kestrel
#

mhm

#

so instead of right i put forward?

blissful veldt
#

Yes.

echo kestrel
#

oooh that works

#

ok now

#

issue num 2

#

its spawning weird places arround the gun

#

like above or below the barrel it changes

blissful veldt
#

private Vector3 bulletPosition;

#

you're using a fixed point to spawn the bullets

echo kestrel
#

but i set it each frame

#

bulletPosition = transform.position;

blissful veldt
#

Gun script, Line 25:

// Remove this
bullet.transform.position = bulletPosition;

// Replace with
bullet.transform.position = transform.position;
#

sec

#

ah true

echo kestrel
#

confused

#

i was right

blissful veldt
#

I just didn't see that.

echo kestrel
#

so ye why does it spawn above or below the barrel

blissful veldt
#

It's difficult to know exactly what the problem is, when the symptom is visual. If you could make a video with OBS and upload it to Streamable.com or wherever, that would be helpful.

echo kestrel
#

sure

blissful veldt
#

Actually, I think I might know what it is.

echo kestrel
#

itll take a sec

#

oh

#

ok

#

go

blissful veldt
#

Wait, how is the player character moving?
Rigidbody or Transform?

echo kestrel
#

uhh

#

transform i think

#

or the playercontroller?

#

idk

#

tutorial

#

character controller*

blissful veldt
#

Did you write the CharacterController script, or use Unity's built in one?

echo kestrel
#

built in

blissful veldt
#

kk

#

I actually can't recall how it works, but that's easy enough to check.
Does the player have a Rigidbody?

echo kestrel
#

mhm

blissful veldt
#

Okay

#

Then I'm not so sure I see the problem just yet.

echo kestrel
#

that purple thing is the bullet

blissful veldt
#

So both the player and bullet have colliders.
Perhaps you are actually positioning the bullet inside the player collider bounds.

#

That would effectively push it out.

echo kestrel
#

so i should test moving it away really far

blissful veldt
#

sec, a simple test

echo kestrel
#

hmm moving it out didnt affect where it spawned

blissful veldt
#
bullet.transform.position = bulletPosition + (transform.forward * 1f);
#

Line 25

echo kestrel
#

same thing

blissful veldt
#

Alright. Make a video then.

echo kestrel
#

it happens more towards the end

#

also a future issue after we fix this one, they go right through the floor

#

also brb rq

#

bac

blissful veldt
#

From looking at the video, I'm not entirely sure if there is an actual issue. It could perhaps be a visual inconsistency.
I asked a friend with more hands-on experience if he could have a look. No reply there yet.

echo kestrel
#

hmm

blissful veldt
#

There are a few more things we can try:

  • Use a different camera angle
  • Make the gun's visual component invisible
  • Slow down bullet velocity
echo kestrel
#

alr hol up

#

oh ye nahh its definetely going above and below the barrel

#

i just want to make it spawn infront of the barell which im certain it dont do rn

#

how can i make it spawn at the location of the existing bullet and shoot out from there

#

bulletPosition = bullet.transform.position; this didnt work

blissful veldt
#

I can see why

#

Does the placeholder bullet always exist, or is it used in the pool?

echo kestrel
#

always exist

blissful veldt
#

And is it referenced?

echo kestrel
#

wait no it isnt alawys thee

#

when i spawn in it falls throught the ground\

blissful veldt
#

Just disable the Rigidbody component, or disable gravity on it.

#

Two things

echo kestrel
#

u want me to paste that

blissful veldt
#

No, sec

echo kestrel
#

turning off gravity did nothing

#

still falls

blissful veldt
#

is it still off after entering Play mode?

echo kestrel
#

mhm

blissful veldt
#

First off, you don't need the bulletPosition variable

echo kestrel
#

y

blissful veldt
#

second, you have two variables named bullet
it's best to use logical names
Like bulletSpawn and bullet

echo kestrel
#

nvm i see

blissful veldt
#

I'm feeling the need for coffee, because I already ran two circles around my head with this thing,
but I'll unload what I have for now.

echo kestrel
#

dont stress yourself i appreciate your help so far but ima head to bed, and i wont have access to my computer for the following 2 weeks so i guess cya

blissful veldt
#

The bulletSpawn bullet object should have its collider and rigidbody disabled. The collider can stay if you offset the position.
For testing purposes, make Player and Bullet not collidable, with the Collision Layer Matrix

echo kestrel
#

hm

blissful veldt
#

Two weeks sounds like a horribly long break for a beginner.
At least your scripts are still simple at this point.

echo kestrel
blissful veldt
#

Two weeks break means you can't comprehend your previous code, unless you actually made it readable and used comments.

#

Haha nice

#

Good luck with all that.
Break problems down to simpler bits, solve each one, then combine.

echo kestrel
#

ye will do thx and i might see you next week 🙂

#

wait no

#

rip

#

its 3 am

blissful veldt
#

Good night and have a nice vacation o/

echo kestrel
#

will do