New answers tagged

0 votes

Trying to generate a Tile labyrinth in Unity

It is a solid block of walls because you tell it to generate a solid block of walls. ...
user avatar
  • 2,340
0 votes

Gyroscope Parallax Effect In Unity

Unity-Gyroscope-Parallax There is a free plugin for gyroscope parallax effects, it can move object position and/or rotate object in space via gyroscope input data as gravity vector or as a movement in ...
user avatar
0 votes

How to flip enemy patrolling side to side with Mathf.PingPong?

You can do this without an extra member variable, and without overshooting past min/max. "Moving left" just means your new x is less than your old x. So, compare to see which one is bigger. <...
user avatar
  • 114k
0 votes

How to flip enemy patrolling side to side with Mathf.PingPong?

Try it, simple and easy way to flip in a specific range. It worked for me. ...
user avatar
0 votes

How to flip enemy patrolling side to side with Mathf.PingPong?

The problem with the Mathf.PingPong function is that it doesn't tell you if the returned value is from the "Ping" or the "Pong" phase. When you ...
user avatar
  • 112k
0 votes

Send an array of struct to compute shader

For anyone that might have a similar question, here's how I iterate over the buffer: ...
user avatar
  • 13
0 votes
Accepted

How to reference a script that's on just one object in the same scene via code?

If you have just one object in this scene that has a TileMovement component, you can find that unique instance like so: ...
user avatar
  • 114k
2 votes
Accepted

How to move an object when a specific event occurs?

I am not a fan of coroutines, because they make it hard to reason what actually goes on in the code. I prefer to use a pattern like this: ...
user avatar
  • 112k
1 vote

How to move an object when a specific event occurs?

Sounds like what you are looking for is Coroutines. Basically, you would write whatever code you want to execute in a function that returns IEnumerator, like this: <...
user avatar
0 votes
Accepted

Transforming world to canvas goes wrong when camera is moving

I realised an old test blanked out the Z in the worldCorners to just 0 and was never reverted for future refactors - I later fixed my bug but this remaining meant the screen position step was never ...
user avatar
0 votes
Accepted

Player is able to interact with objects but enemy isn't able to

Turns out I made a beginner mistake. I mistakenly used OnCollisionEnter2D instead of OnTriggerEnter2D.
user avatar
0 votes

How should I implement the idea behind this abstract class setup in a Unity-Friendly/Inspector-Usable way?

One simple way to get this behaviour out of the box is to make your FieldGeneric3D inherit from ScriptableObject: ...
user avatar
  • 114k
1 vote
Accepted

Rotating dials with increments, one works and the others doesn't. Why?

The problem is that each Rotate2d has its own independent life cycle, which means that Update will be executed 4 times per frame....
user avatar
  • 1,091
1 vote

How should I implement the idea behind this abstract class setup in a Unity-Friendly/Inspector-Usable way?

One possible solution is that instead of having it as a field in this class, have it as a separate monobehaviour component that inherits from an abstract class or interface. You can then get a ...
user avatar
  • 677
0 votes

Apply an offset to a camera rotating around an object

It was indeed coming from the offsetToCenterPosition calculation. Thank you DMGregory. Here is the full answer: ...
user avatar
  • 1
1 vote
Accepted

How to implement a dial control that snaps to increments?

First, you can simplify your angle method: ...
user avatar
  • 114k
0 votes

3D CharacterController ride moving platforms in Unity?

