unity – How to go back to the old input system?

I have tried using Unity’s new input system, but it’s incredibly hard to learn, the documentation is poor, and the old system is much easier to work with. I would like to revert my project back to the old system, but I am unsure how to do that. How do i disable the package and reconfigure my project so it will accept code using the old system?

c# – Run a Script at Game Startup in Unity

c# – Run a Script at Game Startup in Unity – Game Development Stack Exchange

unity – What controls the platform override codes in AudioClip (and maybe other Assets) .meta files?

My game needs to use Unity 2019.3 for the console versions, and 2020.3 for the mobile ports.

The Console Project is the master project. Virtually any changes done to it are replicated to the Mobile project.

I would like to setup import overrides for Android (e.g. the compression quality for my soundtracks). To be consistent, I perform this change in my Console Project using the “Override for Android” checkbox (which affects the .meta file), and then replicate the change to the Mobile Project.

Ideally, the Mobile Project will now reflect the compression quality I want for said soundtracks, but it actually doesn’t – in fact, the “Override for Android” checkbox does not appear to be checked in the Mobile Project, despite having a .meta file identical to the Console Project.

I digged into the .meta files, and noticed that when I use “Override for Android” in my Console Project, the .meta file adds an entry code 27:

  platformSettingOverrides:
    27:
      loadType: 2
      sampleRateSetting: 0
      sampleRateOverride: 44100
      compressionFormat: 1
      quality: 0.5
      conversionMode: 0

But, when I go to my Mobile Project and check the checkbox, it adds an entry coded 7:

  platformSettingOverrides:
    7:
      loadType: 2
      sampleRateSetting: 0
      sampleRateOverride: 44100
      compressionFormat: 1
      quality: 0.5
      conversionMode: 0

I don’t understand this inconsistency. Does anyone have any insight on the exact conditions/rules Unity uses for the platform codes in the .meta files?

I can only think of a few dubious reasons:

  1. Maybe they changed from 2019 to 2020.
  2. My Console Project actually supports more platforms than the Mobile Project (the consoles, obviously), so maybe they “pushed” the Android code from 7 to 27.

It is not the end of the world; I could make a script that duplicates the settings of 27 into 7… But only if this behaviour is predictable or consistent. So my question is, what is Unity’s logic behind the choice of these platform codes?

Note: so far I’ve only found this behaviour on AudioClip files. With Textures, I see that it actually uses the name of the platform in the .meta file, so the settings do work thankfully, but it is even more puzzling because now I don’t understand why Textures and AudioClips use different conventions to begin with… What other Asset Type has numeric conventions?

Spawn point not working on unity

I have two GameObjects set down. One is named Spawn Point, and it is set at the beginning of my level, and Death Point, set down at about 0,-40,0. My code tries to see if the character is below the Death Point area and moves the player to the spawn point. I feel that the answer is so simple, that I could look it up, but I looked it up, and I didn’t see it.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class death : MonoBehaviour
{
    public GameObject spawnPoint;
    public GameObject deathPoint;
    // Start is called before the first frame update

    // Update is called once per frame
    void Update()
    {
       Vector3 playerPos = transform.position;
        if(playerPos.y < deathPoint.transform.position.y) {
            transform.position = spawnPoint.transform.position;
        }
    }
}

A big thank you in adavance.

c# – Unity freezes when running BFS update instead of visualizing each step

Your code is written under the assumption that the player can see directly into the memory of the program as it’s running. So as long as you change a variable, the player should be able to immediately observe that change, like if you’d flipped a physical card on the table in front of them.

Games don’t work that way. If we want a player to see something change in our world, we need to draw it to an image and put that image on the player’s screen first. That takes some work from the CPU to initiate. And the CPU can’t do that work if it’s busy doing something else.

