#archived-code-general

1 messages ยท Page 146 of 1

somber nacelle
steady moat
#

The big issue I have with your current implementation is that is not enough flexible, i.e. robust to change. Your save game should not know what a player is. It violates the principle of Tell don't Ask.

If you stay like that, your game is going to be non flexible and it will be hard to keep adding new object. Instead, you should consider using an approach with interface such as:

    public interface ISaveable {
      public object Save();
      public void Load(object data);
    }
    
    public class Player : ISaveable
    {
        public class SaveData {
            public int Level;
        }
        public object Save()
        {
            return (object)new SaveData();
        }
        public void Load(object data)
        {
            
        }
    }
    
    public class SaveManager {
        public List<ISaveable> saveables = new List<ISaveable>();
        
        public void Save()
        {
            foreach(ISaveable saveable in saveables)
            {
                //Do Save
            }
        }
    }

https://martinfowler.com/bliki/TellDontAsk.html

edgy lynx
#

Yeah this is a similar approach I used in my past game, but I found it got pretty cluttered implementing the interface throughout all the scripts.

steady moat
edgy lynx
steady moat
#

What exactly was irritating with the interface ?

#

The fact that it added codes to your class ?

edgy lynx
#

Sort of, more so that it added code to many classes, and many classes had to be updated throughout the project when new things were added, rather than one central location.

steady moat
#

What do you mean by many class had to be updated ? Because it usually is the opposite.

#

If you add a new class, there is no need to make any change exept to this specific class.

#

I'm asking those question because interface is not the only way of having this seperation.

edgy lynx
#

I mean the implementation of the interface for each class that needed to be saved, had different implementations. I'm trying to look through some of the classes I had let me see... this was like 9 months+ ago

steady moat
edgy lynx
#

Some were very covoluted such as:

    public void LoadData(GameData data)
    {
        if (data.serializeableFishCollected.TryGetValue(nameOfFishHeldWithinSlot, out FishStats fish))
        {
            if (fish.numOfFishCaught > 0)
            {
                fishTextCount.text = $"Amount Caught: {fish.numOfFishCaught}";
                fishTextName.text = nameOfFishHeldWithinSlot;
                fishDescription.text = fish.descriptionOfFish;
                instanceOfSilhouette.SetFloat("_Hide", 0);

                SetFishLengthSliderOnLoad(fish);
                PopulateFishCollectionDictionaryUponLoad(fish);
            }
        }
    }

    public void SaveData(GameData data)
    {
        foreach (var kvp in fishCollected)
        {
            if (!data.serializeableFishCollected.ContainsKey(kvp.Key))
                data.serializeableFishCollected.Add(kvp.Key, kvp.Value);
            else
                data.serializeableFishCollected[kvp.Key] = kvp.Value;
        }
    }

Others were of course much simpler:

 public void LoadData(GameData data) => transform.position = data.playerPosition;
 public void SaveData(GameData data) => data.playerPosition = transform.position;
topaz ocean
#

if you make a System Serializable data container class, create a set of values, and then assign them to a public reference, what happens to the inspector values?

edgy lynx
#

And then I still had my one GameData class:

[System.Serializable]
public class GameData
{
    public int playerMoney;
    public Vector3 playerPosition;
    public bool wasBasicFishingPoleBought;
    public int wormCount;
    public int progressBarValue;
    public SerializableDictionary<string, FishStats> serializeableFishCollected;
    public bool hasBasicFishingPoleTipBeenDisplayed;
    public bool hasWormTipBeenDisplayed;

    // Default values when starting a New Game go in this constructor.
    public GameData()
    {
        wormCount = 0;
        progressBarValue = 0;
        playerMoney = 35;
        playerPosition = new Vector3(18.23f, 1.997f, 76.922f);
        wasBasicFishingPoleBought = false;
        serializeableFishCollected = new SerializableDictionary<string, FishStats>();
        hasBasicFishingPoleTipBeenDisplayed = false;
        hasWormTipBeenDisplayed = false;
    }
}
steady moat
#

Oh, the issue is that you are reusing the same class through all save.

edgy lynx
#

I just felt like it get so messy overtime

steady moat
#

This is not what you should do.

#

You should have 1 class per object.

#

You understand the difference between what I am suggesting and what you have done ?

edgy lynx
#

Not yet ๐Ÿ˜› but i'm listening...

steady moat
#

You have 1 class containing the whole save data.

#

I have several class containing the data.

edgy lynx
#

So here:
?

#

A new SaveData for each object?

steady moat
#

The result would be:

{
  "0000-0000-0000-0000" : {
    "Level": 0,
  },
  "0000-0000-0000-0001":{
    "HP" : 0,
  }
}
steady moat
edgy lynx
steady moat
#

There is also other method that reduce the amount of boilerplate needed.

#

Such as reflection and code generation.

#

You can also use partial class if you dislike having the save code in the same class as your main class,

#

However, I think using #region is more appropriate.

edgy lynx
#

Thanks, I'm going to mess around with what you have here and ensure I understand it.

mellow night
#

So I'm messing around with some solutions to particle systems vs floating origin and found something really odd...

#

I made a "parent" transform to put all the particles in called "ParticleSpace" this lets me pick up all the existing particles at once and move them 1 "stage size" over along with player, camera and all game objects, whenever player and camera get too far from origin

#

problem with that method is the transform of the particlespace will drift from origin, so to test it out, I put it 10 million units away... so particlespace origin is at massive negative x but particles are generated at massive positive x in local coords within that... what's weird is it didnt lose positional accuracy so much - but it lost the ability to rotate particles

#

they all generate at the same angle... very odd IMO

rain minnow
edgy lynx
#

Actually, think I got it... messing with it more still.

wary coyote
#

this is shader related but the snag I'm hitting is on the code side.
I am trying to encode barycentric coordinates into vertex colors. I found a component online in github, but theirs only draws a wireframe of triangles, I want to draw a wireframe of quads. I found a vert/frag shader that correctly draws quads as well, but I have been trying for hours to marry the logic from the quad into the logic that encodes vertex colors but to no avail

#

their code is so efficient that I am struggling to understand what it is doing and how it works, they make so many lists and sublists and call things labels and just the logic is all over the place in ways that are extremely efficient, but also completely opaque

#

I've tried to parse it repeatedly, and you can see in the 'encode' file there's 3-4 attempts I've made to re-write/recreate what they're doing from scratch, written in a way I understand, but none of my attempts produce results like theirs

tawny elkBOT
#
Posting code

๐Ÿ“ƒ Large Code Blocks
Large code blocks should be posted as links to services like:
https://gdl.space/, https://paste.ofcode.org/, https://hatebin.com/
https://paste.myst.rs/, https://hastebin.com/

๐Ÿ“ƒ Inline Code
Surround code with three backquotes. Not quotation marks.
To get C# formatting the first line should only contain cs or csharp.
Add a comment with a line number if there is an error message.
```cs
// Your code here
```
Do not share screenshots of code unless requested.

wary coyote
#

TLDR: I want to refactor this other person's code to support my use case of detecting the longest edge of each triangle and adjusting those values so that the end result shader culls those edges out, but I can't penetrade their hyper efficient logic to make those changes

#
            [maxvertexcount(3)]
            void geom(triangle v2f IN[3], inout TriangleStream<g2f> triStream) {
                float edgeLengthX = length(IN[1].vertex - IN[2].vertex);
                float edgeLengthY = length(IN[0].vertex - IN[2].vertex);
                float edgeLengthZ = length(IN[0].vertex - IN[1].vertex);
                float3 modifier = float3(0.0, 0.0, 0.0);
                // We're fine using if statments it's a trivial function.
                if ((edgeLengthX > edgeLengthY) && (edgeLengthX > edgeLengthZ)) {
                    modifier = float3(1.0, 0.0, 0.0);
                }
                else if ((edgeLengthY > edgeLengthX) && (edgeLengthY > edgeLengthZ)) {
                    modifier = float3(0.0, 1.0, 0.0);
                }
                else if ((edgeLengthZ > edgeLengthX) && (edgeLengthZ > edgeLengthY)) {
                    modifier = float3(0.0, 0.0, 1.0);
                }
            }```
This is how the shader expects the data to be formatted, the longest edge has more R, G, or B, but I haven't been able to bring that over to the other persons code
#

Like they're doing all kinds of sorting on the triangles for reasons I don't understand, but all my attempts to do this without sorting the triangles failed

#

but all their sorting and rearranging and labeling and indexing are making it beyond my ability to figure out when/where to do the checks for longest edge and add that resulting color

#

its midnight in my timezone so I am putting this to bed for the moment but I need to get this solved and I am beyond my ability to solve it myself

#

spent about 20 hours on this so far

edgy lynx
unique sparrow
#

I am working on a script that allows the player to buy healable objects. The problem is that when I try and add a key to purchase the item it does not go through but when I do not use an if statement and the player just collides with the object it just goes through immediately. This is my code -> https://hatebin.com/rwipcflzha. Any help is appreciated.

livid quarry
#

this might be weird but can someone hop in a vc with me and help me with my vr games

lean sail
#

https://docs.unity3d.com/Manual/ExecutionOrder.html

Before the first frame update
Start: Start is called before the first frame update only if the script instance is enabled.
For objects that are part of a scene asset, the Start function is called on all scripts before Update, etc is called for any of them. Naturally, this cannot be enforced when you instantiate an object during gameplay.

Does this part in the docs mean that an object can run its update before running its own Start? Or does this line just mean that its possible a script runs its start after another script ran its update in the same frame?

rain minnow
#

basically, the latter of your response . . .

rain minnow
unique sparrow
#

I ended up fixing it using booleans

#

Thanks anyways though ๐Ÿ™‚

muted helm
#

Hey, I've written a simple tool that generates pose info for my hands by storing a List<Tuple<string,Vector3,Quaternion,Vector3>> in a ScriptableObject. It works correctly and can save the pose and load it on another hand until the original gameobject that holds the tool is destroyed, in which case the scriptable object is empty for some reason.
I write and save the info like so:

        poseInfo.right = right;

        if (AssetDatabase.LoadAssetAtPath(parentPath + poseInfo.name + ".asset", typeof(HandPoseInfo)) == null)
        {
            AssetDatabase.CreateAsset(poseInfo, parentPath + poseInfo.name + ".asset");
        }

        AssetDatabase.Refresh();
        EditorUtility.SetDirty(poseInfo);
        AssetDatabase.SaveAssets();```
