#💻┃code-beginner
1 messages · Page 652 of 1
did what thing ?
your TimerScript doesn't seem to exist
referencing another script
where is TimerScript ?
yea thats what i dont get cos its litteraly right there at the top but its saying its not
referencing something that doesn't exist doesn't make sense so its telling you it doesn't exist lol
declaring a variable has nothing to do with a type existing
(also if it doesn't exist in your project it's certainly not going to be attatched to an object in your scene)
its like you trying to use a word in autocorrect that doesnt exist in the dictionary
when you declare its just saying " I want to store a TimerScript here "
computer is saying "What is TimerScript? I have no idea"
ive just checked and bothe scrips are working
what is "both scripts"
we dont see your setup
show TimerScript.cs
see how its defined as Timer ?
the filename is completely unrelated to whats defined inside
ah is it because i renamed it?
yes renaming the file does nothing to whats inside
you are trying to use GetComponent on an array
it would not make much sense to do so as an array has no such method
only components have GetComponent methods on them
You probably want the Singular version GetObjectWithTag not Objects
and GameObject
also if you just want that 1 script you can just link it in the inspector no?
ngl i didnt realise i said plural mb
wdym im new
[SerializeField] TimerScript timer
then drag n drop
you already used it for TMP I see
would it still work when the timer script is linked to another game object
ofcourse
as long as they are part of the same scene
other methods otherwise you can check link above it has alternatives if you have to find runtime / spawned objects or referencing a scene object from prefab
ive sorted all the syntax errors but now i can get this to work
the timer reaches 0 and the character is still there
these dont give much context to where this line was placed and whether or not its running
you are keeping characterAlive bool track in 1 sript, and then another script, just because they share the name they are NOT the same at all
each script has its own copy of the bool
is there away to make them share it
several ways
do i just reference the script i want the bool for before hand
if (TimerScript.characterAlive == false)?
the cleanest way would be to reference the timer script and only follow the timer from there
a timer shouldn't really care or track a characterAliveBool
the timer is only a timer, thats all it should do..track the time
if the player cares about the timer going to 0 then you follow the currentTime from the player
and logic from there
i have it so when the timer runs down to 0 it kills the characted
mhm I understand what the usecase is
but if theres another way for it to do the exact same thing?
what do you mean , i just told you how
track the value of currentTime there or use a bool from timer
eg
if(timerScript.isTimerOver)
//do stuff
ideally you use events but here its okay to use Update since you're new
so you switch up the bool in the TimerScript from characterAlive to IsTimerOver or whatever
then in player update you check the value
so scripts share data types exept bool?
scripts only share something public when you access it
they are not global values or anything
thats why you have to make a reference to it
the datatype is irrelevant
i think what im strugaling with is my lack of knowlege on referencing
This is another project where i tried the same thing and it worked
But it doesent work here
because in the first one you are using a variable that happened to have the same name as the class. the second you are just directly using the class instead of your variable
you should consider stopping what you are doing and go through the beginner c# courses pinned in this channel to get a better grasp of the basics instead of trying to brute force it
<access modifier> <class name> <variable name>
And note you are using JumboJoe in the code, not JoeScript:
this is correct, you are using the variable name correctly.
In the other one, you are trying to use the type name.
do i need to multiply my force vector which i am passing in rigidbody.addforce method by time.deltatime if the function is being called in fixed update?
no, you should not be multiplying your forces by deltaTime. The forcemode you choose will take that into account if necessary
and you should also never be adding force in Update with the exception of one-off forces, they should always be done in FixedUpdate to ensure they are consistent (especially considering the rigidbody is only ever updated on FixedUpdate frames anyway)
i'm handling a groundcheck using this method, however it ALWAYS returns false! (like the player is in the air)
if (_jumpAction.WasPressedThisFrame()) {
if (Physics.SphereCast(groundCheck.position, 0.5f, Vector3.down, out RaycastHit hit, 0.5f, playerLayer)) {
rb.linearVelocity = new Vector3(rb.linearVelocity.x, jumpForce, rb.linearVelocity.z);
}
}
edit i solved it by using checksphere!
Thank you, I'll move it there.
Hello not sure if this is the right place to ask but
I have a question on how to keep on track of the scripts you make,
are there any good standards on how to make memos of how each scripts connects and works?
cuz I have like 10 C# scripts in my projects & it's getting hard to get back on track whenever I come back after a break
sort them in folders
however you like
here's an example of mine, but it's always up to your preference
What is playerLayer and is the ground on that layer?
yes, i already fixed the issue
i decided to use CheckSphere instead
Ah it was edited as I hit enter
oof but thanks anyway
if (_jumpAction.WasPressedThisFrame()) {
Debug.Log($"Jump was pressed", this);
if (Physics.SphereCast(groundCheck.position, 0.5f, Vector3.down, out RaycastHit hit, 0.5f, playerLayer)) {
rb.linearVelocity = new Vector3(rb.linearVelocity.x, jumpForce, rb.linearVelocity.z);
Debug.Log($"Character had jump", this);
}
}```
bool isGrounded = Physics.CheckSphere(groundCheck.position, 0.5f, mask);
if (_jumpAction.WasPressedThisFrame() && isGrounded) {
rb.linearVelocity = new Vector3(rb.linearVelocity.x, jumpForce, rb.linearVelocity.z);
}
this was my new solution (yes i know i can put it directly into the statement but i reuse it directly after)
I wouldnt really use that for ground checking logic honestly. Unless you're confident this is as far as you'll need, and not deal with stuff like jumping because a wall is beside you
nope, it's literally a walk around the place game
I meant that because your current logic would let you jump if there was a wall beside you
Theres no way of checking what the slope of the ground is here
Actually I meant like how to keep a document on how the script works
like ex.)EnemyManager: Spawns 1 enemy every 10 sec + Calls someGameObject,
PlayerHP: Spawns HP UI, if hp0 = play animator "dead"
All that kind of information, I was wondering if there's an easy way to keep a memo of all of em/or
like make it easy to read for other devs to get in the project too
comments
// or /* */ for comment blocks
/*
* HEADER
* this code does x
* this code does y
* changing z modifies w
*/```
true
i added your log statements to my original problematic code, it knows jump was pressed but never acknowledged the player's ability to jump
You would usually organize the code into folders already to what topic its solving. Thatd narrow down a lot for other devs. Comments like this could go at the top of the class or above the relevant method names but the examples you gave would probably be unnecessary comments.
this ^^, if your code is readable enough developers will quite likely just understand it from the get-go
Usually your files shouldnt be that large, so it wouldnt really take much for a dev to skim through it and have a basic idea of what it's doing. Comments are there to explain what isnt easily understood. Code should be written in a way in that its understood immediately
And when organizing the code into folders based on what its solving, there wont be that many classes to go through and understand.
There isnt really a built in tool otherwise to solve what you're describing if you're trying to get a dev caught up on the whole project. Youd likely just want to make some basic diagram
I'll take a more look on how to sort my folders, I did sort them in a few genres like GamePlay/UI/etc but seems like I could go more deep on sorting them out
they aren't trying to make a flowchart, they are just explaining the purpose of each script
my message was an extension of bawsi's
Actually draw.io was something I was expecting lol but I learned we didn't really use flowcharts
Thank you all so much
A diagram doesn't necessarily mean flowchart. A flowchart would be more to describe a complex process. It could even just be a class diagram or whatever software eng nerds call it these days, where you just show how classes are linked.
GamePlay would be a bit too vague for sure. I would split it up much more. Like movement in it's own folder, hp/stats stuff in another
Not sure if this helps at all but a misc. diagram ive made in the past to visualise that kinda thing
i had nicer looking ones somewhere but they have poofed
kinda cooked but was semi helpful too
So then
Class diagrams = See how everything is linked (mainly in complex stuff
Code explanation = comments and sorting them more specific
I'm still a noob dev didn't know how everyone sorts it out
That's looks super helpful omg ty
Ideally a majority of code is written in a way that makes comments unneccasary
Depends what it is though
A lot of the time a comment can be really handy if you decided to do something a certain way because another way did not work and your comment explains that your not doing it that way because of whatever reason
don't punch me but I've been learning code through unity's tutorials until I came across Claude's ai code gen
which made reading/understanding all codes a pain in the neck
not knowing this really feels like ai is rather making it hard to code either way
no ones gonna crucify you for using AI but you can't ask for help with ai code on this server and most active members strongly believe it harms in learning rather than helping
I believe you're right, cause the mindset is starting to be learning code → make the code done(which is tough to do without learning) after using ai, its no easy way through
ai makes half-baked stuff for you at the cost of you not learning how to make it yourself, or any of the other relevant skills (researching info, understanding docs, debugging, reading & tracing errors, project architecture, separation of concerns, breaking up a problem, etc...)
it's not against the rules unless you havent tested it or noted such but it's a hrorible idea
Its not. People have this perception of AI cause a lot of other people use it the wrong way: To do what they want, without thinking about what its doing and then taking it for granted.
I regularly use AI to help me understand a topic better, then I go and check if its correct. To generate code, but then I go and try to understand why it generated that specific code and if it has problems or not. And so on
The problem is not with AI, its usually the users
but why not just skip the middleman and go to known reputable sources lol
Cause sometimes the source doesnt explain it the way an AI can
The problem is not with AI, its usually the users
true, but it's much harder to instruct people to "use it efficiently" lol
genAI is just very inconsistent
basically, it's a beginner trap
it has its uses, but it's much, much more likely to be misused, whether intentional or not
Have you ever looked into the unity docs? Half of it feels like noone actually cared about it and literally just wrote down what the arguments to a function are, and didnt provide any examples
and chatbots are using that same documentation. if an example isn't given, it'll just make one up, whether it's valid or not lol
you don't actually gain anything by using a chatbot
how do you check it if there's no example?
there are examples, just not in the unity docs; so again, just skip the middleman and go to those lol
The ai gives an example, which I can then try out and figure out what its doing
you're just creating more work for yourself 😆
it is, really
this topic has been exercised to death already, the concensus isn't going to change
Im not trying to change it
the recommendation to beginners will stay the same: don't use chatbots, you just end up stunting your learning experience
chatbots are a tool, one that you need experience to navigate and use correctly
beginners, by definition, don't have that experience
And thats what we should tell the beginners
How to use it
if you advocate using chatbots to beginners, you are actively hurting them
Not just dont use it its bad
orrrr, just skip the unnecessary step and just learn the actual relevant topics yourself lol
you just add another unnecessary step 😆
and that interrupts the actual useful learning
It's not the 'how to' that's the main issue - it's that "ai" will give incorrect code, and beginners don't have the knowledge to know what's wrong, won't understand it, and won't learn anything about coding.
I don't think it does, if you know how to use it. But if everyone is telling you to never even touch it, you'll never know how to use it. Like you said, its a tool. Tools can be super useful, but it has to be learned, just like the 'actual relevant topic' has to be learned.
I'm on the train with you all, that it shouldn't be the first thing to go to
but if you never even try it cause everyone is saying its bad, you'll never know how to use it properly
does one have to know how to use it properly? what is it even good for lol
i digress
it's just not a tool for beginners
it shouldn't be learnt alongside the other skills
you need to already have those other skills to be able to filter through all the bullshit it will spawn
that's for everyone to find out themselves
I personally find it really useful for small things
it is, objectively, a harmful tool when learning
by time you're at a level of knowledge to be experienced enough for ai to be... ok to use... you're no longer a beginner.
it's a masquerade of a source of truth
so to put it bluntly; sure, maybe genai has a place. but it absolutely does not have a place here, in #💻┃code-beginner
I never said its good for leaning. I just said its not a bad tool if you know how to use it
and if you know how to use it, you aren't in the learning stage
eh, yea thats probably true
just to make sure we're on the same page; the issue is not the question of learning to use genai, the issue is about beginners learning to use genai
I know, but I still wouldn't 'scare' beginners into never using it cause it's bad. I'd say use it when you know more
it is, still, pretty bad as a source of truth anyways
depends on what you want it to do
"when you know more" to a beginner would just be the next day lol
anyways another point; you have to have a certain level of experience in general to be able to handle the output
you should see the absolute pain it is to deal with people who use that shit and are surprised when it doesn't work lol
it's just much easier for us, the community, and the learner, a beginner, to just stay away from genai
don't give a benchmark for them to start using ai lol
oh yea I know, its mainly the younger people
it'll backfire to hell
I'm not giving them a benchmark
anyways still; recommending genai in any capacity in a support space just isn't going to be productive for anyone involved
Getting the error MissingMethodException: Method 'PlayerController.OnMove' not found. When using the new input system
public void OnMove(InputAction.CallbackContext context)
{
moveInput = context.ReadValue<Vector2>();
}
I have this method in a script on the same gameobject that has the player input component and the component has behavior it send messages, i checked the action and map name both are correct.
sendmessages doesn't emit a InputAction.CallbackContext, it emits a InputValue
yea, either change the sendmessages to Unity events, or the arguments to InputValue
so you'd need to have something like this instead
public void OnMove(InputValue val) {
moveInput = val.Get<Vector2>();
}
Oh lol thanks, first time using it even tho i should have long time ago
Any recommendations to fix an issue with my jittery camera?
I think the issue is that the player position changes, which messes with the camera's movement speed to catch up with the player, but I'm not entirely sure how to address that as an issue.
Does the player have a Rigidbody?
Also please share code via !code
📃 Large Code Blocks
Use links to services like:
https://paste.mod.gg/, https://hastebin.skyra.pw/, https://paste.ofcode.org/, https://paste.myst.rs/, https://scriptbin.xyz/
📃 Inline Code
Surround code with three backquotes. Not quotation marks.
To format as C#, add cs to the first line:
```cs
// Your code here
```
Add a comment with a line number if there is an error message.
The player does have a rigid body yeah
You need to turn on interpolation for the player's Rigidbody
And you need to make sure it's moving and rotating only via the Rigidbody, never the Transform
Oh it's immediately 100x better thank you so much.
I'll change up the code so it works with Rigidbody over Transform
💜
Having a issue with a button i have private IEnumerator SetSpinButtonSelectedNextFrame() { yield return null; // wait 1 frame EventSystem.current.SetSelectedGameObject(null); EventSystem.current.SetSelectedGameObject(spinButtonTenCoinsGameObject); Debug.Log("Selected GameObject: " + EventSystem.current.currentSelectedGameObject?.name); } to set the current selected button as highlight. the console shows the button as current select gameobject like so . but the sprite swap is not working and if i press enter it does not work. So what would stop this from working
doesn't sound like a code issue, perhaps #📲┃ui-ux or #🖱️┃input-system?
So i am doing that script:
But it wont work with "Random"
hover over the error and see what it says
Change it to UnityEngine.Random
Or that
Ty guys
guys could someone help me how do you create JSON using code and also encrypt it so players cant modify it
You can do this one step at a time. Just figure out how to save data to a JSON file first, then after that you can figure out how to encrypt a file.
I know how to add data to .txt file meaning .json wouldnt be different
there's no way to encrypt a json file on a player's computer such that they can't modify it
I believe
Worst case scenario they can just use Cheat Engine or similar to modify the data in memory
If you want the data to not be modifiable by the player it can't live on their computer, you would need to store it in the cloud
yea but I also want that players would save progress offline
then you have to accept that the players will be able to modify it
which isn't a problem
wait you cant encrypt it at all or just there are tools to bypass that
nobody cares if someone cheats at solitaire
If you want your game to be able to read the encrypted file the game would have to include the encryption key
which defeats the purpose
I mean its similar game to COD zombies so I believe people wouldd care if someone cheated tho the only way they could cheat is unlocking guns and unlocking whole upgrade tree xD
that is an online game
and progress is stored online
(i think, i never played it)
assuming it's online multiplayer
you can also play it single player and that doesnt require internet
but your progress is still stored in the cloud
but the progress stays even when you are offline so they must use something else then cloud
they use a hybrid approach no doubt
like both database and JSON?
almost certainly not json
probably a custom binary format
but JSON is really irrelevant to the discussion
it's just a data format
it's orthogonal to the discussion of where the data is stored and if and how it's encrypted
would you mind speaking in simple english please I am not really good at it
so what would you recommend me using to store player data? if I want to have database and offline storing?
basically just a local save file of some kind that gets synced to the cloud and gets validated by your server if and when it gets synced to the cloud
as for how validation works (i.e. "was this earned legitimately") that's a pretty hard problem in and of itself
Every cube is a chunk and the code is spawning 10 chunks whereas i have set the value to 5. could someone pls help
https://paste.ofcode.org/gmvqpD3ZSy7VfFw37DnwPY
I feel like i found how to make validation
pl help
Double check the number you have set in the inspector
@wintry quarry what if I made like that by each update the storage would get version up and if the version stays the same but values change in database and the file where the data is stored then players would get banned idk for 1week for playing with files
o m g. i spent 1 hr js to seeAAAAAAAAA
average thing of unity developer xD
heeyy (idk if i put this here but honestly im desperate, just lmk if im wrong) so as i am a beginner, i made a game over the course of 4ish weeks.. (its 2D and bad) but the point was to use it for my study in one of my classes (so it doesn't have to be like expert level or anything). issue: it works perfectly fine when in Unity (ykn) and I published it to several different (free) websites to try to make it into an actual web browser game. I wish I knew what question to ask but im just confused at what the issue is... i've researched and researched, so I guess I need help with the publishing aspect and how I can actually make it into a 2D web-browser game? (I can explain more if someone helps, its a whole thing)
you mean publishing and then having it public so other people can play it?
Continue with your current message that you're typing and then react to this xD
You'd have to explain what goes wrong in the build
its so hard, let me try
another thing. the inspector is correct but when i run the game it becomes like this. and i dont understand why
2nd image is how it becomes
Do you have a custom editor for this script?
explanationish: in unity, my game runs pretty fine when I test, no errors come up, seems as the title screen works + my instructions screen (for the study), my buttons are clickable, and the score works perfectly fine. so, after, what I did was I made my game into that web browser build, I seem to have a correct scene list to my understanding, then i have the zip file which I put into several websites (that are free, so unfortunately its itch.io and game something i forget), and I put in the correct file+ stuff for it to be a browser only type game.. now, when I open the game in my browser, it wants to play, but I am met with a blue screen and only one of my UI is working (which happens to be the round UI so it says "round 1/4") and it stays there, nothing happens-- no title screen/instruction screen. I occasionaly see my button (that the people can click) pop up but its one and stops.
nope
or not that i know of
You need to do this with a development build and check the browser console for errors
No idea then, seems like a Unity bug. Try restarting and/or upgrading?
I guess? thats my best at trying to explain ^, my troubleshooting was going through different settings in player other settings, trying to change a camera to see if one of the UIs was stuck? or like trying to find why the UI's aren't working? idk not sure
This would be good to see what errors the build has on webGL, do this first, see whats happening.
its been 2 days i thought of looking on it after i perfect the script
so restarting wont work
how are you getting the WebGL thing? mine just says web
I was thinking it doesnt matter too much if it was just named web?
could be a diff unity version, I am on 2022
also, is this photo from build profiles?
that was another thing I saw to troubleshoot but I only have build profiles and to my understanding thats the same thing as build settings?
when i click file*
ah I see, has been renamed I guess, Ill create an unity6 project rq to see whats there
ohh I see what you are talking about, development build at the bottom. I should click that and see the errors? (where would I see the errors also?)
(ive been troubleshooting for hours im buffering lol)
Yeah, enable that box, then make an new build and try running that. Then whenever you go to your browsers console and see what errors you get. I have not used the Dev mode on a web build myself yet, so I wont know wxactly what I would look for.
As mentioned, in the console window of your browser
console: ctrl+shift+c / ctrl+shift+i most of the time
my games taking forever to build for some reason lol but yea ill do this
https://paste.mod.gg/hgcamwazgoek/0
I'm not sure why, but it seems gravity is not applying properly, could anyone have a look at this?
A tool for sharing your source code with the world!
It's probably some indenting problem because it worked before
okay, well the only error i got is WebGL: INVALID_ENUM: getInternalformatParameter: invalid internalformat so 😭
how can you interpret this formula in unity?
float g = Physics.gravity.y;
float v = 45;
float d = Vector3.Distance(transform.position, targetPosition);
angle = 45 + 1f/2*(float)Mathf.Acos(g*d/(v*v));```
this is my attempt at it, but it doesn't work
(this is the formula for the angle of reach at which a projectile must be launched in order to go a distance d, given the initial velocity v.)
I'm basically trying to create a mortar
Acos returns a value in radians in Unity's API
You need to convert it to degrees
oh
shit
that makes more sense
I was given values between like 0 to 1
well not exactly but yea
is there a built in function for that?
Math.Rad2Deg is the constant to multiply by
oh theres Rad2Deg yea
multiply? isnt it a function?
Nope
it's just 180/pi
It's just a number
between rad and deg
also, how can I calculate the minimum velocity required to hit a point?
I couldn't find a formula for that
nvm it is an bracket error lol
in 2d?
yes I suppose
I mean
the mortar will always be looking toward the enemy
so I can ignore the Z variable
lemme check notes
I'm also having issues actually like
rotating the object
the red ball on the cube is meant to indicate which way is forward
but rn it just does this
transform.rotation = initialRotation * Quaternion.Euler(new Vector3(angle, 0f, 0f));
o shit actually it works
I just needed to subtract 180 degrees from it
@quick pollen
since the cannon is always facing the object you can do this
and if you need to find the distance you can do Vector3.Distance
i assume that the angle of the cannon is fixed right?
btw u is the velocity, |u| is the magnitude of the velocity
but it shouldn't matter since u should be positive anyways
Oh yea I should remind you
This angle is in radians iirc
also one more thing
this equation breaks when there is a differance in y level between the two objects
its not?
I mean
I'd need both the speed and angle to be variable
and I always want the steepest angle and lowest velocity
alright
I already have the formula for the steepest angle
but I need to be able to calculate the velocity beforehand
so before you know the angle or after
if after you know the angle
you can just plug the angle in the equation I just gave out
that returns minimum velocity
alright
tho wait
classic hs math
that doesn't work in projectile motion
it would guarantee a possible angle, no?
yes?
i think thatd be fine
I updated the formula 45 + 1f/2*(Mathf.Rad2Deg * Mathf.Acos(g/d))
instead of g*d/v*v I substituted v for d
it wouldn't work if you're looking for steepest angle and slowest velocity
I guess it doesnt matter THAT much
what matters is that its guaranteed to hit, and doesnt try to fire in a straight line
now I need to find a way to also make it look towards the player
if you optimize for slowest velocity it'd be 45°
the mortar, i mean
that's the angle for which you get maximal distance for a given speed
oh yea i forgot about that
thanks
transform.rotation = initialRotation * Quaternion.Euler(new Vector3(angle - 180, 0f, 0f));
right now I do this
but I need to make sure it looks towards the player
theres Quaternion.LookRotation, yes
i meant to reply to #💻┃code-beginner message here, sorry
@quick pollen
but I only want to change the y and z rotation
I thought so, yeah
I mean, honestly it doesnt matter too much
as I said, I just wanted to avoid cases where the projectile either flies for wayyy too long, or is aimed directly at the player
using the distance made it so it takes more time to get there depending on how far you are
maybe this doesn't really apply, but the direct solution i can think of is just having 2 nested GOs
the parent represents the mortar as a whole, and rotates about y to point towards the player
the child represents the barrel, and rotates about x to angle the barrel up
actually, not too bad of an idea
lemme test that
I'm thinking of getting the angle difference between the mortor and player
that exists, yes
and yeah i think it'd use this for the Y rotation at least
but that doesnt sound like it
thats the only thing id need to change
but idk how
I only need the Y angle from the lookrotation
I guess i can convert to eulerangles and take it from there?
transform.rotation = Quaternion.LookRotation(Vector3.ProjectOnPlane(target.position, transform.up))?
wait i don't think that's how ProjectOnPlane works one sec
im trying smth else wait
I tried this float playerAngle = Quaternion.LookRotation(targetPosition - transform.position).eulerAngles.y;
not great
transform.rotation = initialRotation * Quaternion.Euler(new Vector3(angle - 180, playerDirection, 0f));
i mean you're working directly from rotations to rotations
why go through eulerangles and back
for the LookRotation?
o wait you're right
transform.rotation = initialRotation * Quaternion.Euler(new Vector3(angle - 180, playerDirection + 90, 0f));
this works tho
actually i can get rid of the 90 and 180
Vector3 target = player.transform.position;
target.y = transform.position.y;
transform.rotation = Quaternion.LookRotation(target);
I just need to change the initialrotation
this would work fine, i think
ProjectOnPlane might give a way to do it in one line, but maybe that gets convoluted
but I also need it to be angled
so it both looks toward the player and is also angled so if a projectile is fired, it hits the player
im going off of this, where you would have a separate y and x rotation
so this would just be the y rotation
and then you'd compute the x rotation with trig on the other GO
or i guess if you have a single object and (for example) just make it point up 45°, could do something like
Vector3 target = player.transform.position;
target.y = transform.position.y;
transform.rotation = Quaternion.LookRotation(target) * Quaternion.Euler(45, 0, 0);
that does look tidier, lemme test that
also, how could I go about making it so something is always looking in a specific direction, regardless of its parent?
as an example, make something always look towards exactly down, without caring about the parent?
btw this isnt correct
set its rotation in LateUpdate, i guess?
target is a position, not a direction
can't you just set rotation?
you need to do targetPosition - transform.position
i didnt want to use code
i wonder if theres a way to do it without code
with like a constraint
oh whoops, goes to show how familiar i am with Quaternion lol
I mean, this is vector math, but its okay
you've already helped a lot
expose the argument in a script [SerializedField, Range(0f, 1f)] private Quaternion setRotation and then apply the rotation on start?
then you can just edit that rotation in editor
though FromToRotation does take 2 positions, could use that ig
so either:
Vector3 direction = player.transform.position - transform.position;
direction.y = 0;
transform.rotation = Quaternion.LookRotation(direction) * Quaternion.Euler(45, 0, 0);
```or```ts
Vector3 target = player.transform.position;
target.y = transform.position.y;
transform.rotation = Quaternion.FromToRotation(target, transform.position) * Quaternion.Euler(45, 0, 0);
is it dynamic is my question
does Range even work there?
oh yea
it's rotation
but should still be able to edit it no?
gimme a sec lemme try sth out
i mean the API lol, i confused myself about what it wanted
i don't think so
oh didnt see mb
oh wait hold on, the serialization rules thing isn't an exhaustive list
you can expose it with [SerializeField] private Quaternion rotationSet
I think I figured it out
and then you can set the rotation
in code but only need to be called once
and you can edit your values in editor
the projectiles linger a bit after they hit the ground
it looks a bit weird, yes
!cs
Join the C# Discord server, a programming server aimed at coders discussing everything related to C# (CSharp) and .NET. https://discord.com/invite/csharp
but it seems to do it normally
I would kinda like to make it so it goes faster and falls faster too tho
I guess I can double the gravity
no that doesnt work
if I go too close it just breaks
I get a negative sqrt
Hello, I was wondering if I could get a bit of help with some code. I'm trying to get a trigger to activate when the player goes into it and holds the space bar. I unfortunately can't see what I'm doing wrong. Thanks!
' private void OnTriggerEnter(Collider other) { if (other.CompareTag("Mantle") && Input.GetKey(KeyCode.Space)) { mantleCheck = true; } }
OnTriggerEnter gets called only once
when you enter the trigger
oh wait
are you sure that the collision is actually registered?
you should debug that first
This would trigger if you're holding down the space button before you make contact, and then contact the trigger while still holding space
how can I make an object fall faster?
changing the mass doesnt seem to change anything
So I'd need OnTriggerStay?
The code works fine so long as it dosn't the spacebar condition
did you forget middle school physics
what digiholic said, thisd work if you are holding it down while entering, not after you entered
yes
fall time isn't affected by mass
lmfao
what is it affected by then? surface?
I just gave that ago to debug it and did not seem to have any luck getting it to trigger still (Never mind it's now triggering just as I posted this -.- )
no thats not right
gravity and inital velocity
which includes angle
i thought heavier objects fall faster than lighter ones?
no
No, gravity is constant
v = u + at
man im tired lmao
Thank you @quick pollen and @polar acorn you're life savers for newbies <3
velocity is independent from mass
how could I make it fall faster then?
Mass affects terminal velocity, but not acceleration
Add force down
acceration which is gravity
the forces involved - gravity and drag
the force of gravity is proportional to mass, so it stays a constant acceleration
drag is proportional to velocity squared and the cross-section (though in unity these aren't considered, it's also just a constant deceleration)
but then my calculations for the trajectory get fucked
Well, yeah. If you're computing trajectory assuming normal gravity, changing gravity is going to change those
ignore drag for now ig, it's negligible currently
you either AddForce or just adjust the gravity variable imo
what's your eqn
if I get too close, I get a negative square root
this right?
angle = 45 + 1f/2*(Mathf.Rad2Deg * Mathf.Acos(g/d));
I changed velocity to be the distance
which is kinda stupid, but it semi works
where's the square root
idk but it seems to be mad about a negative sqrtmagnitude
distance is linearly proportional to speed, so.. sure, i guess?
that is square magnitude, not squareroot
i think it's because of the acos function
normally when we do acos, we take domain 0, 2pi no?
which line are you getting this from
idk lmao, I guessed smth with the acos function too
transform.rotation = Quaternion.Euler(new Vector3(angle - 180, playerDirection, 0f));
I know it has nothing to do with playerDirection
this was an issue before I added that
I mean -1, 1 mb
acos would have domain [1, -1] and range [0, pi]
oh yeah true
(why the new Vector3 though lol)
g/d is not between that
lemme look at acos in unity
needed one to use both the angle and the playerDirection
playerDirection is a float
which is a kinda misleading name now that i think about it
no you don't
yea i just checked
I tried your method with multiplying the 2 quaternions
but it was always off
it never hit the player
uhh that's not what i mean
Quaternion.Euler has an overload for float x, float y, float z
oh?
so you can do Euler(angle - 180, playerDirection, 0)
real useful lmao
a lot of functions have that kind of thing
uniform api surface 👍
wtf
when g is large enough
why are they different
so for some reason g is too large yeah
weird programming
it's 180/pi and pi/180
why is one a calculation and the other hardcoded
thats just 180 / 3.14 yeah
idk
unity ✨
Probably floating point shenanigans
Hard-coding that one probably gives a more precise answer than letting the division do it
love it
i think bc unity spits out angles in rad normally so they use a set value
Remember: Floats are significantly more dense near 0.
anyway, back onto the calculation thing, so its 100% that i cant use the distance for calculating the velocity
pi / 180 is in the range of "there's a lot of numbers here to use"
180 / pi is not
but idk what I could actually use
you can but like it's classic high school vector decomp calculations
ah, of course.
but the problem seems to be with the fact that I'm outside the domain of acos?
so idk what you mean by vector decomp calculations
when you solve projectile motion, you solve for the original vector by decomposing the x and y vectors given the range
and vice versa
yeah...?
give me a sec
which is a bit tedious to say the least
yeah im a bit confused lmao
I just wanna make it so the time it takes to reach the player is linear with the distance
ok so that's proportional to the velocity but it's not equal
that would take into account the angle
i mean, i dont need the time specifically
ah
what I mean is
I want to make it, so if you are x distance from the enemy, the projectile reaches you in x * y amount of time
ok what exactly are the variables in this scenario?
yea im also confused af
as in, what can you change
basically guarantee that the player is always hittable
you want t to stay constant over s
what can change, v and a?
presumably g stays constant, right?
I can change g I guess, thats it
and the velocity is proportional to the distance
uh no
sorry
ah
problem is
yeah that comes with time being constant
if the distance is too low, then I acos breaks
because velocity also becomes too low
a.k.a. it cant find an angle at which is reaches it, because the projectile falls to the ground too fast
if it worked like this, itd be perfect, but as you can see, if i get even slightly close, it doesnt work
for the angle?
for a mortar that might be 60°
then now you only need some distance for velocity magnitude
yeayea
I mean
i feel stupid for making all those calculations
and now it being useless
more than an hour ago
One can also ask what launch angle allows the lowest possible launch velocity. This occurs when the two solutions above are equal, implying that the quantity under the square root sign is zero. This, tan θ = v2/gx, requires solving a quadratic equation for
wait this isnt it
I would want to make it so the angle is based on a velocity maybe?
problem with that is itd be the opposite of what i want
itd take longer the closer you were
instead of longer the further you were
you're overcomplicating it
ight its like 1am in aus imma tap out first, but you can always set velocity for angle and angle for velocity if you use this equation
just set a constant angle
so the horizontal speed and velocity stay relatively consistent
as in, v_x = kv
k being cos(a)
If I make the angle be a constant, then I need to calculate the minimum velocity required to hit the player
no it's just. 1 exact value
which is given by my equation
it's 1 value
what is u here?
x is distance, u is inital velocity and theta is angle in radians
initial velocity
yes, and I need to solve for u
gimme a sec
i can do this maths myself i think
what's a tho?
here's for theta=60°
acceleration, gravity in this case
a is acceration which is gravity
yea looks right enough
I need to convert it later anyway
it does for the function you use in code
it's in rads
yea thats what I mean
degrees is just a constant, π/180
I can just convert the output of the sine to degrees
uh.. no?
?????
you can just...plug the values straight in no?
v is in a number
not some angle
oh
yea
so wait
Mathf.Sqrt(d*g/(float)Math.Sin(2*angle))
this is it, no?
angle is a deg tho
not a rad
but it shouldnt matter
then convert it lol
it does here
when you actually. yknow. give it to something else
why do I need to convert the angle to rad?
because Sin expects rad
then I have to convert the sin to deg too?
im stupid
aren't we all
fair enough
often it feels like no lmao
a bracket killed my code
and I spent like 30 minutes to find where i fucked up
love it
peak
i was so confused
ehm
float v = Mathf.Sqrt(d * g / (float)Math.Sin(2 * Mathf.Deg2Rad * (angle + 180)));
pemdas issue maybe?
nope
idk what it is wait
what are you assigning on the line given in the stacktrace?
why is v a float
why shouldn't it
why you doing angle + 180
it worked before for d
for some reason its rotated wrong
its looking into the ground
mb i should rephrase, why is input velocity nan
and I'd rather work in degrees
what's g?
usually that happens when you plug a float in
idk, thats the issue
how did you define g in
that'll be negative
that's why it was the wrong direction
can you just
it needs to be flipped, not rotated 180
g = 9.8
angle + 180 is in quadrant 3 or 4
answer is a negative
it's not about the square root
that wasnt the main issue
it was about the sqrt
distance is positive
I made it -Physics.gravity.y and it no longer does that shit
i fixed it
d = Vector3.Distance(transform.position, targetPosition);
float v = Mathf.Sqrt(d * g / (float)Math.Sin(2 * Mathf.Deg2Rad * angle));
float playerAngle = Quaternion.LookRotation(targetPosition - transform.position).eulerAngles.y;
transform.rotation = Quaternion.Euler(angle + 180, playerAngle + 180, 0f);```
not exactly intuitive
but if i dont do this i get a negative sqrt
Yep the v is now correctly written
thank you @shut swallow and @naive pawn for the help!
guess i learned quite a bit of physics
use Mathf instead of Math
(on the Sin call)
that's why you're getting a double instead of a float
where did you pull this formula out of tho?
Here
and those formulas?
those are pretty basic kinematics
Yea
my physics teacher is an ass
i need to learn all this by myself
and im not saying this as like a rebel
In projectile motion at the top of the parabola, velocity in the y direction is 0 and half of total time has passed
he straight up threatens you if you ask a question
🤨
i thought you said teacher
yes?
Ight it's 2am here
that aint a teacher
what is it then
an asshole with a degree
have a good night!
yep, I guess I should say employee at my school
fr tho
I had to get into quantum mechanics without him
I just started doing gamedev and such on his classes
and he gets mad that im not looking at the presentation hes reading
others are just playing videogames or sleeping
at least i do smth productive
and somewhat physics related
I like physics a lot, and I am trying to learn it, mainly through gamedev rn, I just hate him, and it discourages me from even trying when he gets mad that I don't know the formulas stuff like, idk number of oscillations over x time
(rant over, sorry for off topic)
can someone tell me what is bigginer code like
would linking JSON to Database looking the data and uploading to each other whatever has higher value
would count as begginer code or should I go to advance or general?
that would probably go in #archived-code-general
nvm I found the mistake I was dumb I switched Key value with IV value
@quick pollen anyways here's a (relatively) brief explanation coming from the basics
- a velocity 𝑣 with an angle 𝛼 to the plane can be split into 2 components, 𝑣ₓ = 𝑣cos𝛼, 𝑣ᵧ = 𝑣sin𝛼
- the time it takes to travel back to the same plane: v = u + at (final velocity = initial velocity + acceleration * time)
- when it comes back down it'll be the same speed, but in the opposite direction
- -𝑣ᵧ = 𝑣ᵧ + 𝑔𝑡 ⇒ 𝑡 = -2𝑣ᵧ / 𝑔
- the time it takes to travel some distance - s = vt (displacement = velocity * time)
- 𝑠 = 𝑣ₓ𝑡
- ⇒ 𝑠 = 𝑣ₓ * -2𝑣ᵧ / 𝑔
- ⇒ -½ 𝑠𝑔 = 𝑣ₓ𝑣ᵧ
- ⇒ -½ 𝑠𝑔 = 𝑣²sin𝛼cos𝛼
- sinAcosA = ½ sin2A
- ⇒ -½ 𝑠𝑔 = 𝑣² ½ sin2𝛼
- ⇒ 𝑣 = √(-𝑠𝑔/sin2𝛼)
i probably didn't have to do the full working, huh...
idk, just a habit lol
ahhh
tysm for the explanation!
i think i get it
(ngl i usually use integrals to calculate displacement, but if the velocity is linear then it doesnt matter ig)
also ngl i didnt know you can split a velocity vector into 2 components like that
I am insanely new literally just started like 20 mins ago what did i do wrong trying to do some movement stuff
watching a tutorial
you didn't open the method
huh
look at the tutorial code closer, you haven't followed it accurately
{ } you dropped these
yeah io was looking for those they didnt come up
last time they came up automatically
It'll make an end brace when you type the start brace
and.. what's the issue with typing them yourself
there is no issue i was just wondering if i did something wrong
you didn't add { }, that's what you did wrong lol
...why
yea
just type them lol
bro i dont have them on my keyboard
yea you can copy and past them
not sure its a 60% keyboard

relying on copy/paste will be a pain
what layout do you have
this isn't a control key
it's not removed by being 60%
idk where they are but ill just paste them for now
is it qwerty?
{ is shift+[, } is shift+]
You're not going to be pasting braces for the entirety of your time programming
im doing it for now
braces are used very frequently
just find them
why do you want to make life harder for yourself
maybe python is more your speed?
im going to paste them once
python uses braces too
for dicts and sets
and then ill man up and find it
In the time you've spent arguing why you can't look down, you could have just looked down
my brother in christ, look down
that is true
2 sec
looking down intensifies
...you don't know how the shift key works?
hey they said they found them already
.............. on my keyboard.... there is æ and ø
to be completely fair to them, it entirely depends on the keyboard language
that's why i asked what layout you're using
you didn't answer
so i assumed lol
norwegian
try R-ALT + 7 for {, R-ALT + 0 for }
that's what works on my Finnish keyboard
It's been asked because folks were willing to go out of their way to address the location of it relative to keyboard layout.
sometimes you're too busy being the semunGOAT 
this one?
are u norwegian
no, finnish
the symbols on the right of each key are for altgr
so it's altgr + 7 for { and altgr + 0 for }
well ok i'm not "finnish", i live in finland
but yes you need to know how to enter your own braces
i mean.. did you not know how to use altgr before?
to be fair, skemp gave you the answer earlier
ah yeah didn't notice 🐌
altgr? what are we, dogs?
alternative graphic
alt grrr
alt meow 
im not a nerd
you are on discord asking for help programming
sounds like a nerd to me
how to use keyboard tutorial, 2025, free, no virus
such a low bar 😆
my goal is to become a nerd thanks to chris for bringing me 1 step closer
Fellas is it nerdy to know what country you're in
we're on discord because we have social ineptitude
we're all geeks
my IRL friends dont know i have a discord 😉
your next step is to go through the C# tutorial from Microsoft first
thats a good step 👍
since you had issues with the code not working without braces {}
what's a friend
you should know the basics from here
ill try it later thanks boss
go through that, and then !learn
:teacher: Unity Learn ↗
Over 750 hours of free live and on-demand learning content for all levels of experience!
can i use a script macine and a state machine a t the same time when i do code a 2d game with visual scripting?
wdym by a "script macine"? also if you have visual scripting questions they belong in the #763499475641172029 server
and "script machine" means what in this context?
I think that was the nickname hollywood producers gave to Stephen King in the 80s
you have a code in the script machine, but i did see that the state machine also does use codes(nodes) in the tutorial
do you mean the IDE??
It looks like "Script Machine" is a visual scripting component. No one here is really going to know what that is
We use code here
ok, i just wanted to answer to the question boxfriend asked
oh yeah just looked it up, it's all visual scripting stuff
and you managed to not actually answer it. digi's response was actually informative though. but yes, this is definitely not the place to get help with visual scripting because we are sane people here
i remember my first project's teacher said to use visual scripting for a state machine lol...
i did not end up doing that
ah...i am just confused because there would be 2 parts where codes are
that actually makes some amount of sense though, provided you actually implement the states themselves in code and just expose them as nodes. which is pretty much what the animator controller is
true, i just remember he was being really vague about it, i looked up visual scripting and got turned off immediately lol
yup, like the average person
I don't know if this is the right place to write this but I'll post it anyway. I have a problem with battle scenes conflicting with my overworld scenes. My "game" is a game with multiple rooms/scenes and I use DDOL on my player to retain my player position when switching scenes. Basically I make a door that change scene then I make position the scene where the door is, so when I switch back scene I spawn at the door. But I wanna make a battle-arena scene that appears when encountering an enemy. And I want my player to be in the same position that it was before the battle after the battle is over. Could I possibly store the player position then teleport to it after the battle is over? How would I do this?
BTW I want my player to be in the battle-arena scene since Im going to be able walk around and dodge the enemy's attack in this case the "Flame enemy" lol
Could I possibly store the player position then teleport to it after the battle is over? How would I do this?
not sure y u couldn't? something like a gamemanager keeping track of where the player will spawn bakc into.. jus like the DDOl stuff
Is there a better way to this?
it'd have to be something persistent
ur player gets spawned in correct? couldnt be on the player.. u said u have some DOOL stuff.. could possibly cache it and use it there
well if ur player sticks around.. and ur not instantiating it in..
u could probably put a variable on it and change it as needed
then when u need to reposition him or w/e u just read from its variable
next time u go thru door or into battle.. just give the player his "returning position"
im trying to do multiplayer, i've pretty much done it all its just getting it so when the 2nd player spawns it doesnt take over the camera
alr workin on it
hye im a beginner in unity and utilize this software in my daily work. Im trying to be prepared if given task related to unity. I have been discovering the unity for 1 month.
The issue i had with unity is when writing c# script. How to find the directory name and its related class because some directory or should i say Meta sometimes does not have an up to date documentation about the directory i want to use . Soo how does the big shot and pro league manage to get their hands to utilize this scrambled documentation and directory.
sometime the class name i use does not recognize the directory im ising although it stated in the doccumentation
what do you mean by "the directory" in this?
oh, i just figured since it was on the same machine it didnt need any wouldn't be networking related
Well i presume in this "battle scene" you dont need the same player components right? Why not have the player sprite/model in this battle scene to start
you're literally using a networking library
true
if you are trying to do local multiplayer then you don't need a networking library so you're doing it wrong anyway
Should I hide the player during battle, It's still going to be in the battle scene since its under DDOL
Are you talking about namespaces? you need to give more information cus what you just said hardly makes sense...
Then thats a flaw and you should not use DDOL OR hide it during this
