Search Unity

Weird interaction between addForce and Light

Discussion in 'Global Illumination' started by Garien, Feb 6, 2019.

  1. Garien

    Garien

    Joined:
    Oct 11, 2018
    Posts:
    36
    Hi.
    I recently started to use light materials and here's the issue I run into during tests:


    In this scene, there's a trigger within the cave with a on trigger stay function that sets every object default material to a material with Sprites/Diffuse shader setting, that I called ''Luz2D''.

    As you can see, it's working fine with every sprite. Even the boar... but when he starts to run, he turns pitch black and I have no clue why.

    I checked every line of the boar's script to find what was causing the error, and turns out that when I remove this piece of code, the error no longer happens:
    Code (CSharp):
    1.  if (!movingLeft)
    2.                 {
    3.                     //rb2d.AddForce(transform.right * -moveSpeed);
    4.  
    5.                     if (rb2d.velocity.x < maxSpeed) // && maxSpeedRestriction) //Enquanto sua velocidade for menor que a max speed, add force.
    6.                     {
    7.                         rb2d.AddForce(transform.right * moveSpeed);
    8.                     }
    9.  
    10.                     rb2d.transform.localScale = new Vector2(1, rb2d.transform.localScale.y);
    11.                 }
    12.                 if (movingLeft)
    13.                 {
    14.                     if (rb2d.velocity.x > -maxSpeed) // && maxSpeedRestriction)
    15.  
    16.                     {
    17.                         rb2d.AddForce(transform.right * -moveSpeed);
    18.                     }
    19.  
    20.                     rb2d.transform.localScale = new Vector2(-1, rb2d.transform.localScale.y);
    21.  
    22.                 }


    I'm absolutely lost. Is there any way to fix this, while keeping the boar's script intact? Why did this happen?
    I appreciate your help. Thanks!

    EDIT: Digging in deeper, I found out these lines of codes are the problem:
    rb2d.transform.localScale = new Vector2(1, rb2d.transform.localScale.y);
    rb2d.transform.localScale = new Vector2(-1, rb2d.transform.localScale.y);
     
    Last edited: Feb 6, 2019
  2. uy3d

    uy3d

    Unity Technologies

    Joined:
    Aug 16, 2016
    Posts:
    187
    localScale takes a Vector3. Can you try using this instead:
    new Vector3(1, rb2d.transform.localScale.y, rb2d.transform.localScale.z);
     
  3. Garien

    Garien

    Joined:
    Oct 11, 2018
    Posts:
    36
    that worked, thanks!