I can gaurantee the left and right collections are populated, they only are emptied after the original object is destroyed. How do I go about saving a scriptableobject to the disk properly?
ashen yoke
muted helm
#

Ah, okay. Will try

ashen yoke
#

unity wont serialize generics unless you use SerializeReference, it may not serialize tuple anyway

muted helm
#

Yeah, I figured something like that might have been a worry but never though too much about it

#

Could I just use a struct?

ashen yoke
#

does it need to be?

#

anything can reference those entries?

muted helm
#

What other type could it be?

ashen yoke
#

is this a hotpath?

#

a class

muted helm
#

Is a class not overkill for four values?

ashen yoke
#

define overkill

muted helm
#

Fair enough. I don't need methods or anything is why i figured

ashen yoke
#

structs can have method

#

the limitation is that interface cast to object in some conditions

muted helm
#

Oh

#

So literally just this is serializable?

#

Still not sure what the rules for serializable are

ashen yoke
#

the only benefit you get by storing as a struct is that it will be embedded into the object, you skip one dereference

#

just add System.Serializable

#

and read docs on unity serializer its rules

wind palm
ashen yoke
#

can you predict all the edge cases ahead?

wind palm
# ashen yoke why?

There are no references inside that struct. Why would you waste heap allocations when you can just have it on the stack?

ashen yoke
#

no references? string is a reference

wind palm
#

True, to be fair

ashen yoke
#

you cant predict all the edge cases that arise from copy semantics and designing your api from the get go with it

wind palm
#

I'd also remove that

ashen yoke
#

i mean, you can if you try hard, and you know exactly why you are using struct

#

and you understand all the implications, but if not the marginal nanoseconds you save can become a massive headache in the long run

wind palm
muted helm
livid quarry
#

does anyone know how to fix this

wind palm
livid quarry
#

how do i change it

wind palm
ashen yoke
#

its SteamVR package throwing

livid quarry
#

oh

ashen yoke
#

this isnt compiler error right? you are launching the game and you see this

#

do you have steamvr installed/steam running?

livid quarry
#

I booted up unity and that started happening

ashen yoke
livid quarry
#

and i have like 999 errors

ashen yoke
#

from your error

#

read your errors

#

dont crosspost

muted helm
#

Yes, a class marked as serializable works properly now, thank you for the help .cache and Tom

livid quarry
#

okay im sorry

livid quarry
ashen yoke
#

OpenXR?

#

also check Assets/Plugins

quartz folio
#

What's that screenshot meant to show

wind palm
quartz folio
#

it's in the middle of a search and shows nothing in Assets that's relevant

livid quarry
#

okay sorry

ashen yoke
#

from your error again

#

read your errors

deep willow
#

im trying to move an object in my scene but all of a sudden its not letting me drag it around

#

im selecting it in the scene but the arrows arent showing up

#

not sure why thats happening

gray mural
deep willow
#

i did that

#

something wonky is happening

#

not sure what i did

#

i can select the object, but it only highlights it with orange

#

and no move arrows appear

quartz folio
#

Try reopening the scene view and resetting your layout if that doesn't work (also this is not a coding question)

deep willow
#

ah kk sorry

balmy void
#

anyone know why I'd get UnauthorizedAccessException from system io filestream when I write to Application.persistentDataPath?

#
StreamWriter writer = new StreamWriter(Application.persistentDataPath);

foreach(RaceLogEntry entry in m_RaceLog)
{
    writer.WriteLine(entry.position.ToString() + "," + entry.velocity.ToString());
}

writer.Flush();
writer.Close();
quartz folio
#

You need to create a file inside it.

gray mural
#

is it better to use m_foo or foo in script where both field and property exist?

balmy void
#

oops

quartz folio
thin aurora
#

Don't look at Unity's syntax

gray mural
wooden knot
#

What is the simplest way to stop motion using raycasting?

thin aurora
#

What Vertx said, there are code conventions pinned

quartz folio
gray mural
#
private int m_textComponentIndex;

private int textComponentIndex
{
    get => m_textComponentIndex;
    set => SetTextComponentIndex(value, false);
}
gray mural
#

It seems nice

quartz folio
#

We know you use it, why though is a mystery

gray mural
quartz folio
#

do you even know what m_ stands for

thin aurora
#

You're not forced to do it

gray mural
thin aurora
#

Nope

quartz folio
#

I know what they do

gray mural
thin aurora
#

Why exactly are you asking these things if you're just going to argue?

quartz folio
#

I'm glad Unity decided to change to C# standards in most of their new packages

gray mural
gray mural
thin aurora
#

There's no better, there's only conventions that you are recommended to do to keep in line with everyone else

#

If you want to prefix all your fields with m_, feel free to. It just doesn't make sense because that's not why they're prefixed like that

thin aurora
#

Good job team

quartz folio
gray mural
ashen yoke
#

f_speed
s_name

thin aurora
#

I actually thought it represented the actual purpose, like m_ being for MonoBehaviour and such. Turns out it's old c++ conventions to indicate class member variables

ashen yoke
gray mural
ashen yoke
#

what does m_ stand for?

gray mural
ashen yoke
#

and just _ ?

gray mural
ashen yoke
#

and private fields are?

gray mural
ashen yoke
#

they arent members?

gray mural
ashen yoke
#

so if its for members that are private what else does m_ cover?

gray mural
ashen yoke
#

then why is it an m_

#

and not for example b_

#

since its a backing field

#

and you make a strong distinction

gray mural
ashen yoke
#

or k_

#

i think its k_ right? the compiler generates

gray mural
#

I forgot what it's used for

ashen yoke
#

for exact purpose you are using it

lean sail
gray mural
#

I think it makes my code readable too

#

because I have some properties

lean sail
#

because those are old

gray mural
#

I like them

ashen yoke
#

i think it stems from microsoft having those guidelines for years

#

i wonder if they changed in new .net

#

yeah they dropped it altogether

gray mural
ashen yoke
#

pity was m_

#

ill elaborate

#

when you indicate that a private field is a backing field, can you guarantee that the field is only used as a backing field for that property?

ashen yoke
#

if in the future the property would retarget to another field, or completely drop that field usages, will you rename the field

ashen yoke
#

what if several fields participate in single property?

#

should they all also be marked?

thin aurora
#

Make sure you prefix all the methods with F_ by the way, otherwise you might mistake it with a property, and it's more readable

ashen yoke
#

what happens you rename m_a to _a ?

#

and the code that was written under assumption that the m_ is welded to the fact that its a backing field for a property would have to be rewritten as well?

#

that is done so you can write code that makes that assumption right?

#

code that discriminates fields

#

otherwise what purpose does it serve but cosmetic?

spark flower
#

await UnityServices.InitializeAsync() => { GiveConsent(); }
how do i make it run GiveConsent automatically when the initializeasync function has finished?

thin aurora
#

Or put it below the async method since the method waits until the async method finished

spark flower
#

like this?

thin aurora
#

Yes, that's how async-await works

spark flower
#

ok thanks

thin aurora
#

Probably a good idea you get used to how it works and also why async void should only ever be used when you make Start and/or Awake asynchronous

#

Not knowing how it works can give you some serious issues

spark flower
#

im sure

#

im just new to it

gray mural
gray mural
ashen yoke
#
float _speed;
float _speedNormalized;

//was
public float Speed => _speed;
//became
public float Speed => _speedNormalized;
#

