unity – Transparent objects in the scene only get lit half way by point light

I have encountered a weird graphics problem, where all of our transparent objects in the scene only get lit half way (only from the y position downward) by a point light. This happens with all of our grass and tree shaders.

Also, when looking through translucent objects, they become really desaturated and gray-is which is an undesired effect.

Does anyone know how to deal with this problem? Please refer to the screenshots below.

I’m are using Unity 2020.1.14f, built-in render pipeline with a forward renderer.

For grass and trees we are using the Fantasy Adventure Environment, but all other grass shaders from other packages have the same problem.

enter image description here
enter image description here

unity – Is WaitForSecondsRealtime() dependent on machine lag?

I was reading the documentation and I’m trying to get some timing down for a few animations.

My question is, is the WaitForSecondsRealtime dependent on machine lag or anything? A friend of mine said that WaitForSecondsRealtime was invoked at the kernel level and thus it would be as accurate as possible.

Like I said I’ve read the documentation, but they don’t really go into the specifics about how that works. Anyone have any insight?

unity – Road Intersection mesh generation

For the last month I was learning about meshes and how procedurally generate them. For what I need it looks very straightforward.
For my little app I planning to have a road tool (very simple), user draws lines and curves and road is generated with specific width (flat).
So I can now create a drawing tool and generate simple mesh along the curve or line.
Next step is to be able to construct intersections…

I looked at lots of example but couldn’t find any descriptions how it is done.

  1. Could you please advice what approach to take, how to identify intersection of two or multiple curves and generate mesh junction?

  2. Also could you please advice on bigger picture, should I have sort of underline system for city building app, like graph, to know where to place road intersections, buildings and so on?

Thank you

This is a quick prototype I did (not in code)
https://youtu.be/1TxxB-ofLAo

unity – How can I use the flags logic to turn on/off the lights and rotations?

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

public class ChangeColors : MonoBehaviour
{
    public List<Transform> objectsToRotate = new List<Transform>();
    public bool turnOnRotationLights = false;
    public float rotationSpeed;
    public Light() lights;
    public Material material;
    public Vector3 targetAngle = new Vector3(-90f, 0f, 0f);

    private bool rotationLightsOn = true;

    // 0,31,191
    // 255,0,0

    // Start is called before the first frame update
    void Start()
    {
        if (turnOnRotationLights)
            StartCoroutine(SetTrue());
    }

    // Update is called once per frame
    void Update()
    {
        if(turnOnRotationLights == true && rotationLightsOn == true)
        {
            StartCoroutine(SetTrue());

            rotationLightsOn = false;
        }
        else
        {
            rotationLightsOn = true;
        }

        if (rotationSpeed == 0)
        {
            for (int i = 0; i < objectsToRotate.Count; i++)
            {
                Transform t = objectsToRotate(i).transform;
                float newAngle = Mathf.LerpAngle(t.localEulerAngles.x, targetAngle.x, Time.deltaTime);
                t.localEulerAngles = new Vector3(newAngle, 0, 0);
            }
        }

        if (turnOnRotationLights)
        {
            for (int i = 0; i < objectsToRotate.Count; i++)
            {
                objectsToRotate(i).Rotate(new Vector3(Time.deltaTime * rotationSpeed, 0, 0));
            }
        }
    }

    public void setHighlight(bool highlight)
    {
        if (highlight)
        {
            material.EnableKeyword("_EMISSION");
            material.globalIlluminationFlags = MaterialGlobalIlluminationFlags.RealtimeEmissive;
            material.SetColor("_EmissionColor", new Color(0, 31, 191));
        }
        else
        {
            material.EnableKeyword("_EMISSION");
            material.globalIlluminationFlags = MaterialGlobalIlluminationFlags.RealtimeEmissive;
            material.SetColor("_EmissionColor", new Color(255,0,0));
        }
    }

    IEnumerator SetTrue()
    {
        setHighlight(true);

        yield return new WaitForSeconds(0.5f);

        StartCoroutine(SetFalse());
    }

    IEnumerator SetFalse()
    {
        setHighlight(false);

        yield return new WaitForSeconds(0.5f);

        StartCoroutine(SetTrue());
    }
}

It was working fine without the two flags turnOnRotationLights and rotationLightsOn this is how the code looks like before adding and using this two flags :

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

public class ChangeColors : MonoBehaviour
{
    public List<Transform> objectsToRotate = new List<Transform>();
    public float rotationSpeed;
    public Light() lights;
    public Material material;
    public Vector3 targetAngle = new Vector3(-90f, 0f, 0f);