I am not very familiar with the built-in CharacterController, but I can tell you how I have implemented this for my own character controller (although, I should warn you it is almost definitely more ...
user avatar
0 votes

Collision callbacks not working

I fixed the issue. I needed to give my player object a new tag called "Player"
user avatar
1 vote

Collision callbacks not working

Your scripts are okay, your issue is due to scene setup rather than coding. In order to detect a trigger collision, OnTriggerEnter2D() has some requirements: Both objects must have one ...
user avatar
  • 4,037
1 vote
Accepted

Unity reference randomly becomes null

I've found the issue! Basically I was making use of Unity's 'Enter play mode settings' and disabling the reload domain. Since I was using static events this causes the reference to remain in-between ...
user avatar
  • 63
3 votes

When should I store a reference to a component instead of searching for it?

If you can have a reference, use the reference. Never* search. If you cannot maintain a reference, something usually smells. *There are situations where searching is the best path forward. An ...
user avatar
  • 5,221
10 votes
Accepted

When should I store a reference to a component instead of searching for it?

Don't search for something every frame if you can store it. ...
user avatar
  • 114k
2 votes
Accepted

Trouble adding to a dictionary using a for loop

You forgot to assign a value to your dictionary. Dictionary<GameObject, KeyCode> SlotKey = new Dictionary<GameObject, KeyCode>(); Until you assign a ...
user avatar
  • 114k
1 vote

Preventing other animations from resetting when a new animation starts playing

This is what animation layers in the animator controller are for. Each animation layer is a separate state-machine. So it is possible for each layer to play a different animation at the same time. The ...
user avatar
  • 112k
1 vote

How to detect objects within a radius without using a trigger collider?

The classes Physics and Physics2D contain several useful static methods to detect Colliders ...
user avatar
  • 112k
0 votes

Why is Vector3.ProjectOnPlane() "drifting" further from desired rotation on steep slopes

I think you want something more like this: ...
user avatar
  • 114k
2 votes
Accepted

How to design an upgradeable skill system?

Using scriptable objects to store game state is usually a bad idea. They are meant to represent game-specific static assets. "Upgrading a skill" by upgrading the data of the skill is an even ...
user avatar
  • 112k
0 votes
Accepted

Why is Vector3.ProjectOnPlane() "drifting" further from desired rotation on steep slopes

If you wanted to achieve a similar result without using projections, you might want to try just rotating the vector. This should be relatively easy to do knowing the normal of the plane. I would take ...
user avatar
1 vote

Unity coroutine not yielding sometimes

For making the coroutine wait for a few seconds before executing isCoyote = false, you will need to use new WaitForSeconds(). ...
user avatar
1 vote

Why does the player rotate nonstop?

My usual solution to this problem is: Introduce a new variable Quaternion desiredRotation which represents the direction where the object should look but might or ...
user avatar
  • 112k
1 vote
Accepted

Why rotating backwards is too fast, even if I set the time to 3 seconds?

You forgot to cache your starting rotation, and rotate each frame relative to that starting point, rather than where you already advanced to: ...
user avatar
  • 114k
0 votes

How to reference an object that was not destroyed from a previous scene in the inspector

Use a relay: ...
user avatar
  • 114k
1 vote

Unity, hold down right mouse button to turn the camera

An even better solution IMO would be to use transform.Rotate: tf.Rotate(0f, Input.GetAxis("Mouse X") * speed, 0f);
user avatar
  • 112k
0 votes

How to make the transform rotate backwards but in FixedUpdate instead in coroutine?

How about using WaitForFixedUpdate instead of WaitForEndOfFrame? You should put the yield before your code to ensure your code is called in FixedUpdate. Also, I find tracking the elapsed time easier ...
user avatar
  • 417
0 votes
Accepted

Why is my character controller deacceleration not working?

Here is the solution: ...
user avatar
  • 127
0 votes

Raycast collision not working

I would use tags instead of layers. If you are looking to detect enemy hit collision with raycast, layers are generally not the best method. Call FireWeapon() ...
user avatar
1 vote
Accepted

How to detect if hit by raycast from object with name/tag

You seem to fundamentally misunderstand how raycasts work. It's not a signal that pokes the object that it hits into action. It's a sensor that reports back to the one firing it what it hit. It is the ...
user avatar
  • 114k
0 votes

Finding GameObject with Tag, excluding this object and its children

You can write a simple function that checks whether an object is this object itself or one of its children/grandchildren/etc: ...
user avatar
  • 114k
2 votes

Passing a method as a parameter of another method

DMGregory's answer worked! Here's the new code: ...
user avatar
2 votes

Refresh resource folder during runtime

When you import a 3D model into your project in the Unity Editor, Unity converts the 3D model into an internal mesh format that's optimized specifically for the Unity engine (and stores this somewhere ...
user avatar
  • 3,505
2 votes
Accepted

Passing a method as a parameter of another method

When you write a method with parentheses after it, like: SomeFunctionWithNoParams() // or SomeFunctionWithOneParam(someParameter) ...then you are calling that ...
user avatar
  • 114k
0 votes

DontDestroyOnLoad only works for root GameObjects

set this line code before DontDestroyOnLoad you will avoid the error: yourObj.parent = null;
user avatar
1 vote

Save Resource Usage when Player is inactive

This is an unrealistic problem. The player is very unlikely to leave the game running unpaused and walk away from the computer for an extended period while the computer is running on battery. Gamers ...
user avatar
  • 3,505
0 votes

Vibration duration for mobile devices in Unity

You can use this script, which calls the native Android API: https://gist.github.com/ruzrobert/d98220a3b7f71ccc90403e041967c46b The author allows commercial use and even adopting the script The script ...
user avatar
  • 203

Top 50 recent answers are included