how?

swift comet
ashen yoke
#

you .. write it

gray mural
ashen yoke
#

was

#

became

#

i left comments

gray mural
#

you cannot change property

#

iirc

ashen yoke
#

what

winged mortar
#

Over-time, as in with code changes :)

gray mural
winged mortar
#

So as development continues they change how that variable is derived

ashen yoke
#

you have some weird understanding

gray mural
ashen yoke
#

nothing stops you from changing get, set

#

because they are simply methods

gray mural
#

and properties

gray mural
swift comet
#
    public int MyProperty
    {
        get
        {
            // Custom logic to return a modified value
            return myProperty * 2;
        }
        set
        {
            // Custom logic to set the value
            myProperty = value + 1;
        }
    }

ashen yoke
#

can you please explain where did you get this interesting misconception about properties and methods?

#

ive never seen this before

ashen yoke
#

you are saying you cant change methods, what do you mean by this?

gray mural
#

you cannot rewrite method or property

private string text
{
    get => m_text;
    set => SetText(value, true);
}

private string text
{
    get => _text;
    set => SetText(value, true);
}
ashen yoke
#

are you suggesting a technical limitation or some conceptual/paradigmic limitation?

gray mural
#

this throws error

ashen yoke
#

oh

#

third times the charm, the example i provide

ashen yoke
#

means that there was property a, that was rewriten as property b

gray mural
ashen yoke
#

it doesnt mean there are 2 properties at the same time

swift comet
#

The wording was confusing*

ashen yoke
#

was/became is pretty clear

#

was means in the past

#

became means present

#

indicates a change

#

between past and present

gray mural
#

I still don't get it

private string m_foo;

private string foo
{
    get => m_foo;
    set => m_foo = value;
}
#

m_foo and foo won't be ever changed in this example

ashen yoke
#

because this example is not real code

#

in a real code base changes happen all the time

gray mural
ashen yoke
#

i type on a keyboard

swift comet
#

Man im so confused xD

gray mural
#

it seems that we are talking about different things

swift comet
lean sail
gray mural
gray mural
#

with F2

#

that's all

winged mortar
#

imagine if unity would rename all of their fields every engine update

gray mural
#

what do you even mean?

#

renaming fields is when you do it before script runs

swift comet
#

Yes hes joking

gray mural
#

you are not able to rename field on runtime

#

just it's value

swift comet
#

joke

winged mortar
#

Also not during runtime, naming conventions dont matter during runtime anyway

gray mural
#

you cannot

string foo = "blabla";

string foo = string bar;
ashen yoke
#

and the code that was written under assumption that the m_ is welded to the fact that its a backing field for a property would have to be rewritten as well?
that is done so you can write code that makes that assumption right?
code that discriminates fields
otherwise what purpose does it serve but cosmetic?

gray mural
ashen yoke
#

repost

winged mortar
#

your computer is never going to have trouble reading your fields

#

anyhow, besides the point

winged mortar
#

what cache is trying to say is that if you use the m_ prefix for variables that act as backing fields, and you then later change this behaviour, the prefix will no longer make sense

#

and it is not sensible to constantly rename your varaibles when working in larger projects

#

Especially when your functions are used by others, in the case of unity

gray mural
#

it doesn't matter, but will you understand it then?

winged mortar
#

Cache also outlined some other cases where it would no longer make sense

thin aurora
gray mural
lean sail
ashen yoke
#

its not about renaming, its about a semantic load it carries, why do backing fields have meaning for code? Should they? Why code has to differentiate between kfield and field? What implications arise from that?

ashen yoke
#

that is cosmetic aspect

#

you are missing the point

ashen yoke
#

if you are after readability _ is more readable

gray mural
thin aurora
ashen yoke
#

but you differentiate k fields

#

which makes them less readable

#

and puts on you the load to make assumptions about your fields

#

if this field is a kfield then... if not then...

gray mural
winged mortar
#

I think this is one of those things that comes with experience + he doesn't seem to quite understand what you are trying to say

ashen yoke
#

so it seems

swift comet
gray mural
swift comet
gray mural
gray mural
#

but returned with property

swift comet
#

Yes but you are using C# so better use C# conventions

thin aurora
# gray mural I cannot give them same names, but yeah, naming fields with `m_foo` and their pr...

Do you realise that absolutely nobody resorts to naming their variables foo, bar, a, b, i, j or anything simplistic like this? You give actual readable names that explain the purpose. I have developed for years and very rarely I get naming conflicts, which is only the result of my own poor naming

So this is a very bad way to omit this, and definitely not any more readable over just using the regular naming conventions.

#

I understand you find it better, in which case go ahead and do it your own way. I can assure you however that the moment you start working in a team, your preference will most likely cause a big conflict because they will agree with you.

gray mural
lean sail
ashen yoke
#
private float _speed;
private float _maxSpeed;
private Vector3 _velocity;

@gray mural which of these is a backing field?

gray mural
#
public float caretBlinkRate
{
    get { return m_CaretBlinkRate; }
    set
    {
        if (SetPropertyUtility.SetStruct(ref m_CaretBlinkRate, value))
        {
            if (m_AllowInput)
                SetCaretActive();
        }
    }
}
public TMP_Text textComponent
{
    get { return m_TextComponent; }
    set
    {
        if (SetPropertyUtility.SetClass(ref m_TextComponent, value))
        {
            SetTextComponentWrapMode();
        }
    }
}
lean sail
#

thats text mesh pro.

gray mural
thin aurora
# gray mural maybe

Not even maybe. The moment you start adding linting packages or any sort of code convention validation code in your project, it will scream at you because of all the prefixes. Nobody does this anymore.

ashen yoke
#

that is not written by unity

thin aurora
#

Look up any modern Unity package and it will not have this convention

ashen yoke
#

and its not their code, its developed by one guy, which asset they purchased

lean sail
#

damn all that was developed by 1 guy?

ashen yoke
#

yeah and still is afaik

gray mural
lean sail
#

tmp_text is like 8k lines, that guy is a psycho

#

and thats like 1 class out of the entire package

ashen yoke
gray mural
#

probably _speed

ashen yoke
#

im asking, why do you need to know

gray mural
#

cuz I gonna just change it in property

#

oh

#

I haven't mentioned why

gray mural
#

right?

ashen yoke
#

right? i dont know if its right, but i assumed properties are public interface

thin aurora
# gray mural e.g.?

Pretty sure Vertx already gave you examples a few hours ago when you started the discussion

gray mural
ashen yoke
#

what stops me?

#

i have a complex code that sets it reads it all the same

#

theoretically speaking

thin aurora
#

A literal comment could explain it better than having a prefix everywhere

gray mural
ashen yoke
#

and?

#

private properties?

#

thats a new one

swift comet
#

Why so focused on Unity anyway. Use c# conventions, then everyone can read code more easily ๐Ÿ˜Š

Also @gray mural imagine you have these fields

private m_exampleField;
private _otherField;
public someOtherField;
private m_oneMorefield;

It gets confusing, especially since m means member and fields are all members, so not even sure how that helps with properties in the first place.
Plus a new guy may come on and then has to try and figure out why m_ is used ๐Ÿ˜…

gray mural
#

unless you don't need to access it in another classes - it's private

ashen yoke
#

because for that reason there are private methods

swift comet
#

You dont need properties (usually) if its the same class.

gray mural
swift comet
ashen yoke
#

those things are used widely, and its a convention, because properties initial and most important value is encapsulation

gray mural
ashen yoke
#

and using private properties achieves no encapsulation, as such they are useless

#

the only exception is protected virtual properties

#

that can be used to change object behavior in subclasses

swift comet
gray mural
ashen yoke
#

why do you use private properties in your script?

gray mural
ashen yoke
#

why do you use the wrong tool for the job?

swift comet
gray mural
ashen yoke
#

not anymore

swift comet
gray mural
ashen yoke
#

i explained above

gray mural
#

you mean for working in teams, or?

ashen yoke
#

encapsulation

#

the main reason property exists

#

no encapsulation, means property is useless

#

it is better replaced with a method

swift comet
gray mural
ashen yoke
#

yes

gray mural
thin aurora
#

Why not just let foobar use old conventions and let them figure out the drawbacks themselves? I remember when I learned of all these different conventions and I constantly switched to new ones because I thought they were better

#

It was quite a phase

gray mural
swift comet
#

Ye for the better

ashen yoke
#
private float _speed;

private void SetSpeedInternal(float value) => value / 100f;
gray mural
#

not more readable

ashen yoke
#

you left the scope of cosmetic readability already

#

you are in the grounds of valid patterns and correct api design

#

this goes beyond just using a prefix

gray mural
#

๐Ÿ™‚

ashen yoke
#

i tried ๐Ÿ™‚

gray mural
#

@ashen yoke @swift comet @thin aurora @lean sail @winged mortar thank you all for your help and time!

winged mortar
#

๐Ÿซก

strange ferry
#

