#๐ผ๏ธโ2d-tools
1 messages ยท Page 58 of 1
ty
Np
sometimes my gameobject's spriterenderer counts color by 255f and sometimes by 1f. What determines this?
Idk tbh
We've got Color and Color32
Afaik they are both the same except Color32 uses 255 while Color uses 1
really?
Interesting
Please never post code in an image itโs a pain to read. Please put it in a Pastebin / hastebin
ok
I've never found it a pain to read
Also, is this source code or something?
I've never seen code written this way for a game
Oh wait
It's like a custom transform
oh
basically I make functions for every common task that are easy to use, like basic commands
which makes the main code easier to read
That's cool
It's like combining the Transform and the SpriteRenderer into one component
I might try that tomorrow
nice
cya
What are the elements in Color data types respectively?
i used to come across a problem where a data type for colors was ordered b g r a instead of r g b a
and my sprite turned the wrong color
i searched it up and its correctly ordered
um... red, green, blue and alpha.
It's standard RGBA color representation.
What do you mean by "ordered"? You shouldn't really need to know the memory order unless you're working in unsafe mode for some reason.
Thanks, just one question what does the Force!pde2D.Impulse dp?
It just tells the rigidbody how to apply the force
Using impulse makes it start fast and then slow down
Ah okay
Using force just adds to the velocity, nothing cool about that one
And if I make the bounciness of an object high, will that affect things that collider with it if the object itself cant move?
Any object that has the physics material should be affected as long as it's rigidbody is not set to kinematic or static
Alright thanks I will try both of these ways out
Np, hope they work
But I think I will stick with the second one, as in my experience its not good to purely rely on unityโs physics
Yeah
I don't trust Unity's physics lmao
I usually find it to be inconsistent and somewhat glitchy
Yep I tried making things that entirely use unity physics a couple of times, but always ended up writing a script to do it
I've run into issues myself... especially handling corners
Aghh corners
I've tried to make platformers so many times, but the player gets stuck every other block I pass
Cuz of the collision
Me: "I'll make a simple arkanoid clone using physics! Should be really straight forward!"
Unity: "Hahahaha. No."
I recently discovered that the composite collider 2d is a good fix
So true
3D physics is fine
2D though...
That's not very fine
Hi, I have a problem with changing scenes, when my player sprite doesnยดt show in game. Layers order should be ok, because in the main scene its alright. Where could be problem?
Whats the Z position of the player in the second scene?
And the camera?
z=-1,4659
that is probably what it didnt catch it, because the camera was behind the player
I would set both to 0
ok, for some reason now, when i change z to 0 on player, he appears, but I am only able to change the z position in game mode, bcs the player is teleported to the second scene. Is there a way to set it properly?
How do you teleport it into the second scene?
via empty object with 2d collider and script
So you are setting the transform of the player to match the transform of another object?
And is the player already in that scene?
no
Then how do you teleport it into the scene?
ehm i dont know
ok, I fixxed it (changed cameras position values in second scene to match with the camera in first scene)
thank you for help
but I have another issue
When I teleport, it duplicates my player, so in that scene, there are two players (both move the same)
now it even duplicated it several times
So how do i move a Rigidbody2d without interfering with the vertical velocity? Everything i tried either pinned it in place or made it fall slower
if you're modifying the velocity directly, you can do rb2d.velocity = new Vector2(velocityChangeX, rb2d.velocity.y). Or you can use AddForce and just not add any force on the y axis
Hey folks, I have a question regarding 2D cameras and pixel/sprite art.
I've been working with the above for some time, and understand the issues regarding the rotation of sprites, how (and why) they alias, and also issues with sub-pixel rendering and how that can create "doubling" of individual pixels when a sprite is not clamped to the pixel grid.
I have managed to alleviate these issues to an "acceptable" level, however I would like to make further improvements. Specifically, I noticed that the Editor viewport camera does not suffer from any of the above issues. I am able to zoom/scale, rotate, translate my sprites by more or less any factor (no matter how large or small) and the result in the viewport will be a (near) perfectly positioned/rotated view of my original sprite. (Although the view in the game will never be as high-quality)
Howeverm I have not been able to find out why the editor camera is able to do this, so I was wondering if anyone had the answer? And also, would be possible to replicate that same behavior with a game view camera? If it were possible, it would be a huge benefit to my game!
Thanks ๐
when a projectile is launched, I made it so that it only depletes hp from an enemy. However, when it passes through obstacles it jumps over them or below them as though the obstacle should affect the projectile's single y position attack.
I thought while writing this that the projectile's rigidbody is affected by the obstacle's collider. If this is true, how can I prevent it?
shouldn't this question be posted in the art channels?
Dont think so, its a camera issue not an art asset issue
Well, may be related to the sampling/import settings of an art asset but it falls under the programming spectrum more than asset creation
i mean they might know more about rendering stuff, which will help getting reached out to sooner
Oh I see what you mean
Well, if nobody is able to answer it here I'll post it over there. Im in no hurry at the moment, at my day-job anyway haha
yes
Hi! I'm trying to make it so it loads a new scene when it collides with a certain object. I have made it known in build settings and applied the script to the box + I have turned off mesh rendering and on for is trigger. Hope yall can help!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneSwitch1 : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
SceneManager.LoadScene(0);
}
}
what's wrong with it? you havent mentioned what's wrong
aahh! sorry, im just going right through it
did you build the scene?
how would one do that?
ahh, just seen it, trying now
still doesnt seem to be working
how can I make every element of a List<bool> true without iterating?
actually I change my mind
how do I check the length of a List?
Normally there will be a property that is either size, length, count, num etc
list.Count
list<bool> mylist
mylist.count
^ that but capital C and L
yeah
also i realized in my code there was a way to do it without count
but thx still
I have an enemy ai script here:https://pastebin.com/0ZPTrg2H that moves the enemy towards the player only on the x axis. How would i make it so that the enemy rotates either 0 or 180 degrees depending on where the player is?
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
E.g if the player is to it's right face right
an extremely simple implementation of that would be something like:
if player.transform.position.x < enemy.transform.position.x: rotate enemy to the left
if player.transform.position.x > enemy.transform.position.x: rotate enemy to the right
Tilemap colliders exist
It's a component you attach to a tilemap object like any other collider
It will give everything on that tilemap a collider. So if you only want collision with certain tiles, move those tiles to a new tilemap and put the collider on that
you can give any sprite a custom physics shape in the sprite editor
TileMap collider will respect that
Anything that's part of a tilemap is a sprite
yep - go edit the various sprite(s) custom physics shape in the sprite editor
or do that
AFAIK there's only one sprite
the tile palette thing is not a sprite
it's a tile
I think it probably just edits the same sprite?
ยฏ_(ใ)_/ยฏ LMK - I didn't realize the Tile had a sprite editor button on it
check his position
maybe he's just behind the tiles
Is it on a tilemap layer that has a tilemap collider on it?
Do you see the colliders for the tiles? Select the grid in the hierarchy and you should be able to see the collider outlines
Alright, you should probably make a new tilemap that all the collision tiles will be on. Also do you have your player and the tilemap on separate collision layers? Because your player should be forced out of the collider if they are able to collide
It's the right drop down menu under the object name. You can also change what layers collide in Project Settings > Physics 2D
Layer: Default
The drop down menu on the right directly under the box to enter the object's name
Well they should be colliding then. Is the player able to move at all with how you have it set up now?
Oh wait, does your player have a RigidBody2D on it?
You can turn gravity scale to 0
Give it a RigidBody2D, set its gravity scale to 0, then press play and let me know if the player flies away
Did you turn off collision for the default layer for itself? Check in Project Settings > Physics 2D. Scroll all the way down until you see the matrix with all the checkboxes. Make sure the one that is on the Default row and column is checked.
Can you screenshot the inspector with the player selected to show the collider and rigidbody settings
Yes, that's what I suggested earlier.
For collisions to work as expected because I just realized you are probably changing the transform to move your player which entirely ignores physics/collisions
Yeah you should be using the rigidbody to move the player instead of the transform. You can use rb2d.MovePosition(), or set the rb2d.velocity for easy movement
could someone explain this to me
private IEnumerator AutoFire()
{
while (true)
{
FireBullet();
yield return new WaitForSeconds(FireRate);
}
}
``` why cant i add a "while (false)" inside of this?
because right now i have inside my ontriggerenter2D StartCoroutine
but i want on exittrigger2D to StopCoroutine and wait a set amount of seconds
so how can i make it wait
because right now this happends
everytime i reenter the trigger it starts up again but i want it to suppend 3 seconds or so after ontriggerexit
Well if they expected to interact with physics then they are doing it wrong
Anyone have an idea how I can check the tile depth of a tilemap inside a shader?
I can check x and y coordinates using the vertex position, but it seems all layers are at z=0
wont it loop when i put StopCoroutine inside ontriggerexit?
No
wdym
Physics objects interact with colliders but projects with only colliders dont use physics
You need to elaborate a little
You can use physics with only colliders. Colliders interact with all Physics queries like Raycasts, OverlapXXX, CheckXXX, etc..
but simulating newtonian physics with colliders? You need a rigidbody.
If you want your character to collide with those colliders, the easiest way by far to achieve that is with a Rigidbody
You don't need a rigidbody, but if you want to detect collisions you need to either use raycasts, overlaps or implement your own collision system
that's what it's made for
but yes there are other ways - querying the colliders via raycasts etc..
It's also a good idea in general to used a fixed timestep for your character movement to ensure it behaves the same regardless of framerate
If it's a platformer kinda game
that will ensure the player always gets the same jump heights etc.
I use my own collision system, it's just a bunch of overlap checks
and then resolve the position based on extents
You need a composite collider
It will merge colliders together
That is because you gave a rigidbody to your collider
Only your player really needs one, yeah
Then it is part of the composite (I don't really remember), if you remove it, you might be able to remove your rb
Ah yeah, if you mark it as static or kinematic you will solve that
If you mark your tilemap rb as static it will solve it
yes
Just try it
No, the rigidbody in your tilemap
it has an option called body type
Did you make sure the composite is working?
Does it merge your colliders into big chunks?
I think you need to mark the colliders with "used by effector"
in your walls
Not the composite collider, but the actual colliders
Not that I am aware of
You probably could with editor scripts but that is a bit advanced for this topic
If you wanna see if they are single or in chunk you can just turn off the 2d grid
It should
You might not be checking the "used by effector" box on the actual colliders
Oh it's used by composite not effector, I'm sorry
Yes
how do I delete all the children objects of a parent gameobject?
Unity is the ultimate game development platform. Use Unity to build high-quality 3D and 2D games, deploy them across mobile, desktop, VR/AR, consoles or the Web, and connect with loyal and enthusiastic players and customers.
Googleable
physics material 2d @still tendon
loops froze unity once
what does foreach do?
it sees each object in something?
it gets each transform that the current transform contains?
i like that
in this case, each transform would be the children
yeah\
also I have a List<GameObject> of all the objects
can I iterate through that instead?
it's easier if you name 'em like this:
foreach(Transform child in parent)
{
Destroy(child.gameObject);
}
thank you for your help
No idea, I haven't used physics material in awhile
that works too if you want to delete each object of the list and not its child
each object in the list is the child
Then that works
By lag do you mean it freezes when you shoot? It could be that you are instantiating new objects every time you do it, which can be resource intensive
If that is the case, look into object pooling, it will be a big performance boost
actually, i built an fps tracker and discovered something
my fps is amazing, and the real issue is that my movement scripts are highly-dependent on frames
they change at tiny amounts, so if I lose about 30 frames, it looks like I'm getting severe lag
I create objects on level start and destroy them on level over
I'll try to use timers instead of changing a set value per frame
can i suggest using FixedUpdate for physics or thing not frame dependant
and Update() for inputs
sure np, actually fixedupdate lets you not use Time.deltaTime
because you are already using time
and with Time.scale =0 it pauses fixedupdate
I have updated all of my movement scripts and made them time-oriented
everything moves at a set speed ๐
i made it 210fps based
so if the game is running at 210fps it's going to move at the amount that the script says
you should make it so that objects move the same no matter what the framerate is.
thats what i did
so what do you mean by "210fps based"
i meant by the 210fps based that
lol
that if its running lower than 210fps it's gonna look a bit chunkier but it's going to move at the same speed
so if its running at 200fps its going to be a tiny difference of chunkiness
Most people don't have monitors that can even draw more frequently than 144 or 60 times per second
or 60fps is gonna suck a bit
oh
but my game runs at around 200-300fps
so it renders about every 3 frames?
on your hardware it might, but your monitor can most likely not even draw that many frames
I don't doubt it
i guess it's because I use small numbers
idk
even if I made it 60fps based it would render the same
well i guess its nice if the game runs faster than 60fps
210fps based, 60fps based? What does that mean
basically if the game runs at 210fps exactly the amount my object would move per frame would be exactly the amount I put it to, the same amount that is multiplied by Time.deltaTime
example:
oh i meant the amount divided by 210
so 31.5f / 210 * (speed / 75f) is how much the y position would change here per frame if it was running at 210fps constantly
if it runs at, say, 30fps the movement would be a lot chunkier but would move the same speed
basically 210fps-based is a term meaning I have to change up the numbers in every movement script I make so that it fits that level of smoothness
i can always make a script that goes on 100fps but that depends on me
Why don't you just make it something sensible like "N meters per second"
Then you just do speed * Time.deltaTime
yeah
and no magic numbers required
what are magic numbers
31.5 and 75 in your screenshot
oh those
i cant make a new variable because right next to this are other ones that go by 10.5f * (speed / 75f) and stuff
also (speed / 75f) is used because my speed will either be 75f or 25f in this game
if its 25f I want this movement script to catch it and move 3x slower
and I use the same speed variable so I don't have to make new variables like velocity = (1/3) to represent 3x slower
Heeey, so I have a Box Collider 2D component set to trigger, but it doesn't actually seem to detect anything at all
Anyone have any ideas?
Intersecting object has a rigidbody, and is on the same layer
Woops, figured it out.
Was supposed to be OnTriggerEnter2D rather than OnTriggerEnter
๐
He made a prefab, which is an object that's easy to make copies of and quickly alter. Or if it's an animation, he made a .anim file, which can easily be done by dragging a bunch of images to hierarchy
which can also be easily be used as a variable
you make things in the scene
and to save that thing, you prefab it
Yes
Well one way. theres probably more. like generating prefabs via editor scripts
But thats more advanced. the way he did it is the normal way
What do you mean
instantiate them as a child
i'd just make a bomb gameobject, add a public Sprite[] array, add them in the inspector window's script component, and iterate through them in the script
i don't know what you're actually doing. Childed objects inherit the transform animations of the parent
@still tendon Are you having trouble doing what he's doing?
Basically what he's doing is making the explosion a different prefab
That way he can have the bomb create the explosion
This is helpful for a few reasons
First: you don't have to tell the bomb how the explosion actually works
And second: you can have the explosion spawned by other things, like explosive barrels or something
I have to watch the video to understand exactly
What do you want the bombs to do?
Like, what is the animation?
Is that in the video?
Oh okay
You can animate the bomb, you just need to have an animator component and everything
On the prefab
Yeah
Let me know if you need help, animator can be tricky if you don't know what you're working with
:]
A question for ya guys
How would you generally do hurtboxes?
Do you turn the trigger collider on and off?
Or do you do something else?
I have a collider I'm trying to use as a hurtbox by turning it off and on at will, but it only detects OnTriggerEnter the first time it's enabled
If the thing is enabled, then disabled, then enabled again, it only calls OnTriggerEnter once
does the colliding object leave the hurtbox after the first time, and re-enter it?
Nope!
So the problem would be that you can't really hit a stationary object
Which is annoying
if you also call OnTriggerStay, it will run more
until it leaves
and if it leaves, you can also can OnTriggerExit
also, if you turn off the Trigger option, you can also use the functions:
OnCollisionEnter
OnCollisionStay
OnCollisionExit
I guess you realized your mistake eh? ๐ฆ
Actually no
I was gonna move this to #archived-code-general or somethin
Okay did that
:]
If you have a rule tile which generates a gameobject wherever you place it, and you destroy the game object in the middle of the game, does it delete that tile as well?
no, but the game object will (obviously) no longer be attached to the tile
Hm okay
How would I destroy the tile that the game object is one? Taking its position and the round it down to the next lower integer and then deleting the tile at that position?
a kinda hacky way that i use is to just put the sprites onto the game object and turn off the tilemap renderer for the layer that the rule tiles are one. Then the tiles won't show, but the objects attached to them appear to be the tile. That way if you destroy one, it doesn't look like it is still there even
That does of course mean that those tiles will need to be on a separate tilemap layer for it to work as expected
Hm Im not sure if I want to do that. Its already annoying with two tilemaps as somehow the pallet always manages to draw on the tilemap I dont want to draw on at that point and I imagine it would be even worse with three tilemaps
Would this also work if I want to limit the tilemaps to as few as possible?
How can i AddVelocity to object that doesnt care about direction it will just the player to that direction
What is actually happening: it wont launch up it will launch to opposite or other direction
My code: https://gdl.space/imahupeyud.cs
If you're not going to look at responses that you get when posting in other channels, then don't post.
#๐ปโcode-beginner message
did it not working
That's so vague to the point I think you're just saying that to claim you tried.
@tepid drum show what you tried, the concept itself will work
I tried to remake dani game called balls
Thats not what I mean, I mean what you tried related to this concept
@tepid drum if you want help, you have to provide this information
What does the second line in here work? I am having some problems as it sets the x velocity to a set amount each frame, but I want to change it.
Vector3 targetVelocity = new Vector2(move * 10f, m_Rigidbody2D.velocity.y);
m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity, targetVelocity, ref m_Velocity, m_MovementSmoothing);
hi so im currently making a cutscene and i want the material of my scene to change half way through how would i do something like that
Are you using Timeline or an Animator?
should be able to keyframe that material in either of those I think?
maybe share your code?
none of this code rotates anything
you said your problem is your character is looking up
so I was looking for code that rotates the character
So sounds like your problem is in your animator I guess
Might be worth asking in #๐โanimation
hello! im following some different tutorials and im trying to make a simple 2d game and at the player controller part im kinda stuck my character doesnt jump and idk if the grouncheck that i placed is good or not or if the code is badly done
Im using timeline and that doesnโt work because if i change the material it will change it for the whole timeline and not the one bit
Hi, i need 2 box colliders on my sprite (one at the feet, and one for the body) but how can i reference these both in the script as GetComponant<BoxCollider2D>() doesnt specify which one
drag and drop them into two different serialized fields (or an array)
it's also a bit more manageable if you put the colliders on two separate child GameObjects
Or use GetComponents<BoxCollider2D>() which returns an array of all of them
hm okay
i managed to get it working with an edge collider at the feet and box collider on body so that i cant wall jump when hugging the wall
but now if i clip from the underside of the box collider i cant jump or fall down
unless if i moved the side points of the edge collider to be like .1 inwards from the sides of the box collider making it an almost impossible occurance of the box collider being collided with from undernearth
but that seems a very cheated way to fix
I have a boxcollider and it was previously set to a small sprite, but when I changed it to a larger sprite the boxcollider didn't change. I don't even remember changing it to that specific sprite. Is there a way to update my boxcollider?
there's an edit button on the component in the inspector
oh i just realized
i made a formula to calculate boxcollider's extents using size
nvm i accidentally accessed boxcollider
thank you for replying
i was taking a break but because of you I got back to focus and solved the problem
yes
You can add 2 params for the last x and y input
Hi, I'm having trouble with scaling with screen sizes (in a 2D game). I would like to have the whole screen (not just the canvas) match the width/height of the screen (whichever is smaller), and the height/width to scale accordingly. I'm hoping this will cause the dimensions to be exact, which is important as it is a puzzle game. Is there any way to program this, or otherwise change a setting? Thank you in advance!
idk if this is a stupid question but how do u go about connecting animations like in my case qb throws ball reciever catches it
I guess itโs a 2d game if posted hereโฆ you sort of fake it by making animation of holding a ball. Not sure if you can use IK in 2D
so like make seperate animation with him catching then add ball
that feels like how u get ball teleporting
You just deactivate gameobject (ball) when itโs close enough
I donโt know what game youโre making and if itโs a good a solution for you
But it is a solution
You can use IK in 2D with Unity's 2D animation stuff
is this what you mean? If not, reply to this message to specify the details of your issue.
If you want camera to display more, you can adjust its orthographicSize variable in inspector or in your code. The orthographicSize of a camera is half its height, and the size of the width depends on your screen display settings as shown above.
What is IK anyways
Inverse kinematics
I canโt create new palette in tilemap. Click create but nothing changes?
I've made a post in the forums since my question its a bit large: https://answers.unity.com/questions/1855572/how-do-a-entire-grid-checks-if-one-possition-its-c.html
You're all over this discord. This isn't a 2D code question either.
Read the limitations
https://docs.unity3d.com/Manual/class-MeshCollider.html
hi so my code works but it also doesn't because i want my player to walk into the trigger collider to enable the textbox but then when they press space it disappears however if they walk back again i dont want the text box to reappear but i dont know where else to put the if statement other than the update function
if the trigger is no longer needed at that point, just disable the component
how do i do that
I have this code in my player movement script:
Vector3 targetVelocity = new Vector2(move * 10f, m_Rigidbody2D.velocity.y);
m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity, targetVelocity, ref m_Velocity, m_MovementSmoothing);
And the problem is, that it sets the velocity every time it gets executed to a certain value. I want the player to move with another platforms, if he is standing on it. Since the velocity gets set to a certain value every fixed update, the way I would normally do it, by setting the player to be a child object of the platform, doesnt work. I tried adding the velocity of the platform to the velocity of the player each fixed update, but for some reason that makes the player always have an a lot higher velocity. Does anyone know why?
@compact knoll
just getcomponent then set enabled to false for whichever component you want to disable
that's for a game object not a component
Oh wait are we talking about a component here? My bad, sorry
how are you moving the platform?
yeah idk how to do that cause im dumb af
I mean if that works, why not use it? or does the platform actually need correct physics for something?
or if you meant using movetowards for player movement in which case ignore that
It does need physics, as I want the player to be pushed of if the platform goes under a wall, and that looks relatively with setting the position
your platform doesn't needs physics for the player to be pushed off, just the player needs physics for that
Hm but the player seems to always be pushed into the ground a bit since the platform directly sets the position and then the physics push it out again
I think we may be on different pages here, but i did find this simple solution
https://www.monkeykidgc.com/2021/03/unity-moving-platform.html
Does it say anything about the player movement code anywere, or do I just go to the point called "the hard way"?
yeah i still cant get it to work
use the "hard way" method, it's probably better than the first. but it does show you how it moves the character too
you're going to need to be specific
Hm looking at it, the first code block shown under that point is exactly what I tried to resolve my error, but it resulted into the error of the player always having a higher velocity than the platform, even when he isnt moving at all
boxCollider is a game object, not a component
soooooo
if you are attempting to disable a Box Collider component, then your boxCollider needs to be of type BoxCollider2D not GameObject
im very new to c# so idk what u mean
did you make sure to give your platform friction?
Oh, I dont think I did, why is this necessary? I thought you could do either one of the two things. And will it also work if the player has a friction of 0?
prevents the player object from sliding too much. whoever wrote the guide i linked said a friction value of 7 did the trick for them
i haven't actually tested it myself though so i can't confirm
Yeah I guess I have to try that. I just thought that it wouldnt slide if it has the same velocity as the platform
wait also how would i make a public string be able to use more than one line in unity
ok nice that works thanks
no how would i disable a whole script
wait no
how would i disable one script in another script
Get a reference to the script, and set it as enabled = false;
Though, scripts should rarely have the authority to control other scripts in that manner.
hi anyone knows how to make this? i want to make a top down player that follows mouse , with something like Vector2.MoveTowards , the problem is i want it to be smooth like lets say if there is a wall on top of the player and the mouse clicking on the topside as well i want it to check the collision on top and if there is a wall there , then just ignore the Y input of the mouse and only do the X input , so it should not get stuck on the wall
i tried ray casting but it somehow doesn't work with this type of movement and get stuck on the wall
how do i get a referance
Have you tried pathfinding?
I mean itโs what you tried
But A* project
Can work
not pathfinding but i already wrote the code
(ignore that i forgot to change the last 4 playerX to playerY)
the only problem is i don't know how to write this function called place_free to find if the position is free (no collision)
hi so im using brackeys topdown movement and animation system and when a text box appears i dont want to be able to move so i disable the movement script but when that happens my guy will carry on doing the running animation
stop the running animation in OnDisable()
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnDisable.html
yeah that dont work
i still need help
show some code, you're in a code channel @hot fog We cant help if we dont know what you're doing
bear in mind that the animator is on a different gameobject to the script
Do you not see the red errors in your script?
Just curious why your initial question wasn't regarding them. When something doesn't work, it usually means you have bad code, so that's usually what you ask about.
First off, you need to provide a type to your variable. Notice how all your variables have three parts but your animator one only has two.
Sorry for taking so long, Ive been gone for a while, but I tested a lot of things and the reason for it not working, seems to be that the rigidbody shows that the player has a velocity even when he doesnt, which makes the adding the velocity of the platform to the velocity of the player have a higher velocity as far as I can tell.
I got this error while trying out stuff, what does it mean?
can I change the layer of a gameobject during runtime?
yes
why lol
there are some times I want an object to collide with it, and sometimes I want it to pass through
You can always look at the documentation for this kind of question btw:
https://docs.unity3d.com/ScriptReference/GameObject.html
im just trying to have something move
i justw ant to move the objectt
why is this so hard
i tried using sprite
BoxCollider2D
none of them have a way to actually move their position i dont understand
this doesnt work either
Perhaps look at the error message and think about why it's giving you the error
You should really do some basic tutorials because you're way off the mark here
There's a lot wrong with your script. Better to follow a tutorial
i feel like crying
- Can't have public variables inside a method
- BoxCollider2Ds don't have
velocity - Even if they did you need a reference to an actual instance to use its methods/properties
ive done tutorials before i dont know what is happening
go back
do more
you didn't learn
try to understand what's happening in the tutorials as you do them
don't just blindly copy
I HAVE BEEN TRYING TO UNDERSTAND
slow down
what is the object that the script is attatched to CALLED
i want to change its position
thats all im trying to do
i called the object goomba
but that doesnt seem to work either
it doesn't matter what the name of the obejct is
what is the thing that has a position for me to change
you can access the GameObject that your script is attached to simply with gameObject
or its Transform simply with transform
the thing with the position is the Transform
small t
Just transform
no GameObject.
No capital T
and if you want to change the position you need to do transform.position = something;
(gameObject should work, but its redundant)
because the Transform keeps track of position, rotation, and scale and other things too
Sorry ive been at work and yeah no i had it as public Animator animator; and it still didnt work
that's correct, but it seems like you have other errors too
calm down a bit
go through the errors one by one
the errors only help if i know what im doing
which i dont
which is why im asking for help
because the tutorials didnt teach me anything
you're a bit scattershot is all
go through errors one by one
if you don't understand them
feel free to ask
its not teling me what options i have within transform.position
do i need to ()
or .
no
just cs transform.position = transform.position + Vector3.right * Time.deltaTime; for example
or transform.position = new Vector3(1, 2, 3);
ยฏ_(ใ)_/ยฏ
you can assign it to anything you want
im not allowed to access movespeed from update but it freaks out when i make the float public
it will work, it's pretty weird though
ok
i dont like making too many big variables like gameobject variables
or variables i wont use that much
MoveSpeed only exists in Start(), making it practically useless. You can still make it private float MoveSpeed, outside of the start and update functions
i guess i'll have to make a new gameobject variable ig
you have to declare MoveSpeed outside of Start()
or is there a way to go from script to parent of script?
dont you initialize variables when the object is created though? why cant you do it in start??
you can't declare public variables in any method
you can initialize it in start if you want. but you have to declare it outside of start
well what do i do if i want it to be set once and not set again unless i change it
the object whose layer I'm changing is the one with the script changing it. Can I access the parent gameobject from the script without doing something like GameObject.Find();
see where im from
if you set a variable that doesnt exist, that is declaring it
yes. Just gameObject.layer = whatever; for the object with the script
or transform.parent.gameObject.layer = whatever; for the parent
C# isn't like that
you have to declare your variables
god if moving is going to be this complicated id hate to think how much harder this is going to get
I have an object called object1, object1 has a script called script1
script1 tries to change object1's layer but has to access it using GameObject.Find();
script1 wants a cleaner, more lightweight way to access its parent object. How can script1 do this?
Movement is probably one of the hardest aspects of a game, to get it to feel good and work well with your design.
So if you can nail that down, you're in a good spot.
one of my many scripts
GOOD mevement is hard
having movement that exists at all shouldnt be
It's hard only because you aren't familiar with Unity or the programming language yet
Once you are familiar, it's a breeze
^
once you get the basics down, you'll be able to tackle more complicated concepts in unity
and make your game richer
alright it works
but i want to add this stuff to the position rather than setting it
but "+=" isnt allowed
can I go from a script to its parent gameobject without finding the gameobject
I just said
you're replying to the code of how to do that
+= is allowed
you can't add a vector2 to a vector3
it doesnt let me
what happens
Oh it's because you're iusing Vector2.right instead of Vector3.right
well its a 2d game
doens't matter
why doesnt it matter
everything in Unity is 3d
this is important
there are no 2d games in Unity
wait
only 3d games with an orthographic camera and a side-on camera angle
is vector 3 is required, why does vectore 2 EXIST
transform.position is a Vector3
aren't transform and layer on the same level?
there are other, uglier ways of doing it where you can keep Vector2
gameObject.transform and gameObject.layer
if you really want to use those
vector3 isn't required, there just are various uses for Vector2/Vector3
but I wouldn't
vector2 is a datatype that contains two floats
I like using it to get my boxcollider's x and y extents
or for seeing how far a gameobject is from the other
the reason you're getting an error is because your code is essentially adding a Vector2 and a Vector3 and it doesn't know if it should convert the Vector3 to a Vector2 and then add, or if it should convert the Vector2 to a Vector3 and then add them
it's not about Vector2 "not being allowed"
so it's easier to just resolve the confusion by only using Vector3 in this case
and why is this information made clear
?
its clear if you are familiar with types
C#'s compiler error messages assume you are familiar with the workings of C#
float
ik
in fact vector1 does exist in many languages, especially shader languages
C# isnt really the hard part for me
the issue i have is unitys functions themselves
i never know what to use
That's what the scripting reference is for
but the error you were just struggling with is a C# thing not a Unity thing
i fixed it by this point
and the "scripting reference" doesnt help me find what im looking for
i need to know what im looking for in order to search for it
Ah, sorry for being unclear.
What I would like is for all screen sizes to show all four sides of the white border (16:10 and Free Aspect do this correctly) but the others do not. Is there a way to make sure that instead of matching the height every time, it matches the width if the width is smaller?
and tutorials
you're not going to just know everything right away
googling isnt always as magical as people say
your code needs to check if the aspect ratio is > 1 or < 1
Oh, that should work! So the way I would do that is
aspectRatio = screen.width / screen.height;
correct?
yeah
Alright, thank you!
or just check if Screen.width > screen.height
googling is a skill in of itself, as you get more experienced you learn what terms to google
you might have to cast one of them to (float) or you'll get integer division too
If I have two trigger colliders, how can I define which trigger collider is which when calling something like OnTriggerEnter2D ?
Check tags
Huh?
I mean if I have two trigger colliders on the same object
One for detecting vertical and one for detecting horizontal
How can I tell the script "if the horizontal trigger collider is triggered..."
you put one of the colliders on a child object
For this reason It's best practice to put each collider on its own separate GameObject
That's what I'm doing but it seems like it's just pulling info from the other trigger collider anyways
Maybe I'm screwing something up
you need to put each collider on a separate object, and have OnTriggerXXX scripts on each separate collider object
if you want to detect the individual triggers
if there's a script on the parent object, it will detect collider events from any child colliders
they should be siblings
Right so I have a parent with two children both with their own trigger colliders and scripts
Oh well, guess I'll just keep looking for what the bug is
Haha
I thought it might've been that the script is pulling info from the wrong collider
For some reason I can't get the walls above or below to register collision
Something weird is happening

OH wait duh
I recoded this and forgot to take out a condition
Disregard
I'm trying to make it so that when I enter a house trigger the roof tiles become transparent
I know that you can set individual tile's colors through their position, but how could I identify where all of the roof tiles are to mark them all transparent?
i am already burnt out and ive been working for 3 hours
but if i dont do this again i'll forget everything
any pointers on how to go about making a replay system that captures how the player dies and then save it to disk where if needed it can be viewed from the main menu?
Hello, I have tilemap and I want to simulate item drop "bounce" effect (like in stardew valley when you drop items and they bounce a bit). But the issues is how should object bounce of tilemap, it just falls right thru it... Any ideas?
I have a script, it's attached to a gameobject and I want to modify that gameobject's layer without using a GameObject variable or calling GameObject.Find().gameObject.layer
How can I do this?
gameObject.layer```
yes
that's the only place you can
tysm!
otherwise you need a reference
my life is 100x easier now
if I have a OnCollisionEnter2D function, can I call it inside the script?
you can call it but it wouldn't make sense to
Unity calls it for you when a collision happens
i would call it if an object moves too much due to its velocity and skips over an object
i set it to be on top of the object, but it might not call until next frame so I'd just call it to get it over with
how do I determine the collision
like what should I put for the parameters
i'll just copy and paste the action
you wouldn't
it doesn't make sense to do this.
ik i just like exploring possibilities
I'm calling Random.Range(0, 1) but it never gives me 1. Why is that?
seriously, I even called it 6 times
doing a second test run on that
like six times at a time
wait actually the six times got called 26 times
Random.Range is exclusive on the upper bound
well documented too
0, 1 will only give you 0
you have to do Random.Range(0, 2) if you want 0 or 1
but when using Random.Range with floats it is inclusive
Documented... in the docs: https://docs.unity3d.com/ScriptReference/Color.HSVToRGB.html
Hello, I'm trying to implement a system where the player can illuminate the "closest" block to the light source, but then that block will cast shadows on ones behind it. Does anyone know how I could go about implementing this in a shader? It'd be nice to be able to pull the light level from the base, and then project it upwards.
Ok so im using brackeys 2d topdown movement tutorial but the thing is if i press D to move right when i let go it will return to idle front instead of idle right
How do i change this
This is for a flat golf game (Top down i think theyre called)
how can i add friction within this code (Changing the velocity every frame)
hey! so I am using a 2d mouse parallax script for some ui
it seems to work on scene view but it doesnt load on the game
What a weird thing to print to your console.
O_O you didnt see that
Right.
Well first off, you're applying a new position to the gameobject twice, not sure why.
Secondly, what's the position of your camera?
o_o
1 sec recompiling >_>
i just noticed tho that when i hover the bottom left colour it does appear massive on the right side
camera is at 0 1 0
You're setting the canvas to z:0 as well, which is exactly on the same plane as the camera.
Either move the camera back, or put the canvas forward a bit.
Or rather, use the z-value the canvas has in the editor you currently have it set to.
Since that seems to be the distance you want it to be, anyway.
i tried setting it to the canvas (90) and it is the same thing
im trying the game object that they are currently in
It's whatever object your ParallaxUI script is on.
Remove your gameObject.transform.position line all together, it's not doing anything useful.
Then add pz.z to the third parameter to your Vector3.
This won't fix it, but at the very least it'll clean up your code a bit.
In your above screenshot, where is your camera?
third parameter intead of 0 correct
apologies if these are simple answers, im just a lowly artist haha
guys im using brackeys top down 2d system but whenever i stop running it will go to idle down instead of idle the way i was running how do i change this
Let's move this to #๐ฒโui-ux. But if you disable your parallax script, and then manually move the canvas around, can you get it to appear?
oki
Can a coroutine start a coroutine?
Yep
You can even yield to other coroutines to wait for them to finish before the code inside the previous coroutine continues.
Useful if you need to queue things sequentially, such as in a turn based game.
Okie doke, cool
Trying to figure out why a coroutine is being called in Unity but not in the build
Okay the coroutine is being called
But the result is different