    // 0,31,191
    // 255,0,0

    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(SetTrue());
    }

    // Update is called once per frame
    void Update()
    {
        if (rotationSpeed == 0)
        {
            for (int i = 0; i < objectsToRotate.Count; i++)
            {
                Transform t = objectsToRotate(i).transform;
                float newAngle = Mathf.LerpAngle(t.localEulerAngles.x, targetAngle.x, Time.deltaTime);
                t.localEulerAngles = new Vector3(newAngle, 0, 0);
            }
        }

        for(int i = 0; i < objectsToRotate.Count; i++)
        {
            objectsToRotate(i).Rotate(new Vector3(Time.deltaTime * rotationSpeed, 0, 0));
        }
    }

    public void setHighlight(bool highlight)
    {
        if (highlight)
        {
            material.EnableKeyword("_EMISSION");
            material.globalIlluminationFlags = MaterialGlobalIlluminationFlags.RealtimeEmissive;
            material.SetColor("_EmissionColor", new Color(0, 31, 191));
        }
        else
        {
            material.EnableKeyword("_EMISSION");
            material.globalIlluminationFlags = MaterialGlobalIlluminationFlags.RealtimeEmissive;
            material.SetColor("_EmissionColor", new Color(255,0,0));
        }
    }

    IEnumerator SetTrue()
    {
        setHighlight(true);

        yield return new WaitForSeconds(0.5f);

        StartCoroutine(SetFalse());
    }

    IEnumerator SetFalse()
    {
        setHighlight(false);

        yield return new WaitForSeconds(0.5f);

        StartCoroutine(SetTrue());
    }
}

but there are two problems :

  • When running the game the lights and rotation start at the beginning.
  • I can’t control the lights and rotation on/of while the game is running in runtime.

I want :

  • When the game start also start the lights and rotation if the public flag is true.
  • While the game is running in runtime to be able to turn off/on both lights and rotation.

The way I used it with the flags it’s not working good. When I turn it off at runtime only some orations stop and the lights keep on and it’s all messed.

Bullets Stop when Shooting Unity 2D

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

public class Makarov : MonoBehaviour
{
    public GameObject Bullet;

    public float BulletSpeed;

    public Transform ShootPoint;

    // Update is called once per frame
    void Update()
    {
        if(Input.GetMouseButtonDown(0))
        {
            shoot();
        }
    }

    void shoot()
    {
        GameObject BulletIns = Instantiate(Bullet, ShootPoint.position, ShootPoint.rotation);
        BulletIns.GetComponent<Rigidbody2D>().AddForce(BulletIns.transform.forward * BulletSpeed);
    }
}

When I shoot the bullets just stop right in front if the gun.
This is for a top down shooting.

unity – How to assign an index to different node clusters? (for pathfinding)

Hellow~

So for a bit of context: i’m currently working on a RTS (way above may league) which already has a level editor and a node grid which maps the terrain, i previously make a navigation system with A* as explainned by Sebastian Lague in his tutorial series about the topic, but then i run into a problem when running with multiple units (not the point here: but never manage to find each unit cluster within the selection), so i change it into a navmesh mapping which if made dynamic by assigning navmesh obstacles to the unwalkable nodes (the frame after the creation of the node grid)

Now i want to use the node grid basically to check if the target node and the start node share the same node cluster. But here is the thing: i can’t assign the needed index to the nodes as i expected.

I’ve been trying adapting code from this post, but didn’t quite make it.
Now i have this one, which i think could achieve something but still gets me a invalid operation exception:

public const int UNWALKABLE_ISLAND = 999;

public static void SetIslands(ref WorldGrid _worldGrid) {
    int width = _worldGrid.nodeGrid.GetLength(0);
    int height = _worldGrid.nodeGrid.GetLength(1);

    bool(,) visited = new bool(width, height);

    int clusterCount = 0;

    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++)
        {
            if (visited(x, y)) { continue; } else {
                List<Node> cluster = new List<Node>() {
                    _worldGrid.nodeGrid(x, y)
                };

                foreach (var node in cluster) {
                    if (node.isWalkable)
                    {
                        visited(node.X, node.Y) = true;
                        node.SetIslandIndex(clusterCount);
                        cluster.AddRange(_worldGrid.GetNeighbours8D(node));
                    }
                    else
                    {
                        cluster.Remove(node);
                    }
                }

                clusterCount++;
            }
        }
    }
}

The error:

InvalidOperationException: Collection was modified; enumeration operation may not execute.
System.ThrowHelper.ThrowInvalidOperationException (System.ExceptionResource resource) (at<437ba245d8404787b9fbab9b439ac908>:0)
System.Collections.Generic.List1+Enumerator(T).MoveNextRare() (at<437ba245d8404787b9fbab9b439ac908>:0) System.Collections.Generic.List1+Enumerator(T).MoveNext() (at<437ba245d8404787b9fbab9b439ac908>:0)
Pathfinding.SetIslands (WorldGrid& _worldGrid) (at Assets/Pathfinding.cs: 25)
WorldGrid.CreateNodeGrid() (at Assets/WorldGrid:80)
WorldGrid+d_12.MoveNext() (at Assets/WorldGrid.cs:42)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, Sustem.IntPtr returnValueAddress) (at<437ba245d8404787b9fbab9b439ac908>:0)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)

My node class contains an int that should hold the cluster index, which will be using for the request check. (the code its based from this youtube video)

Right now i’m trying to understand the spectral clustering stuff, i’ve been told it could help.

I hope you can help an animator in need with some pointers.

Cheers :3

*Oh!, I also tried reading the nav agent path end coordinate for comparison, but it ended up becoming a huge mess for me.

unity – Problem using a CharacterController with a moving platform

My game has a wierd problem that I can reduce to the following minimal working example.

  1. There is a box object with a component with the following method:

     void Update() {
         transform.position = Vector3.MoveTowards(transform.position, endPoint.position, speed*Time.deltaTime);
     }
    

It is just a platform moving towards a given point.

  1. There is a capsule object which represents the player. It has a character controller. In some part of the game, it is a “child” of the box object. Without further additions, it just moves along with the box, which is the expected behavior for child objects. However, when I add to the capsule another component with the following method:

     void Update() {
         GetComponent<CharacterController>().Move(new Vector3(0, 0, 0));
     }
    

the capsule stops moving with the box (this Update method is a reduced form of my actual method, which also included keyboard input handling).

When I change the Update method of the box to FixedUpdate, again the capsule moves with the box as intended.

Why does this happen?

unity – How to create terrain made of small tiles?

Whether performance is an issue depends on the size and quantity of the tiles. If the environment is 10×10 tiles, there shouldn’t be any performance issues. If the environment is 1000 x 1000 tiles, spawning 1 million tiles at once is probably going to kill performance.

If the map is very large (in terms of number of tiles) but only a small portion of it is visible at a time (e.g. due to a top-down camera angle), you can use a pooling system. E.g. say the map is 1000 x 1000 tiles but only 10 x 10 tiles are visible at once. You could spawn a 12 x 12 block of tiles, which gives you 1 row of padding on each side. When the camera moves towards one edge of the screen, you move the tiles from the opposite edge of the screen over to create the new row of tiles that’s coming into view.

Here’s a diagram with a 2×2 viewable area and 1 row of padding around each edge.

enter image description here

unity – Why the random positions are not random?

It’s like it’s Instantiate once random positions and then using this positions each time and not Instantiating new random positions.

That’s exactly what your code tells it to do. randomPositions is a member variable, so any values you add to it are going to stay there unless you overwrite or remove them. You first do this from Start():

for (int i = 0; i < numberOfLines; i++) {
    randomPositions.Add(new Vector3(UnityEngine.Random.Range(5, 50), UnityEngine.Random.Range(5, 50), UnityEngine.Random.Range(5, 50)));
}

You’ve already filled the list with some values. These values are never cleared or overwritten in your code.

Later you do this:

for (int i = 0; i < numberOfLines; i++) {
    randomPositions.Add(new Vector3(UnityEngine.Random.Range(5, 50), UnityEngine.Random.Range(5, 50), UnityEngine.Random.Range(5, 50)));

You’re calling Add() here, so it’s adding new random values to the end of the list. Your list of positions just keeps getting longer and longer, but the elements in each index never change.

The list is pointless and is just wasting memory. Write your code like this:

if (randomLinesPositions) {
    var position = new Vector3(UnityEngine.Random.Range(5, 50), UnityEngine.Random.Range(5, 50), UnityEngine.Random.Range(5, 50));
    instance = Instantiate(
        linesPointsPrefab,
        position,
        Quaternion.identity
    );
}

unity – CharacterController – should I work with FixedUpdate or with Update?

I know that updates related to the physics engine should be made in FixedUpdate, but I am not sure about updates related to CharacterController: should they be done in Update or FixedUpdate? I tried both options and putting them in FixedUpdate appears smoother, but this may be due to other factors, so I would be happy to know what the recommended practice is.