Hello guys, not sure where to ask but..
I'm trying to generate a mesh based on Vector3, but when it comes to generating the triangles needed for the faces to show up I can't get my head around it, I'm very new to mesh generation... Is there a way to automatically generate triangles within unity? If not, do you have a recommended github page or algorithm that you usually use? Thanks!

ashen yoke
cosmic ermine
#

is this error anything to worry about?

thin aurora
cosmic ermine
thin aurora
#

If the error persists and/or troubles you during your work you can also try updating your Unity version, assuming you're not using an LTS version

ashen yoke
#
float Foo {get; set;}
void Bar(Func<float> f) {}

Bar(() => Foo); // valid
Bar(Foo); // not valid

is this solved in later c# versions?

#

i dont see why it wouldnt convert it, help me understand what is preventing

#

hm maybe because you need a lambda to capture this ?

#

right

winged mortar
#

Might just be that the properties aren't considered to be of type Func

late lion
ashen yoke
#

weird there must be some concrete reason why the opted out of it

dense ridge
#

Trying to work with splines from code is weird. How come the editor has more info on the knots than the code does, I can't even say if the knot should be continuous in code

ashen yoke
#

with AnimationCurve?

#

the Keyframe struct has that

#

Use AnimationUtility.SetKeyLeftTangentMode or AnimationUtility.SetKeyRightTangentMode

#

oh thats editor

#

anyway all it does is modify the tangent values

#

you can just look up its source and do the same

dense ridge
#

that's a fair point

#

ahh there's actually a utility function called GetAutoSmoothKnot

ashen yoke
#

im trying to understand where the tangent mode is kept

#

hm right in the
tangentMode: 1

#

why is it obsolete then

#

changes in editor serialize into this field marked obsolete, what

late aurora
#

Could someone give some tutorial resources about how to protect script fields and etc

ashen yoke
#

protect as in encapsulate?

winged mortar
ashen yoke
#

or obfuscation encryption?

winged mortar
#

These?

dense ridge
#

Since when is -1 % 10 == -1?

#

shouldn't it be 9?

ashen yoke
#

love the name of the thread

dense ridge
#

how have I never heard of that

#

that's looking better

prime mica
#

Hey guys, how would I go about calling an event to individual objects? I have an OnDamage event in my health script that invokes on a character (which is a base class for the player as well as NPCs) whenever it takes damage, but as of right now, it invokes on EVERY character. How do I get it so that it only invokes on the damaged character? sorry if this is a silly q

ashen yoke
#

is OnDamage static?

#

show code

prime mica
#

Health component : ```CS
public class Health : MonoBehaviour
{
public UnityEvent onHeal;
public UnityEvent onZeroed;

public event Action onTakeDamage;


[SerializeField] float maxHealth = 100f;
float currentHealth;

private void Awake()
{
    currentHealth = maxHealth;
}
void Start()
{

}

public void DamageHealth(float val)
{
    if (val == 0) return;
    ModifyHealth(-Mathf.Abs(val));
    onTakeDamage?.Invoke();
}
ashen yoke
#

new line after cs

#

noo i was reading

prime mica
#

oh lol

#

sorry haha

ashen yoke
#

three ticks

#

cs

#

enter

prime mica
#

Character class: ```CS