In Build:
In Unity:
could be you have code that is framerate-dependent. In other words your game behaves differently depending on the current framerate
builds tend to have higher framerates because they don't have the overhead of the Unity editor itself
Ooooh interesting, that makes sense
The code that raises and lowers the sprite happens on every frame
That's probably exactly what's going on
yep
sounds like it
You can generally multiply things by Time.deltaTime to convert things that are "per frame" to be "per second" instead
which is the typical way to achieve framerate independence
Yep that fixed it. :]
Also, how come I can't use Cursor.visible?
Was it changed?
wdym?
what happens when you try to use it
did you name something else Cursor?
... ye
Haha
Crap
Alright well figured it out ๐
Literally the most helpful community I've ever come across, good stuff guys

seems like you're trying to access something called IsGrounded on an Animator.
Animator doesn't have a member called IsGrounded
so naturally it doesn't work
this is kind of like asking someone to show you the wheels on their surfboard
So I need to create an IsGrounded boolean inside of my code?
And wow you type fast, what wpm if I may ask?
I didn't say that.
It depends on what you're actually trying to do
Maybe you're just trying to get IsGrounded from the wrong variable or something
What is your script called?
Player Script
also can you show line 37 of your script?
that's the one that has the error
doesn't seem like it would cause the error you're seeing
weird
would you mind showing some more context
maybe just the whole script
also make 100% sure you've saved any changes you've made to your script
#854851968446365696 has some tips for how to share your code here
if you don't follow it the bot will delete your code
the code looks fine to me
do me a favor - select your script in the project window and look at the text preview that it shows in the inspector. Does the code look like it matches what you've sent me here? @formal smelt
no this looks very different
wuts going on
see wheerr it says anim.IsGrounded
yea
Double click this script in the project window and see what opens up
I would guess you have multiple duplicate scripts by accident or something
idk
Press this Open button
ok
and what does that open?
same code as the link
can you show your console window? DO you have any other errors besides the one you shared?
FOr some reason Unity doesn't seem to be getting the up to date code from your file.
Also you can try restarting Unity.
try restarting Unity ๐ค
i did right before i sent u that
also triple check that the file was saved in Visual Studio
Make sure there's no asterisk like this next to the filename
I've had bugs where ctrl+s in VS doesn't actually save the file for whatever reason
i did have that bug apparently
I just manually saved
now I can run the game but i get this error
you haven't assigned one of your variables in the inspector then
yeah
click on your player object
in the hierarchy window
the look at the inspector
you should see your script there, attached to the player
there are slots on it
you didn't drag the animator into its slot
hierarchy window?
oo
see how those all say none
also you have multiple copies of your Player Script on the player object
you should get rid of all but one of them
i have 3 objects in my game all with the same script getting sprites from a sprite atlas stored in a singleton "GameAssetManager"
Debug.Log(GameAssetManager.instance);
_sprites[0] = GameAssetManager.instance.entityAtlas.GetSprite("entities_" + spriteID);
2 of these objects find their sprites perfectly fine, but one cant seem to find the asset manager
even if I duplicate one of the working ones it cant find it
can someone please tell me what went wrong?
ok now i restarted unity none of them work rip
which line is 25
sprites[0] = GameAssetManager.instance.entityAtlas.GetSprite("entities" + spriteID);
Oh I know the problem
dont need to show the script
you are running this code in Awake right?
yep
and in GameAssetMaanger, you're assigning instance = this in Awake()
yep
if one of your Animator scripts runs its Awake before GameAssetManager runs its Awake
instance isn't ready yet
makes sense
try moving this code to Start()
I am adding speed to my character with this line of code, but my character gets stuck on a flat tilemap randomly
rigidbody2D.velocity = new Vector2(Input.GetAxis("Horizontal") * speed, rigidbody2D.velocity.y);
Can you show a screenshot of what you mean with the tilemap?
Im trying to modify these two lines of code:
Vector3 targetVelocity = new Vector2(move * 10f, m_Rigidbody2D.velocity.y);
m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity, targetVelocity, ref m_Velocity, m_MovementSmoothing);
And make it so that it doesnt take the entire x velocity of the player and changes it, but only the velocity that was added through this line.
not sure what you mean. Can you explain a bit more?
Currently when I add any velocity to the object it instantly gets damped down, but I only want this to damp the velocity that was added through this movement script
Is that understandable?
You are overwriting the velocity right now. You should add it instead, I guess, if I understand correctly
Anyone have and idea on how I could find the position of the players mouse in 2d?
Camera.ScreenToWorldPoint(Input.mousePosition)
mouse position is always 2D, so Input.mousePosition tells you the screen position
there you go
Thanks
I only want to overwrite a a specific part
I only want to overwrite the part of the velocity that is added by this script. tried storing the velocity in a variable, but it works until I walk against a wall, since then the velocity gets reduced
Okay, so right now your velocity is overwritten, what "part" do you want to overwrite instead of the whole velocity?
The whole velocity I do here, show be a different "part".
So instead of damping the whole velocity it damps the velocity that was added in here
Vector3 targetVelocity = new Vector2(move * 10f, m_Rigidbody2D.velocity.y);
m_tempVelocity = Vector3.SmoothDamp(new Vector2(lastVelocityX, m_Rigidbody2D.velocity.y), targetVelocity, ref m_Velocity, m_MovementSmoothing);
m_Rigidbody2D.velocity = new Vector2 (m_Rigidbody2D.velocity.x - lastVelocityX + m_tempVelocity.x , m_Rigidbody2D.velocity.y);
lastVelocityX = m_tempVelocity.x;
This is what I tried. I made a variable called lastVelocityX which always stores the velocity added by the script last time and takes that as the current velocity to damp from. It works until the player encounters anything that changes its velocity e.g.:
slopes, walls or custom abilities
@craggy kite do you have any idea? Ive been stuck on this for a while and I have no idea how I could do it
I am a bit sick and not in the state to udnerstand fully what you want, sorry ๐ so you are trying to damp down the velocity change instead of the velocity
Oh okay, of course you dont have to answer, especially when you are sick. But yes Im trying to only damp the velocity that is responsible for the movement and was added there, and not any velocity that was added through and other objects or scripts
so did you try to just add it to the last velocity of your rigidbody? Can you maybe break it down into steps what you want? 1) get the velocity of the player, 2) add velocity to it through whatever force 3) damp that added force 4) attach it to the original velocity of the player?
-
Determine what part of the velocity was added through the movement script and not any other script.
-
SmoothDamp the velocity determined in step 1.
-
Overwrite the velocity from step 1 with the velocity of step 2 while leaving the rest of the velocity intact
@craggy kite
Please dont mention me all the time ๐ I come here when I got time. So what two vectors do you want to smoothdamp? The lastAdded to the new added Velocity?
whenever my character moves left or right when it stops moving it will return to player idle down instead of player idle the way it was running. how do i fix this
You only have one player idle animation which the movement returns to.
Where is playeridledown?
yeah ik but idk how to add my others in
drag drop them in the animator window
ok now what
Im not an interactive tutorial ๐
do i need to make a blend tree for them like my movement
Blend trees aren't really ideal for 2D animations.
You better check the tutorials on animator on youtube, there are dozens
i have its literally a straight copy of brackeys 2d topdown movement but he leaves it like that
at least for sprite animations, bones is another story
Right, 2D sprite animations. *