A typical game loop looks something like this, in a simplified model:

  1. Process window messages – this is what keeps the application from hanging, and allows it to read user input provided from the OS

  2. Update the game state, running any per-object update scripts

  3. Render the resulting frame and present it to the screen

  4. Sleep until it’s time for the next frame

This most often runs single-threaded, and that’s the case for the Unity game loop too. (Even systems that use threading typically use it to “go wide” in a single step, like running multiple non-conflicting state updates in parallel in step 2, while still finishing all step 2 work before taking a state snapshot to render in step 3)

So, as long as you’re keeping the CPU busy in step 2, it never gets to step 3, so you never get to see an intermediate state rendered. And it also never gets back to step 1, which is what makes the app go unresponsive until your script finishes its work.

The culprit is your MyDelay function:

public static void MyDelay(float seconds)
{
    DateTime dt = DateTime.Now + TimeSpan.FromSeconds(seconds);
    do { } while (DateTime.Now < dt);
}

This is what’s called a “Busy Wait” – it tells the CPU to sit there checking and re-checking the time, burning millions of cycles doing nothing useful at all, until it’s allowed to continue on to the next line of code. You’ve temporarily suspended any real processing and turned this CPU core into a space heater.

(There are times when a busy wait is appropriate, when you think you’ll very likely be waiting less time than it takes to sleep and wake a thread. A busy loop running for a hundredth of a second as you have here is never a good idea, and you should throw away any tutorial that told you to do that)

Because you’ve kept this thread busy this whole time, it’s not allowed to exit step 2 and continue on to step 3, rendering your changes for the player to see them, until the whole routine has finished. At that point it shows only the finished state.

The better way to do something like this in Unity is to use Coroutines. This is a function that can run partially, then yield control flow back to the code that called it, so the CPU can finish the rest of step 2 and draw it in step 3. Then, in the next iteration of the game loop, the calling code can resume your coroutine right where it left off, and continue until the next yield. In this way it takes turns with all the other work needed to update and render your scene, rather than hogging the CPU to itself until it’s finished.

Here’s an example of what that could look like. Along the way I also cleaned up some issues:

  • Used a descriptive enumeration instead of magic numbers to define the different tile states
  • Swapped empty and wall so we get the border around the map for free from default initialization
  • Used the built-in Vector2Int type instead of a tuple that needs a new add method defined
  • Reduced the number of temporary collections allocated during the search
public class BFS : MonoBehaviour
{
    public enum TileState {        
        Wall,
        Empty,
        Start,
        Goal,
        Visited,
        Current
    }

    static readonly Vector2Int() MOVES = new Vector2Int() {
        Vector2Int.right, Vector2Int.up, Vector2Int.left, Vector2Int.down
    };

    public float delayPerStep = 0.01f;

    TileState(,) InitializeSearchSpace(Block(,) blocks, Queue<Vector2Int> startingPoints, out int goalCount) {
        goalCount = 0;

        int width = blocks.GetLength(0);
        int height = blocks.GetLength(1);
        TileState(,) area = new TileState(width + 2, height + 2);

        for (int x = 0; x < width; x++) {
            for (int y = 0; y < width; y++) {
                TileState state = blocks(x, y).GetState();
                switch (state) {
                    case TileState.Start:
                        startingPoints.Enqueue(new Vector2Int(x + 1, y + 1));
                        break;
                    case TileState.Goal:
                        goalCount++;
                        break;
                }
                area(x + 1, y + 1) = state;
            }
        }

        return area;
    }

    IEnumerator RunSearch(Block(,) blocks, TileState(,) area, Queue<Vector2Int> openSet, int goalsRemaining) {

        float timeBudget = Time.deltaTime;

        while (openSet.Count > 0 && goalsRemaining > 0) {
            var tile = openSet.Dequeue();

            foreach(var move in MOVES) {
                var next = tile + move;
                switch(area(next.x, next.y)) {
                    case TileState.Wall:
                    case TileState.Visited:
                        continue;                    
                    case TileState.Goal:
                        goalsRemaining--;
                        break;
                }
                blocks(next.x - 1, next.y - 1).SetState(TileState.Current);

                timeBudget -= delayPerStep;
                while (timeBudget <= 0f) {
                     yield return null;
                     timeBudget += Time.deltaTime;
                }

                blocks(next.x - 1, next.y - 1).SetState(TileState.Visited);
                area(next.x, next.y) = TileState.Visited;

                openSet.Enqueue(next);
            }
        }
    }