[RequireComponent(typeof(Health))]
public class Character : MonoBehaviour
{
Health health;
// Start is called before the first frame update
protected virtual void Awake()
{

    health = GetComponent<Health>();
    health.onTakeDamage += TakeDamage;
    health.onZeroed.AddListener(Die);

}

protected virtual void TakeDamage()
{
    Debug.Log(gameObject.name + ": Ouch");
}

protected virtual void Die()
{
    // base class does nothing!
}

}```

#

did not know that was a thing, ty

ashen yoke
#

i dont see an issue here

thick bough
#

public event Action onTakeDamage;

#

event shouldn't be there

ashen yoke
#

why not?

#

so what happens? you say its invoked on every character

#

but each of the health comps is unique right?

prime mica
#

yeah so uh I changed it sorry

#

it WAS static

ashen yoke
#

ok

prime mica
#

and that was calling on every character

#

but the code now, apologies

#

does nothing

ashen yoke
#

put a breakpoint in DamageHealth

#

step through see whats going on

#

do you override Awake?

#

do you call base.Awake()?

#

in the override

prime mica
#

I call the base awake function yes

ashen yoke
#

any errors?

prime mica
#

i am an actual fool

#

the only problem was the inherited NPC class, did not call the base TakeDamage() function from the character class...

#

well thank you anyway, if not for my stupid mistake I would not have found out about the sick CS code feature

ashen yoke
#

always call the base in virtuals

#

its added automatically if you tab override

finite sonnet
#

ok so i have a player management script which manages all the collisions with other things (except movement related). I have a list of structs with a string called tag and a unity event. if a collision happens and the tag of the collider matches one of the tags in the list then the corresponding unity event is run. all works fine. a nice modular way of managing things like teleporters, coins, level loading areas e.t.c. it just checks a tag and runs some functions.

but I want to use my level loading function (which is in my game manager script) but the game manager is a don't destroy on load object and so i can't reference its functions with the inspector in the unity event (its created when the game loads only once and never gets destroyed, it doesn't exist in the levels by default and must be created in the loading scene(i can make it whilst in development but it won't be there in shipping)), i can get a reference to the game manager script in the scene when it does exist but how would you go about then referencing functions in it after that as i can't use the unity events in the inspector

i thought maybe a bool saying the functions is in the gm script and then a string with the name of the functions which somehow runs them but it feels a bit janky and was wondering if anyone could thing of a better solution

playerManager - the struct (shown in inspector) :

public struct CustomCollisionEvent
    {
        public string targetTag;
        public UnityEvent eventsOnTrigger;
        public string[] eventsInGMScript;
    }

playerManager - on collision:

private void OnTriggerEnter2D(Collider2D other) {
    foreach (var eventItem in collisionEvents) {
        if (other.tag == eventItem.targetTag) {
          eventItem.eventsOnTrigger.Invoke();
          //run gameManager functs
        }
    }
}
#

playerManager - get game manager script referance:

void Start() {
    GM = (GameManager) GameObject.Find("gameManager").GetComponent("GameManager");
}

gameManager - level load:

public void LoadLevel(string LevelID) {
    SceneManager.LoadScene(LevelID);
}
ashen yoke
#

so you serialize calls to that component which in turn calls gm at runtime

#

also called facade

#

no not facade thats different thing

finite sonnet
#

thanks, idk why i didn't think of that. appreciate the advice.

#

would that effect efficiency that much?

ashen yoke
#

barely

#

not one but two direct method calls, nanoseconds

finite sonnet
#

thx, your a lifesaver. i just got myself all confused XD

ashen yoke
#

actually one delegate invokation + direct call

#

oh youd have to locate the references to desired objects as well in awake

#

all adds footprint but marginal

#

depends on how you locate your managers

#

if using GameObject.Find() then yes thats very intensive

finite sonnet
#

something like this?

void Start() {
    GM = (GameManager) GameObject.Find("gameManager").GetComponent("GameManager");
}
    public void LoadLevel(string data) {
        GM.LoadLevel(data);
    }
#

and have this one in the scene

ashen yoke
#

yes dont do it

#

is that a singleton?

#

your game manager

finite sonnet
#

game manager is a singleton

round lagoon
#

Hey I'm making a small platformer game but I'm having some trouble with the walljump mechanic. Is there any way I can send my code here or something if someone could help?

finite sonnet
#

that code is in the sandboxing script. which isn't a singleton and is in each level

ashen yoke
#

shouldnt you be using GM = GameManager.Instance;

finite sonnet
#

cos im an idiot

#

thanks again XD

#

yeah works perfecty , thanks so much

tulip temple
#

i need advice, im tinkering with a top down 2d sandbox game and im using a multi layered tilemap system for the world, my terrain gen will always be slow with a normal SetTile(); nested x & y for loop system even with me already chunking it up,, sooo i want to try use a List<int[,]> to generate and store the layer data as "raw" so i can use threading without Unity API issues. then once all the raw terrain data is made apply it with SetTile() without threading to the layers with something like SetTilesBlock();.

my issue im having is handling the List<int[,]> raw data its getting more complex than im good at dealing with

so i want to ask, should i keep pushing for this work around system, or just not use threading for my world generation. also any info on a Unity API safe threading method is also appreciated

ashen yoke
#

slow as in? you are regenerating chunks around player as you go?

tulip temple
#

no, its a set world size not infinite

#

has a border, so its all made at the start

winged mortar
#

@me when you have a resolution, I am interested in this aswell - I had issues with the composite collider being slow

ashen yoke
#

hm what i mean is you can treat tile system as just renderer

#

then chunks become renderers

#

you walk around you swap around chunks around players

#

"redrawing" them from backing data

#

i dont know how deep you are into tile system, so its just an idea

tulip temple
#

i plan on doing something like that at some point, but right now just want to get it running like it did before

#

the last version of the world gen and game as a whole used one scene for the world, switching between game "biomes" but using scenes is just better

#

my code is a huge mess right now but i can send it if that might give you an idea on what im trying to do

#

and also to tell me why im dumb here and there lol

ashen yoke
#

sorry im very tired, i can give surface level ideas but i cant delve into code

tulip temple
#

thats okay, help is help!

#

my first version used a WaitForSeconds between every chunk

ashen yoke
#

did you profile the generation as is?

tulip temple
#

but that was slow

ashen yoke
#

what was the purpose of wait for seconds?

tulip temple
#

to make it wait before making the next chunk as i couldent use the threading method i know how to use as, API issues or whatever redit was saying

ashen yoke
#

thats not how it works

tulip temple
#

well, sorta

#

it keeps it from all being made frame 1

ashen yoke
#

i mean it just doesnt affect the total generation time

tulip temple
#

i know its bad but without the async threading with the int[,] idk how else to do it

tulip temple
ashen yoke
#

did you profile it?

tulip temple
#

i dont feel i need to, i know why its slow

#

on frame one it needs to loop through up to 2500x2500 then the chunks layers, etc

ashen yoke
#

yes and still profiler can show you the bottlenecks

#

even with seemingly clear algorithms

tulip temple
#

how do i use it like that?

ashen yoke
#

for example do you use flyweight pattern?

tulip temple
#

idk what that is

ashen yoke
#

meaning for 2500*2500 you have say max of 20 objects created

#

and the rest is simple "insert reference" in a loop

#

but i havent used unity tile system, i only wrote mine previously

round lagoon
#

https://hatebin.com/hhlkamlnmy
My code for movement of my character in 2d platformer. Everything working as I'd like except wall jumping. I think this is because I limited my velocity left and right, so when trying to walljump with a value higher than that it just leads to a failed walljump. Not sure how to exclude the limit from my walljump code.

tulip temple
ashen yoke
#

that calls for abstraction

grand crane
#

i have a 2d platformer only 3 moves left,right,jump it's probably going to be easy to convert it to mobile right?

ashen yoke
#

how much time does this take without any tile allocations?

#

the whole generator without unity tiles

tulip temple
#

so without any rules that set tiles?

ashen yoke
#

with rules but without actually allocating the final tile

#

just skipping as if it was created

tulip temple
#

so just skip the final ApplyRawData() method i have, then the only data being stored is the raw int[,]

#

let me check rq

#

takes about 11 seconds to load in without the data being set onto the tile map

#

the only other thing is my backdrop code, and i already have that as good as i think i can get it

#
    void GenBackdrop()
    {
        Vector2Int size = new Vector2Int(worldWidth * 2, worldHeight * 2);
        activeBiomePipeline.backdrop.origin = Vector3Int.zero;
        activeBiomePipeline.backdrop.size = new Vector3Int(size.x, size.y, 1);
        activeBiomePipeline.backdrop.ResizeBounds();
        activeBiomePipeline.backdrop.BoxFill(Vector3Int.zero, activeBiomePipeline.backdropTile, 0, 0, size.x, size.y);
        activeBiomePipeline.backdrop.transform.position = new Vector2(-worldWidth / 2, -worldHeight / 2);
    }

its just that

ashen yoke
#

can you show screenshot?

#

trying to picture complexity

tulip temple
#

ah one sec

ashen yoke
#

11 seconds is at the same time a lot, and not much

tulip temple
#

the IEnumerator for GenMap will be removed, just haven goten to it

    public IEnumerator GenMap()
    {

        activeBiomePipeline.worldLayers.ForEach(layer => { layer.ClearAllTiles(); });
        for (int l = 0; l < activeBiomePipeline.worldLayers.Count; l++)
        {
            for (int x = 0; x < worldWidth; x += chunkSize)
            {
                for (int y = 0; y < worldHeight; y += chunkSize)
                {
                    LayerGenerationPasses(x, y, l);
                    chunkCount++;
                    yield return new WaitForSeconds(0.06f);
                }
            }
        }
        
        StampPlacement();


    }

    private void ApplyRawData()
    {
        for (int l = 0; l < activeBiomePipeline.worldLayers.Count; l++)
        {
            for (int x = 0; x < worldWidth; x++)
            {
                for (int y = 0; y < worldHeight; y++)
                {
                    
                    activeBiomePipeline.worldLayers[l].SetTile(new Vector3Int(x, y, 0), activeBiomePipeline.gameTileManager.tiles[rawWorldLayerData[l][x, y]]);
                }
            }
        }
        
    }


    public void LayerGenerationPasses(int startX, int startY, int l)
    {
        Debug.Log("running LayerGenerationPasses");
        for (int y = startY; y < startY + chunkSize; y++)
        {
            for (int x = startX; x < startX + chunkSize; x++)
            {
                float mainPerlinValue = FractalBrownianMotion(x / activeBiomePipeline.mainNoiseSettings.noiseScale, y / activeBiomePipeline.mainNoiseSettings.noiseScale,
                    activeBiomePipeline.mainNoiseSettings.octaves, activeBiomePipeline.mainNoiseSettings.lacunarity, activeBiomePipeline.mainNoiseSettings.persistence);

                //Layer0 Rule
                if (l == 0) { rawWorldLayerData[0][x, y] = 0; Debug.Log("running Layer0 Rule"); }

            }
        }
    }
ashen yoke
#

remove debugs

tulip temple
#

thats the bulk theory ive been using, just been remaking it

ashen yoke
#

console spam really affects times in editor

tulip temple
#

does what im doing make sense though? im not good at world gen but so many thing need it so i gotta get good at it at some point

ashen yoke
#

you mean using SOA?

#

or what exactly?

#

most of it is just that, looping

#

things to note

#

too many reference types in use

#

cache things like activeBiomePipeline.mainNoiseSettings.noiseScale

#

in a struct once

#

create a struct Params shove all values you need into it

#

because each dereferencing of a heap object that is not in the cache is a cache miss and as a result a memory roundtrip

tulip temple
#

noted,

ashen yoke
#

jagged arrays always were faster than mdim arrays

#

a single array is even faster

#

harder to write code tho

tulip temple
#

yea phind told me that lol

ashen yoke
#

can you test without waitforseconds?

#

just a normal method call

tulip temple
#

sure

ashen yoke
#

show FractalBrownianMotion

tulip temple
#
    private float FractalBrownianMotion(float x, float y, int octaves, float lacunarity, float persistence)
    {
        float total = 0;
        float frequency = 1;
        float amplitude = 1;
        float maxValue = 0;

        // Add the random offsets to the coordinates
        float xCoord = x + xOffset;
        float yCoord = y + yOffset;

        for (int i = 0; i < octaves; i++)
        {
            total += Mathf.PerlinNoise(xCoord * frequency, yCoord * frequency) * amplitude;
            maxValue += amplitude;
            amplitude *= persistence;
            frequency *= lacunarity;
        }

        return total / maxValue;
    }
ashen yoke
#

hm another thing to note is that

#
        [FreeFunction("PerlinNoise::NoiseNormalized", IsThreadSafe = true)]
        [MethodImpl(MethodImplOptions.InternalCall)]
        public static extern float PerlinNoise(float x, float y);
#

if you find some pure c# noise library that is actually considered fast, some simplex noise lib, you most likely will shave a lot from that time

tulip temple
#

phind linked me to a github for Fast Noise but for the life of me i couldent figure out how to use it

ashen yoke
#

well you should just do it

#

you are working with proc gen, climb those mountains

tulip temple
#

im trying lol

#

its very hard to do it well

ashen yoke
#

first - time, what is it

#

also switch to release mode

#

when testing

tulip temple
ashen yoke
tulip temple
#

OH ive been trying to send a photo lol

#

thats the peak

#

game freezes when i press the world gen button on the main menu, then once its got all the stuff done it loads

ashen yoke
#

release mode?

#

clear and disable profiler as well

tulip temple
#

is that in VS or project settings?

ashen yoke
#

no in unity

#

bottom right bug icon

tulip temple
#

i dont have that

#

i know the button you are talking about, but its not there

ashen yoke
#

which unity version?

tulip temple
#

im on unity 2019.4.31f1

ashen yoke
#

then only build will have optimizations if its not there

tulip temple
#

when i do proper tests of the speeds i build

#

its habit for me

#

i only use play mode for quick tests, then once whatever i made is working okay i do a build

ashen yoke
#

ok so its 4 seconds?

tulip temple
#

let me check in build

#

8 sec in build with a 1000x1000 world with no tiles being set, just the rawWorldLayerData

#

850m of ram from TM

#

so thats not the best at all

ashen yoke
#

check profiler, what is taking most time

tulip temple
#

how do i check that, i only know how to look at the basic read out

ashen yoke
#

switch to hierarchy, its simpler

#

but first i advice to rerun it with small map

#

otherwise it will lag like crazy

tulip temple
#

wdym switch to hierarchy

ashen yoke
tulip temple
#

oh, thanks

#

wait

ashen yoke
#

small map?

tulip temple
#

i wasent on the right part of the timeline

#

small map

ashen yoke
#

enable deep profiling

tulip temple
#

its on already

ashen yoke
#

on the top

#

ok

#

keep expanding

tulip temple
#

into backdrop or worldgen

ashen yoke
#

hm

tulip temple
#

a assume worldgen

ashen yoke
tulip temple
ashen yoke
#

well it seems unity tilemap should be dropped alltogether

#

can you expand it?

#

i assume it allocates new tile for each cell

#

anyway

#

thats the flyweight part, its later

#

keep expanding worldgen

tulip temple
#

seems like its the noise

ashen yoke
#

thats what i would from this point, shave off all the time its possible on a single thread exclusively in worldgen

#

right

#

so eliminating most things i said earlier, you would look at something like 300ms

#

for 100*100

#

thats still 187 seconds if my math is correct for 2500*2500

#

which is ..

#

unfeasable

tulip temple
#

yeaa

ashen yoke
#

also also amount of memory it will take

#

memory alone is more important factor

tulip temple
#

here is the other half btw

ashen yoke
#

as users may simply not have enough of it

#

hm strings huh

#

guess it deserializes transform? and copies name?

#

probably also marshalling

tulip temple
#

all the things i keep seeing say its fine if its slow as its just the start of the world and doesnt effect the game, but these speeds are with only the backdrop
not the 4 tilemap layers that are all assigned

ashen yoke
#

and not on every save load

#

rimworld is limited to 300*300 i think, for a reason

#

mostly pathfinding costs, and enumerations on all the pawns

#

the only radically faster, more or less guaranteed solution will be using JobSystem to generate the data

#

and for rendering you need to go procedural mesh approach

tulip temple
#

jobs scares me a lot

ashen yoke
#

thats the fastest way

tulip temple
#

i know, but it also has the API issue

#

as threding and what not

ashen yoke
#

yes you will have to completely write everything from ground up differently

#

its mostly automatic threading

#

you only need to learn to write the jobs themselves

#

pass the data, use the data from native collections, process it, return, the job system will run them in parrallel automatically

#

assuming that cuts costs on average 4 times, maybe more, 6? maybe more

#

you are still left with memory problem

#

assuming you use highly optimized rendering using mesh generation, that should be fine at runtime

#

if my math is correct a single 2d int array of 2500*2500 would take about 24414 mb

gray mural
#

Is making foreach loop with e.g. ~50 circles like 10 times per second efficient?

fervent furnace
#

since the job is not io intensive (you cant even do io in job) the number of jobs should be equal to number of logic processors to avoid the cost of thread switching

ashen yoke
#

the only reasonable solution to having a world this large is not having it as tiles/grid

#

or, loading and writing chunks to disk

#

dynamically like its done in some games using quadtree

#

you will be storing the quadtree nodes on disk

#

basically minecraft

tulip temple
#

ill look into this stuff, i still want to use the tile map system but ill tinker with more options for optimization

#

my plan was to store the world/save as a file

#

still is i guess but small steps or something

limber agate
worn rain
#

I'm making an open world 3d top down game, when player is obstructed by an object, should I fade the object or should I draw player silhouette on top? Performance wise?

winged mortar
#

is performance really that important? something like that could be achieved with shaders

potent sleet
#

Renderer feature!

unborn flame
tawny elkBOT
#
Posting code

๐Ÿ“ƒ Large Code Blocks
Large code blocks should be posted as links to services like:
https://gdl.space/, https://paste.ofcode.org/, https://hatebin.com/
https://paste.myst.rs/, https://hastebin.com/

๐Ÿ“ƒ Inline Code
Surround code with three backquotes. Not quotation marks.
To get C# formatting the first line should only contain cs or csharp.
Add a comment with a line number if there is an error message.
```cs
// Your code here
```
Do not share screenshots of code unless requested.

unborn flame
potent sleet
unborn flame
#

line 137

potent sleet
#

but it can't run

unborn flame
#

why cant it run?

potent sleet
#

wait

#

nvm h/o

potent sleet
#

I think you're printing the wrong thing

tropic quartz
#

It invokes itself before changing the state?
I suck at reading recursive code tho

#

Actually nvm, I think I read it wrong.

unborn flame
potent sleet
potent sleet
#

anyway I'm not finding that debug.log that spams , which line

#

might be checking the wrong thing

#

cause the one log does stay it goes to FreeRoam

#

Whichever one is spamming Bag Not seeing in this script

unborn flame
potent sleet
#

anyway if it's in another script you're not passing the state correctly

#

cause it does hit that Debug.Log on 137 so it's reaching it and switching it

#

you're just looking at the wrong log

#

keeping that enum in UI code is kinda smelly

#

it should be some type of global enum

#

what if you don't want to move if bag is open or w/e else

tropic quartz
#

Or did I read it wrong, I'm pretty confused by what that invoke does.
It's invoking onBack and the method it's inside is taking in an action called onBack.

If it's using new input system, is it invoking the same event that runs the method it's inside?
But then again, wouldn't that just be a stack overflow...

Does anything change if you set the state before the invoke?

Anyway I'm not the best at reading events so if that isn't the issue, I'd best stop guessing tbh.

potent sleet
#

if that invoke is meant to control the enum state somewhere it should be invoked in the action

#

like Action<MyEnum>

#

then a simple switch / if statement in the listening classes

unborn flame
#

the one spamming bag is in my playermovement script

potent sleet
#

hmm If I were you, I'd refactor this a bit

teal knot
#

is there any downside to using UI Image components on a world space canvas rather than using a sprite renderer?

rich gate
#

is it possible to see the used Unity version of a shipped game? maybe from the given .dll files? in the metadata?

potent sleet
teal knot
#

srry wasnt really sure where to ask

potent sleet
rich gate
potent sleet
rose dragon
#

I have 2 triggers that have these tags but they arent being detected when I enter them. I assume its a problem here?

mellow sigil
rose carbon
#

I have no idea why but whenever I call Sword.Use() the variable _isSwinging is always set to true.
I tried debugging it by putting a breakpoint on the set-accessor and it only breaks on awake, after that there are no other breaks.
YET the _isSwinging variable is always true whenever I try to call Use()
https://gdl.space/aruwewucuk.cs
(Use() only gets called once every MouseButtonDown(0) and the variable is not changed anywhere outside this class)

leaden ice
#

isSwinging = true;

rose carbon
#

Yes, but it never even goes to that line, since when I first call the Use function it is already set to true.
I literally put a breakpoint in the start of the use function
if (isSwinging) return;

leaden ice
#

This property is public:
public bool isSwinging

#

so it could be getting set from anywhere

#

maybe make the setter private

#

so it can't be changed externally

rose carbon
#

I just did that to be able to put a breakpoint on the setter, it was private only before, same issue

leaden ice
#

then it's coming from a previous call to Use

rose carbon
#

It's already set to true the first time Use gets called or the first time the breakpoint of Use gets called, thats the issue

leaden ice
#

Assuming you make the setter private, it can't happen

#

Add Debug.Log in Awake and in the setter and show the console when you run

rose carbon
#

^ the last log gets printed before I actually set the variable to true

leaden ice
#

and "we are in the isSwinging propery" setter or getter? Printing before or after setting it?

rose carbon
#

WeaponController Class

 if (Input.GetMouseButtonDown(0))
{
  print("Mouse button down");
  player.Stats.Weapon.Use();
}```
#

setter and after we are setting it

leaden ice
#

was the code you shared the full script?

#

also do you have Collapse enabled on your console window?

rose carbon
#

Yes, and no I did not have collapse enabled

rose carbon
#

@leaden ice I think I found the problem, unity keeps my variables even after restarting the play mode, no idea why that happens but the counter didnt reset after I restarted the play mode

leaden ice
#

and only if there was no domain reload when you hit play

rose carbon
leaden ice
#

yeah makes sense

rose carbon
#

Okay I give up, even when checking that box and restarting Unity I still have the same issue.

white isle
steady moat
rose carbon
#

What do you mean with type?

steady moat
rose carbon
#

Yes the subclass Weapon is a MonoBehaviour

mortal summit
#

hi first time here!
i already searched the internet and i cant find anything that works or anyone with the same problem.

i have a position where i want to check if the player is standing there. I decided to use BoxCast to check but it dosent detect the player.

            Debug.Log("Hit Something");
        } else {
            Debug.Log("No Collision detected");
        }

position: is where i want to check
start: is the object that is there where i will take the collider to get the size (collider is trigger)
and layer is set to the same layer then the player.
but it doesn't detect the player if its inside.
All layers are set to collide with each other.

i also check with Gizmos and it is the right spot i want to check

Gizmos.DrawWireCube(position, start.GetComponent<Collider2D>().bounds.size);
steady moat
rose carbon
white isle
# steady moat Align the arrow with the velocity/direction of the object by using `transform.fo...