    public void BreadthFirstSearch(Block(,) blocks) {
        var openSet = new Queue<Vector2Int>();

        var area = InitializeSearchSpace(blocks, openSet, out int goalCount);

        StartCoroutine(RunSearch(blocks, area, openSet, goalCount));
    }
}

unity – Instantiate from two values, each in separate array

I have a problem with Instantiating GameObjects from floats in arrays, which I have to merge into one Vector2 (or Vector3). I have only two values in arrays:

GameObject = somePrefab
float() = value1
float() = value2

First I have to combine them into a vector, then Instantiate as many prefabs as the combined vector number is…

Can anybody help me with how to merge these floats into a vector and instantiate?

raycasting – Pinch Gesture for Function Other than Zooming- Unity

I’m working on a game in which I want to pinch outwards on a character, which causes the character to split into 2 smaller copies of itself.

The method I’m thinking about using is to have the “pinch out” gesture destroy the game object and simultaneously create two instances of the smaller game object, and have them follow the fingers that pinch out. The action would also be reversible with the “pinch in” function.

My idea would be to do a raycast to detect the two-finger touch on the object (would I need a collider for that?), then use the beginning and ending touch points to determine if it is pinching out or in.

The problem is I am brand new to Unity and C# and have no idea how to write all of this. All of the tutorials for multi-touch gestures have to do with camera zoom, which is not what I am going for.

Can anyone tell me if I’m on the right track with my logic and provide some guidance on writing the code?

Can I reset the `[DisallowMultipleComponent]` attribute in child classes in Unity somehow?

Can I reset the (DisallowMultipleComponent) attribute in child classes in Unity somehow?

I want to create my base class:

(DisallowMultipleComponent)
public class MyCompanyClass : Monobehaviour {}

I want all my component scripts to inherit from the MyCompanyClass. So, I will be able to add only one instance of my component script to a game object at a time. I need it for most of my classes. But for some classes I still want to preserve the ability to add a few class instances to a game object, while still inheriting from the MyCompanyClass (because I am going to put some custom behavior into it, which is going to be used by all my component scripts).

So, I was trying to search for something like (AllowMultipleComponent), but found nothing. Is there maybe another way to achieve what I want?

unity png sequence animation chaning position in animation

We are making frame by frame animation and we have a canvas size for animation. Attack animation is changing position in canvas. In unity after attack animation completed it teleports back to the initial position. I tried setting animation event and updating parent position but it does not work.it teleports to nonsense locations like player is looking at +x but it teleports to -x. Anybody have solution for this ? since it is not rigged root motion is not working

unity – Snapping to z or x rotation values with Quaternion.Lerp

My player is a cube. I want to restrict/prevent its rotation around global y axis. I am doing this using Quaternior.Lerp since I do not want to completely constraint rotation around y axis, I just want it to quickly snap back to original rotation after a small angular displacement. The problem I have is that every time the cube flips to the side around x or z axis, the local y axis is not parallel to the global y axis anymore and I need to select new local axis and restrict/prevent cube’s rotation around the new axis. My way of trying to resolve this problem was the following:

  1. Constantly keep track of which side of the cube is “at the bottom”. I do this by figuring out which side center has lowest y position value;
  2. Based on that, figure out which local axis is the closest to being parallel to the global y axis;
  3. Use Quaternion.Lerp to interpolate from the current rotation to new rotation. Each time, two of the axis values stay the same, while the third one is being set to the closest 90° angle from the current value (e.g., for x axis: Mathf.Round(transform.rotation.eulerAngles.x / 90) * 90)