transform.rotation = Quaternion.Euler(180f, 0f, 0f);
didnt work


public class Projectile : MonoBehaviour
{
    public float lifetime = 5f;
    public float destroyDelay = 0.2f;

    private bool hasHit = false;
    private GameObject playerObject; // Reference to the player object

    private void Start()
    {
        Invoke(nameof(DestroyProjectile), lifetime);

        // Find the player object by layer
        int playerLayer = LayerMask.NameToLayer("Player");
        GameObject[] playerObjects = GameObject.FindGameObjectsWithTag("Player");
        foreach (GameObject player in playerObjects)
        {
            if (player.layer == playerLayer)
            {
                playerObject = player;
                break;
            }
        }
    }

    private void OnCollisionEnter(Collision collision)
    {
        if (!hasHit)
        {
            hasHit = true;
            // Perform any actions or effects on hit

            DestroyProjectile();
        }
    }

    private void Update()
    {
        if (playerObject != null)
        {
            Vector3 targetDirection = playerObject.transform.position - transform.position;
            Quaternion rotation = Quaternion.LookRotation(targetDirection, Vector3.up);
            transform.rotation = rotation;
        }
    }

    private void DestroyProjectile()
    {
        // Perform any cleanup or effects before destroying the projectile
        Destroy(gameObject, destroyDelay);
    }
}```

also didnt work
steady moat
rose carbon
#

I mean not that it would make sense, I also checked with breakpoints, all really strange

steady moat
steady moat
#

Is it align with the forward ?

white isle
#

if thats what you mean

tropic quartz
#

Might be able to just change the rotation in import settings.
Although if you are already using the same arrow model in other places then those would rotate, too, if you change it now.

mortal summit
steady moat
white isle
white isle
tropic quartz
#

I mean the import settings for the 3D mesh

steady moat
white isle
white isle
steady moat
#

Create a child and rotate your arrow.

white isle
#

rotate it to the blue?

steady moat
#

So it points in the blue direction.

white isle
steady moat
#

To not fuck up the rest of your game.

white isle
#

oh ok lol

white isle
#

i dont understand lol

#

||sry btw||

steady moat
wintry crescent
#

I am super confused - I have an object with a rigidbody, I have a camera with a raycaster, and I have a childobject of that rigidbody with a collider
Now - I'm trying to detect if the raycast hits the childobject, but that's not working - unless I either remove the rigidbody, or add a rigidbody to the object with the collider as well. I am SO confused. Why does it happen?

steady moat
#

The rotate the 1.

tropic quartz
#

Yeah nvm the import settings, might be better to just do what Simferoce is suggesting.

Basically if a character is firing arrows at a certain rotation, but the rotation is funky, you can make the 3D model be a child of the actual (empty) object that's being fired.
Then just rotate the child object (with the model) to face the right direction.

white isle
steady moat
#

Try tht

white isle
#

it kinda works....?
ill send a vid

steady moat
#

However, I m pretty sure you gonna need to rotate it in the inverse of the rotation being apply

#

Not the actual forward

#

But something like the inverse of the forward in your situation

molten sandal
#

Hi there people, I am in need of help because I've been strugling with the same issue for MONTHS now....

I've tried many methods and no one works for me

This is a simple camera follow script I made based on the simplies tutorial I've found:

using UnityEngine;

public class VehicleCameraController : MonoBehaviour
{
    public float cameraSpacing = 6f;
    public float followSpeed = 100f;
    public float rotationSpeed = 100f;
    private float angle;
    private float distance;

    private Vector3 cameraPosition;
    private Vector3 smoothPosition;

    public Transform target;

    private void FixedUpdate()
    {
        distance = Mathf.Abs(target.position.magnitude - transform.position.magnitude);
        cameraPosition = target.position - (target.forward * cameraSpacing);
        smoothPosition = Vector3.Lerp(transform.position, cameraPosition, (followSpeed + distance) * Time.fixedDeltaTime);
        transform.position = smoothPosition;

        angle = Mathf.Abs(Quaternion.Angle(transform.rotation, target.rotation));
        transform.rotation = Quaternion.RotateTowards(transform.rotation, target.rotation, (rotationSpeed + angle) * Time.fixedDeltaTime);
    }
}```

For some reason values higher than 100f for both 'followSpeed' and 'rotationSpeed' variables just don't work, this wouldn't be a problem for a low velocity vehicles, but this vehicle goes 600km/h and faster leaving the camera waaaaaaaaaaaaaaay behind it and I can't compensate it any way even if I square the distance or multiply by the distance instead of adding it, it doesn't work, I can't make the camera to catch up with the vehicle.

I am about to give up, I just can't figure out what it is, I've tried to run the camera calculations in both Update() and LateUpdate() and it looks like crap, too much jitter in both of those methods.

If you have any sugestion, please, I would appreciate it, may be the issues is somewhere else, not necesary in the camera itself.
white isle
white isle
steady moat
white isle
#

should i send it?

#

if you dont trust the link ill send the full script here because some people didnt want ot click it in the past lol

steady moat
#

You could set the rotation here

#

And remove the rotation from your arrow script.

#

Only need to set it once

white isle
#

set it as the second one?
the one you circled?

steady moat
#

Yes

white isle
#

transform.rotation = Quaternion.Euler(0f, 0f, 90f);
is that good?

#

or should i do it in a different way?

steady moat
#

In fact, you might not even need to change your code.

#

Just delete the other code.

white isle
#

ill try

#

@steady moat you will get credit in that game
you legend

steady moat
molten sandal
#

What function could be used instead?

steady moat
#

Why you use a lerping ?

tropic quartz
# molten sandal Hi there people, I am in need of help because I've been strugling with the same ...

The way (heh) lerp works is, the first parameter you give it a start position.
The second parameter is the end position.
The third parameter is a value between the start and the end.

If start is 0f and end is 100f, 0.5f in the third spot would make it return 50f.
Because it's 50% of 100.
So a lerp with third value of 1f is pretty much the same as just having it follow the end point with no smoothing.

Probably why at really big values it's no longer truly lerping.

Also fixed update runs 50 times per second, so if you move at truly insane speeds it would probably get choppy eventually.

molten sandal
#

I want the camera to follow a vehicle, but I don't want to aprent it directly, because it looks motionless and weird

#

I want a camera to smoothly follow the vehicle

static lantern
#

Does anyone have a solution for using SQLite with Unity that doesn't involve using the Mono version? I'd prefer to use the System version

steady moat
tropic quartz
#

Have you tried cinemachine?
I haven't tried it at crazy high speeds but it's official and free.

That is unless you wanna code the camera yourself, which I can respect.

steady moat
leaden ice
#

not with the correct settings

steady moat
molten sandal
# steady moat Define smoothly.

ok

I want the camera to keep x distance to the vehicle, for example I want it to be 6m bahind it, but when it accelerates I want it to be left behind a little bit and when the vehicle deselerates, I want it to get a little beet closer

steady moat
#

(In this situation)

leaden ice
#

missing from the description of this problem is a description of how the car is moving

edgy lynx
#

Cinemachine has all the settings to simulate what he wants, with damping, distance, etc.

topaz plaza
#
ArgumentNullException: Value cannot be null.
Parameter name: _unity_self
UnityEditor.SerializedObject.FindProperty (System.String propertyPath) (at /Users/bokken/build/output/unity/unity/Editor/Mono/SerializedObject.bindings.cs:74)
UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.BindPropertyRelative (UnityEngine.UIElements.IBindable field, UnityEditor.SerializedProperty parentProperty) (at /Users/bokken/build/output/unity/unity/ModuleOverrides/com.unity.ui/Editor/Bindings/BindingExtensions.cs:169)
UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.BindTree (UnityEngine.UIElements.VisualElement element, UnityEditor.SerializedProperty parentProperty) (at /Users/bokken/build/output/unity/unity/ModuleOverrides/com.unity.ui/Editor/Bindings/BindingExtensions.cs:113)
UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.ContinueBinding (UnityEngine.UIElements.VisualElement element, UnityEditor.SerializedProperty parentProperty) (at /Users/bokken/build/output/unity/unity/ModuleOverrides/com.unity.ui/Editor/Bindings/BindingExtensions.cs:39)
UnityEditor.UIElements.Bindings.DefaultSerializedObjectBindingImplementation+BindingRequest.Bind (UnityEngine.UIElements.VisualElement element) (at /Users/bokken/build/output/unity/unity/ModuleOverrides/com.unity.ui/Editor/Bindings/BindingExtensions.cs:1126)
UnityEngine.UIElements.VisualTreeBindingsUpdater.Update () (at /Users/bokken/build/output/unity/unity/ModuleOverrides/com.unity.ui/Core/Bindings/VisualTreeBindingsUpdater.cs:272)~

Can anyone tell me what this error is about? I can't really find the source of the issue

leaden ice
edgy lynx
molten sandal
leaden ice
steady moat
edgy lynx
leaden ice
#

also the fact you have jitter in LateUpdate means your car is probably not using interpolation

steady moat
rose carbon
#

Does anyone know if there is a way for me to check if the instantiated gameobject is unique? Maybe a way for me to debug the issue

edgy lynx
steady moat
# molten sandal No

Add a value to modulate the paramter of the lerp to increase the rigidity.

steady moat
molten sandal
#

I would love to avoid Cinamechine if possible, it's kind of complex to understand and has a lot of configurations and setup to do, I would prefer to implement something simple

rose carbon
leaden ice
#

a few clicks and you're done

#

as opposed to MONTHS (your words) of fiddling with scripts

west lotus
edgy lynx
#

But honestly, he should just use Cinemachine. It has this built in and so easy to use.

molten sandal
#

Cinemachine tenso

edgy lynx
molten sandal
#

Let me try smoothdamp first

steady moat
topaz plaza
static lantern
#

Try to clear it, and if it doesn't clear, restart Unity. If it still doesn't let you enter playmode, submit a bug report.

topaz plaza
static lantern
#

Because the error isn't referencing anything you wrote

#

And also

#

Experience.

topaz plaza
#

Yeah ok but maybe I screwed something up with using the built-in libraries wrong?

static lantern
#

Then the error would reference the file you wrote

#

Jesus, gonna sit there and fucking argue because the answer isn't what you were expecting

steady moat
topaz plaza
#

it's causing my Play Mode to pause every time

#

that makes it somewhat annoying

molten sandal
#

Nein, SmoothDamp didn't help

steady moat
# molten sandal Nein, SmoothDamp didn't help

Try to play with the damping value with the following.

using UnityEngine;

public class VehicleCameraController : MonoBehaviour
{
    public float cameraSpacing = 6f;
    public float followSpeed = 100f;
    public float rotationSpeed = 100f;
    public float damping = 1.0f;
    private float angle;
    private float distance;

    private Vector3 cameraPosition;
    private Vector3 smoothPosition;

    public Transform target;

    private void FixedUpdate()
    {
        distance = Mathf.Abs(target.position.magnitude - transform.position.magnitude);
        cameraPosition = target.position - (target.forward * cameraSpacing);
        smoothPosition = Vector3.Lerp(transform.position, cameraPosition, damping * Time.fixedDeltaTime);
        transform.position = smoothPosition;

        angle = Mathf.Abs(Quaternion.Angle(transform.rotation, target.rotation));
        transform.rotation = Quaternion.RotateTowards(transform.rotation, target.rotation, (rotationSpeed + angle) * Time.fixedDeltaTime);
    }

molten sandal
#

hm... that damping is pretty much the same as followSpeed

#

You know, I can't get the damping aproach

#

I am going to offset the target position

#

based on the distance between the camera and the object it follows

#

that should compensate

molten sandal
#

lol

#

Vector3.Lerp, Vector3.MoveTowards and Vector3.SmoothDamp give the same result

#

Values higer that 100 don't work

heady iris
#

Show your code.

molten sandal
#

I came up with this:

using UnityEngine;

public class VehicleCameraController : MonoBehaviour
{
    public float cameraSpacing = 6f;
    public float followSpeed = 1f;
    public float rotationSpeed = 1f;
    public float angle;
    public float distance;

    private Vector3 cameraPosition;
    private Vector3 smoothPosition;

    public Transform target;
    public Rigidbody rb;

    private void FixedUpdate()
    {
        distance = Vector3.Distance(target.position, transform.position);
        cameraPosition = target.position - (target.forward * cameraSpacing);
        smoothPosition = Vector3.MoveTowards(transform.position, cameraPosition, (followSpeed + rb.velocity.magnitude) * Time.fixedDeltaTime);
        transform.position = smoothPosition;

        angle = Mathf.Abs(Quaternion.Angle(transform.rotation, target.rotation));
        transform.rotation = Quaternion.RotateTowards(transform.rotation, target.rotation, (rotationSpeed + angle) * Time.fixedDeltaTime);
    }
}

The:

smoothPosition = Vector3.MoveTowards(transform.position, cameraPosition, (followSpeed + rb.velocity.magnitude) * Time.fixedDeltaTime);```

Works exactly as:

```haskell
smoothPosition = Vector3.Lerp(transform.position, cameraPosition, 100f * Time.fixedDeltaTime);```
heady iris
#

Well, yes, 100 times 0.02 is more than one

#

Lerp will instantly reach the destination

#

Read the documentation for the methods youโ€™re using

tropic quartz
#

I mean... like I told you earlier, the third number is a percent of a sort. And at 1f it's same as the end pos.

molten sandal
#

The only way out I see is that I can not use Unity methods, I have to write my own

heady iris
#

no, you need to read the documentation.

#

you're using a hammer to drive a screw and then acting surprised when you get a bad result

#

MoveTowards's third argument is the distance to move. Lerp's third argument is the percentage to move.

SmoothDamp's fourth argument is the amount of time it should take to reach the target.

molten sandal
#

yep, lerp uses 0 and 1

desert shard
#

with this information you should be good to go

molten sandal
#

So I used:
smoothPosition = Vector3.SmoothDamp(transform.position, cameraPosition, ref velocity, 0f);
And the result is pretty much the same, the camera keeps getting behid the ship

heady iris
#

Did you read the documentation?

desert shard
#

why would you use smoothdamp if you want the movement to happen instantly?

heady iris
#

You are using SmoothDamp in a nonsense way.

desert shard
#

what

steady moat
heady iris
#

i mean, they're using SmoothDamp with a smoothing time of 0 seconds

#

i'm not even sure how that will behave

#

i guess it'll snap to the target instantly. probably.

molten sandal
heady iris
#

use SmoothDamp with a pretty fast smoothing time and the camera will very aggressive catch up with whatever it's following

molten sandal
#

But you can see the diference in distance when the ship is still, when it moes at 500 and faster

heady iris
#

Show us the entire method again.

#

I am curious to see where velocity is declared, among other things

tropic quartz
#

https://docs.unity3d.com/ScriptReference/Vector3.SmoothDamp.html

"Current, target, current velocity, max speed".

So your current spot is transform,position, your target is cameraPosition.
So you are moving something towards the camera?

I dunno what you put in velocity and you set the time to 0 (instant movement?)

So you are instantly moving something to the camera if I'm reading this right?

molten sandal
#
using UnityEngine;

public class VehicleCameraController : MonoBehaviour
{
    public float cameraSpacing = 6f;
    public float followSpeed = 1f;
    public float rotationSpeed = 1f;
    public float angle;
    public float distance;

    private Vector3 cameraPosition;
    private Vector3 smoothPosition;
    private Vector3 velocity = Vector3.zero;


    public Transform target;
    public Rigidbody rb;

    private void FixedUpdate()
    {
        //distance = Vector3.Distance(target.position, transform.position);
        cameraPosition = target.position - (target.forward * cameraSpacing);
        smoothPosition = Vector3.SmoothDamp(transform.position, cameraPosition, ref velocity, 0f);
        transform.position = smoothPosition;

        angle = Mathf.Abs(Quaternion.Angle(transform.rotation, target.rotation));
        transform.rotation = Quaternion.RotateTowards(transform.rotation, target.rotation, (rotationSpeed + angle) * Time.fixedDeltaTime);
    }
}

steady moat
heady iris
#

I don't know how SmoothDamp behaves when the damping time is zero seconds.

steady moat
#

Reduce your speed and the size of your map.

heady iris
#

consider a non-zero duration.

molten sandal
#

What if I make everything slow, but I run the simulation twice as fast

heady iris
#

none of this is relevant for the camera movement problem

molten sandal
#

Because the goal here is to make a fast racing game

heady iris
#

also, if you want to sanity-check the camera positioning, just directly move the camera to the target position

#

if it still lags behind, then you have something else going on here

#

perhaps you are changing cameraSpacing elsewhere, for example

molten sandal
heady iris
#

No, I didn't say to do that.

molten sandal
heady iris
#

But that is something I was considering. It's possible that things are happening out-of-order and leaving the camera one frame behind.

#

i.e. the vehicle moves after the camera moves

#

This could cause it to appear increasingly far behind as the vehicle accelerates

molten sandal
#

Holy

#

there is something very wrong

#
private void FixedUpdate()
    {
        //distance = Vector3.Distance(target.position, transform.position);
        targetCameraPosition = target.position - (target.forward * cameraSpacing);
        //smoothPosition = Vector3.SmoothDamp(transform.position, targetCameraPosition, ref velocity, 0f);
        transform.position = targetCameraPosition;

        angle = Mathf.Abs(Quaternion.Angle(transform.rotation, target.rotation));
        transform.rotation = Quaternion.RotateTowards(transform.rotation, target.rotation, (rotationSpeed + angle) * Time.fixedDeltaTime);
    }```