I wrote a method named autoCorrectRotation(Vector3 rotation) that I call in FixedUpdate() as autoCorrectRotation(transform.eulerAngles);.

Here is that method:

private void autoCorrectRotation(Vector3 rotation)
    {
        float eulerXX = rotation.x;
        float eulerYY = rotation.y;
        float eulerZZ = rotation.z;

        // Create 6 normal vectors for 6 sides of the cube
        Vector3 vectorDown = Quaternion.Euler(eulerXX, eulerYY, eulerZZ) * Vector3.down;
        Vector3 vectorRight = Quaternion.Euler(eulerXX, eulerYY, eulerZZ) * Vector3.right;
        Vector3 vectorUp = Quaternion.Euler(eulerXX, eulerYY, eulerZZ) * Vector3.up;
        Vector3 vectorLeft = Quaternion.Euler(eulerXX, eulerYY, eulerZZ) * Vector3.left;
        Vector3 vectorForward = Quaternion.Euler(eulerXX, eulerYY, eulerZZ) * Vector3.forward;
        Vector3 vectorBack = Quaternion.Euler(eulerXX, eulerYY, eulerZZ) * Vector3.back;

        List<Vector3> cubeNormalVectorAllA = new List<Vector3>();
        cubeNormalVectorAllA.Add(vectorDown);
        cubeNormalVectorAllA.Add(vectorRight);
        cubeNormalVectorAllA.Add(vectorUp);
        cubeNormalVectorAllA.Add(vectorLeft);
        cubeNormalVectorAllA.Add(vectorForward);
        cubeNormalVectorAllA.Add(vectorBack);

        // Sort normal vectors by ascending y value
        cubeNormalVectorAllA.Sort((a, b) => a.y.CompareTo(b.y));


        Vector3 vectorLowestY = cubeNormalVectorAllA(0);
        
        // Restrict the movement based on which side is "at the bottom"
        if (vectorLowestY == vectorDown || vectorLowestY == vectorUp)
        {
            transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(transform.rotation.eulerAngles.x, Mathf.Round(transform.rotation.eulerAngles.y / 90) * 90, transform.rotation.eulerAngles.z), 1f);
            Debug.Log(gameObject.tag + ": DOWN or UP");
        } 
        else if (vectorLowestY == vectorLeft || vectorLowestY == vectorRight)
        {
            transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(Mathf.Round(transform.rotation.eulerAngles.x / 90) * 90, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z), 1f);
            Debug.Log(gameObject.tag + ": LEFT or RIGHT");
        }
        else if (vectorLowestY == vectorForward || vectorLowestY == vectorBack)
        {
            transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, Mathf.Round(transform.rotation.eulerAngles.z / 90) * 90), 1f);
            
            Debug.Log(gameObject.tag + ": FORWARD or BACKWARD");  
        }

    }

This works exactly as I wanted only in the first case, where local y and global y are parallel, meaning when either one of original top and bottom sides of the cube are on the bottom. As soon as any of the other 4 sides are on the bottom and the rotation needs to be restricted/prevented for x or z axis, the Quaternion.Lerp does not work. Some laggy/jittering movement is present, but the rotation is not snapping to the closest 90° angle.

Any ideas on what could be wrong or if there is some other way to achieve what I want?

DreamProxies - Cheapest USA Elite Private Proxies 100 Private Proxies 200 Private Proxies 400 Private Proxies 1000 Private Proxies 2000 Private Proxies ExtraProxies.com - Buy Cheap Private Proxies Buy 50 Private Proxies Buy 100 Private Proxies Buy 200 Private Proxies Buy 500 Private Proxies Buy 1000 Private Proxies Buy 2000 Private Proxies ProxiesLive Proxies-free.com New Proxy Lists Every Day Proxies123