Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Editor Bug : Collisions cause my mesh to go wild ! (Resolved)

Discussion in 'Editor & General Support' started by Willy-The-Kid, May 11, 2015.

  1. Willy-The-Kid

    Willy-The-Kid

    Joined:
    May 11, 2015
    Posts:
    32
    Hey guys.

    I have this weird (but funny) issue :

    I don't use anything special on my objects, rigidobject and box collider, nothing special.

    Any idea on how to resolve this ?
    Thanks
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,998
    Hi,
    How does the code for collision looks like? (Seems like its scaling the object when it collides..)
     
  3. Willy-The-Kid

    Willy-The-Kid

    Joined:
    May 11, 2015
    Posts:
    32
    Hello mgear.

    I have absolutly no code handling a collision, I just create a cube and a sphere in the editor, added a riggidbody and boom, this is it ...

    Any clue?
     
  4. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,986
    There has to be some script attached to to handle the movement. What are you using for that?
     
  5. Willy-The-Kid

    Willy-The-Kid

    Joined:
    May 11, 2015
    Posts:
    32
    Hello,

    Yes of corse I do use a script to move the character. It's my own script, this is a sample of some point of interest :


    Code that detect user input :

    Code (CSharp):
    1. void Update ()
    2.     {
    3.         if (Input.GetKeyDown(KeyCode.UpArrow))
    4.         {
    5.             playerMoveUp     = 1f;
    6.             playerMoveDown     = 0f;
    7.         }
    8.         if (Input.GetKeyDown(KeyCode.DownArrow))
    9.         {
    10.             playerMoveUp     = 0f;
    11.             playerMoveDown     = 1f;
    12.         }
    13.         if (Input.GetKeyDown(KeyCode.LeftArrow))
    14.         {
    15.             playerMoveLeft    = 1f;
    16.             playerMoveRight    = 0f;
    17.         }
    18.         if (Input.GetKeyDown(KeyCode.RightArrow))
    19.         {
    20.             playerMoveLeft    = 0f;
    21.             playerMoveRight    = 1f;
    22.         }
    23.  
    24.  
    25.  
    26.  
    27.         if (Input.GetKeyUp(KeyCode.UpArrow))
    28.         {
    29.             playerMoveUp     = 0f;
    30.         }
    31.         if (Input.GetKeyUp(KeyCode.DownArrow))
    32.         {
    33.             playerMoveDown     = 0f;
    34.         }
    35.         if (Input.GetKeyUp(KeyCode.LeftArrow))
    36.         {
    37.             playerMoveLeft    = 0f;
    38.         }
    39.         if (Input.GetKeyUp(KeyCode.RightArrow))
    40.         {
    41.             playerMoveRight    = 0f;
    42.         }
    43.  
    44.         if(playerMoveUp != 0f || playerMoveDown != 0f || playerMoveLeft != 0f || playerMoveRight != 0f)
    45.         {
    46.             currentLevel.MovePlayer(playerMoveUp, playerMoveDown, playerMoveLeft, playerMoveRight);
    47.         }
    48.     }
    49.  



    Code that calculate the move :

    Code (CSharp):
    1. public void MovePlayer (float up, float down, float left, float right)
    2.     {
    3.         currentPlayer.Move((right - left) * maxPlayerSpeedX, (up - down) * maxPlayerSpeedZ);
    4.     }

    Code that move the character :

    Code (CSharp):
    1. public void Move (float xSpeed, float zSpeed)
    2.     {
    3.         float x     = this.transform.position.x;
    4.         float y     = this.transform.position.y;
    5.         float z     = this.transform.position.z;
    6.    
    7.         this.transform.LookAt(new Vector3 (x + xSpeed, y, z + zSpeed));
    8.         this.transform.position = new Vector3 (x + xSpeed, y, z + zSpeed);
    9.     }


    That's all. As you see there is nothing handling any kind of scale, I do nothing in my code when a collision happens.


    This is a screenshot of character and ennemy component in unity :
    Character object.png Ennemy object.png


    Thanks for any help.
     
  6. Willy-The-Kid

    Willy-The-Kid

    Joined:
    May 11, 2015
    Posts:
    32
    Any idea guys ? I'm really stuck with this ...
     
  7. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,986
    With out seeing the whole script, (and probably the script attached to object it's colliding with) really can't provide any insight. Collision/physics alone won't scale an object, some chunk of code somewhere is causing it.

    Although, it might also be a visual anomaly created by the fact you are changing the position on a non kinematic rigidbody. Since you are using physics that way, you shouldn't be setting the position. That will cause all kinds of problems. You want to use addforce() or make it kinematic. Either use physics or control position but not both.
     
  8. Willy-The-Kid

    Willy-The-Kid

    Joined:
    May 11, 2015
    Posts:
    32
    Hello,

    I tried to turn all rigidbody to kinematic, I still have the same issue.
    This is my full codes, as you requested.

    Context class

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class Context : MonoBehaviour
    6. {
    7.  
    8.     public List<Level>                    levelList;
    9.     public List<Character_Player>        playerList;
    10.    
    11.    
    12.     private int                         currentLevelNum;
    13.     private Level                         currentLevel;
    14.  
    15.  
    16.     private float                         playerMoveUp     =  0f;
    17.     private float                         playerMoveDown     =  0f;
    18.     private float                         playerMoveLeft     =  0f;
    19.     private float                         playerMoveRight     =  0f;
    20.  
    21.  
    22.  
    23.     void Start ()
    24.     {
    25.         Debug.Log ("Context started");
    26.         StartGame ();
    27.     }
    28.  
    29.  
    30.  
    31.  
    32.     void Update ()
    33.     {
    34.         if (Input.GetKeyDown(KeyCode.Q))
    35.         {
    36.             currentLevel.Attack(Character_Player.ATTACK_MELEE);
    37.         }
    38.         else if (Input.GetKeyDown(KeyCode.W))
    39.         {
    40.             currentLevel.Attack(Character_Player.ATTACK_SPECIAL_A);
    41.         }
    42.         else if (Input.GetKeyDown(KeyCode.A))
    43.         {
    44.             currentLevel.Attack(Character_Player.ATTACK_SPECIAL_B);
    45.         }
    46.         else if (Input.GetKeyDown(KeyCode.S))
    47.         {
    48.             currentLevel.Attack(Character_Player.ATTACK_ULTIMATE);
    49.         }
    50.  
    51.  
    52.  
    53.  
    54.         if (Input.GetKeyDown(KeyCode.UpArrow))
    55.         {
    56.             playerMoveUp     = 1f;
    57.             playerMoveDown     = 0f;
    58.         }
    59.         if (Input.GetKeyDown(KeyCode.DownArrow))
    60.         {
    61.             playerMoveUp     = 0f;
    62.             playerMoveDown     = 1f;
    63.         }
    64.         if (Input.GetKeyDown(KeyCode.LeftArrow))
    65.         {
    66.             playerMoveLeft    = 1f;
    67.             playerMoveRight    = 0f;
    68.         }
    69.         if (Input.GetKeyDown(KeyCode.RightArrow))
    70.         {
    71.             playerMoveLeft    = 0f;
    72.             playerMoveRight    = 1f;
    73.         }
    74.  
    75.  
    76.  
    77.  
    78.         if (Input.GetKeyUp(KeyCode.UpArrow))
    79.         {
    80.             playerMoveUp     = 0f;
    81.         }
    82.         if (Input.GetKeyUp(KeyCode.DownArrow))
    83.         {
    84.             playerMoveDown     = 0f;
    85.         }
    86.         if (Input.GetKeyUp(KeyCode.LeftArrow))
    87.         {
    88.             playerMoveLeft    = 0f;
    89.         }
    90.         if (Input.GetKeyUp(KeyCode.RightArrow))
    91.         {
    92.             playerMoveRight    = 0f;
    93.         }
    94.  
    95.         if(playerMoveUp != 0f || playerMoveDown != 0f || playerMoveLeft != 0f || playerMoveRight != 0f)
    96.         {
    97.             currentLevel.MovePlayer(playerMoveUp, playerMoveDown, playerMoveLeft, playerMoveRight);
    98.         }
    99.     }
    100.  
    101.  
    102.  
    103.  
    104.     void StartGame ()
    105.     {
    106.         currentLevelNum = 0;
    107.  
    108.         if (playerList.Count > 0)
    109.         {
    110.             if (levelList.Count > 0)
    111.             {
    112.                 currentLevel = Instantiate(levelList[currentLevelNum]);
    113.                 currentLevel.transform.SetParent(this.transform);
    114.  
    115.                 currentLevel.StartGame(playerList[0]);
    116.             }
    117.             else
    118.             {
    119.                 Debug.Log("Cannot start game : no levels are defined in Context");
    120.             }
    121.         }
    122.         else
    123.         {
    124.             Debug.Log("Cannot start game : no players defined");
    125.         }
    126.     }
    127.  
    128. }
    129.  



    Level Class

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5.  
    6. public class Level : MonoBehaviour
    7. {
    8.     public float                     maxPlayerSpeedX            =    0.5f;
    9.     public float                     maxPlayerSpeedZ            =    0.5f;
    10.  
    11.     public string                    levelName                =    "New level";
    12.     public int                         levelNumber             =    0;
    13.     public int                         wavesNumber             =    10;
    14.     public int                         maxEnnemiesPerWave        =    20;
    15.     public int                         minEnnemiesPerWave        =    20;
    16.  
    17.     public Spawner                    playerSpawner;
    18.     public List<Spawner>            ennemySpawnersList;
    19.     public List<Character_Ennemy>    ennemyList;
    20.  
    21.    
    22.     private int                     currentWave             =   0;
    23.     private int                     currentEnnemyNum         =   0;
    24.     private    Character_Player        currentPlayer;
    25.     private List<Character_Ennemy>    currentEnnemyList        =     new List<Character_Ennemy>();
    26.  
    27.  
    28.     void Start ()
    29.     {
    30.  
    31.     }
    32.  
    33.     void Update ()
    34.     {
    35.        
    36.     }
    37.  
    38.  
    39. //__START GAME_____________________________________________________
    40.  
    41.     public void StartGame (Character_Player player)
    42.     {
    43.         if (player != null)
    44.         {
    45.             SpawnPlayer (player);
    46.         }
    47.         else
    48.         {
    49.             Debug.Log("Level cannot start : player is missing");
    50.         }
    51.     }
    52.    
    53.     void LaunchWave ()
    54.     {
    55.         currentWave ++;
    56.  
    57.         if (currentWave <= wavesNumber)
    58.         {
    59.             // Start wave
    60.             AskSpawnEnnemy ();
    61.         }
    62.     }
    63.  
    64.  
    65. //__PLAYER_____________________________________________________
    66.    
    67.     void SpawnPlayer (Character_Player player)
    68.     {
    69.         if(playerSpawner != null)
    70.         {
    71.             if (player != null)
    72.             {
    73.                 if (currentPlayer == null)
    74.                 {
    75.                     currentPlayer = Instantiate (player);
    76.                     currentPlayer.transform.SetParent (this.transform);
    77.                    
    78.                     LaunchWave ();
    79.                 }
    80.                 else
    81.                 {
    82.                     Debug.Log ("Level error : Player already exist");
    83.                 }
    84.             }
    85.             else
    86.             {
    87.                 Debug.Log ("Level error : Player is not defined");
    88.             }
    89.         }
    90.         else
    91.         {
    92.             Debug.Log ("Level error : Player spawner is not defined");
    93.         }
    94.     }
    95.  
    96.  
    97.     public void MovePlayer (float up, float down, float left, float right)
    98.     {
    99.         currentPlayer.Move((right - left) * maxPlayerSpeedX, (up - down) * maxPlayerSpeedZ);
    100.     }
    101.  
    102.     public void Attack (string attackType)
    103.     {
    104.         currentPlayer.LaunchAttack (attackType);
    105.     }
    106.  
    107. //__ENNEMIES_____________________________________________________
    108.  
    109.     void AskSpawnEnnemy ()
    110.     {
    111.         if (ennemyList.Count > 0)
    112.         {
    113.             if (currentEnnemyNum < minEnnemiesPerWave)
    114.             {
    115.                 SpawnEnnemy ();
    116.             }
    117.         }
    118.         else
    119.         {
    120.             Debug.Log("Level cannot start : no ennemy set");
    121.         }
    122.     }
    123.  
    124.     void SpawnEnnemy ()
    125.     {
    126.         currentEnnemyNum ++;
    127.  
    128.         Character_Ennemy newEnnemy     = Instantiate (ennemyList [Random.Range (0, ennemyList.Count)]);
    129.         currentEnnemyList.Add (newEnnemy);
    130.  
    131.         Spawner      randomSpawner = ennemySpawnersList[Random.Range (0, ennemySpawnersList.Count)];
    132.  
    133.         newEnnemy.transform.SetParent (this.transform);
    134.         newEnnemy.transform.position = randomSpawner.transform.position;
    135.     }
    136.  
    137. }
    138.  



    Character_Player class
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class Character_Player : MonoBehaviour
    6. {
    7.     public const string         ATTACK_MELEE        =    "ATTACK_MELEE";
    8.     public const string         ATTACK_SPECIAL_A    =    "ATTACK_SPECIAL_A";
    9.     public const string         ATTACK_SPECIAL_B    =    "ATTACK_SPECIAL_B";
    10.     public const string         ATTACK_ULTIMATE        =    "ATTACK_ULTIMATE";
    11.    
    12.     public     string                 surname             =    "Richard";
    13.     public     int                 visionRange            =    100;
    14.  
    15.     public  GameObject          castingDummy;
    16.  
    17.     public  Attack                meleeAttack;
    18.     public  Attack                specialAttackA;
    19.     public  Attack                specialAttackB;
    20.     public  Attack                ultimateAttack;
    21.    
    22.     private List<Attack>        activeAttackList;
    23.  
    24.  
    25.  
    26.     void Start ()
    27.     {
    28.    
    29.     }
    30.  
    31.     void Update ()
    32.     {
    33.  
    34.     }
    35.  
    36.     public void Move (float xSpeed, float zSpeed)
    37.     {
    38.         float x     = this.transform.position.x;
    39.         float y     = this.transform.position.y;
    40.         float z     = this.transform.position.z;
    41.    
    42.         this.transform.LookAt(new Vector3 (x + xSpeed, y, z + zSpeed));
    43.         this.transform.position = new Vector3 (x + xSpeed, y, z + zSpeed);
    44.     }
    45.    
    46.     public void LaunchAttack (string attackType)
    47.     {
    48.         Attack usedAttack;
    49.  
    50.         switch (attackType)
    51.         {
    52.             case ATTACK_MELEE:
    53.                 usedAttack = meleeAttack;
    54.                 break;
    55.             case ATTACK_SPECIAL_A:
    56.                 usedAttack = specialAttackA;
    57.                 break;
    58.             case ATTACK_SPECIAL_B:
    59.                 usedAttack = specialAttackB;
    60.                 break;
    61.             case ATTACK_ULTIMATE:
    62.                 usedAttack = ultimateAttack;
    63.                 break;
    64.             default:
    65.                 usedAttack = meleeAttack;
    66.                 break;
    67.         }
    68.  
    69.         if (usedAttack.isReady())
    70.         {
    71.             Attack newAttack = Instantiate(usedAttack);
    72.             newAttack.transform.SetParent(this.transform.parent);
    73.             newAttack.transform.position = new Vector3(castingDummy.transform.position.x, castingDummy.transform.position.y, castingDummy.transform.position.z);
    74.             newAttack.transform.rotation = this.transform.rotation;
    75.             newAttack.Fire(LayersTypes.Player);
    76.  
    77.             AddCastEffect(usedAttack.GetCastEffect());
    78.  
    79.             usedAttack.StartCooldown();
    80.         }
    81.     }
    82.  
    83.     public void AddCastEffect (AttackEffect effect)
    84.     {
    85.         if(effect != null)
    86.         {
    87.             AttackEffect newEffect         = Instantiate(effect);
    88.             newEffect.gameObject.transform.SetParent(this.gameObject.transform.parent);
    89.             newEffect.gameObject.transform.position = this.gameObject.transform.position;
    90.  
    91.         }
    92.     }
    93. }
    94.  


    Character_Ennemy class
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Character_Ennemy : MonoBehaviour
    5. {
    6.     void Start ()
    7.     {
    8.  
    9.     }
    10.  
    11.     void Update ()
    12.     {
    13.    
    14.     }
    15. }
    16.  

    Spawner Class (used as dummy)
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class Spawner : MonoBehaviour
    6. {
    7.     void Start ()
    8.     {
    9.    
    10.     }
    11.    
    12.     // Update is called once per frame
    13.     void Update ()
    14.     {
    15.    
    16.     }
    17. }
    18.  



    This is it. As far as i know, I do absolutly nothing on collisions.
     
  9. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,998
    Could also happen if your player parent is getting scaled somewhere,
    can you pause the game when its happening and take screenshot of the Hierarchy?
    (with player parent selected)
     
  10. Willy-The-Kid

    Willy-The-Kid

    Joined:
    May 11, 2015
    Posts:
    32
    What do you call a player parent ? The main parent of all my scene object ?
     
  11. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,998
    In the hierarcy is the player at the root level,
    or under some other objects? (as a child object)

    :::hierachy:::
    Player < Root level
    someobject / Player < Player parent is "someobject", if someobject is scaled, player gets scaled also
     
  12. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,986
    Try disabling the effect Instantiate, see if that changes things.
     
  13. Willy-The-Kid

    Willy-The-Kid

    Joined:
    May 11, 2015
    Posts:
    32
    @mgear
    I guess you mean player (as user), so hierarchy looks like this

    Context
    • Level
      • Spawner
      • Player
      • Ennemy

    Neither Context, Level nor Player scales are affected when that issue happens.
    See joined screenshots.

    Screen Shot 2015-05-13 at 9.33.51 AM.png Screen Shot 2015-05-13 at 9.34.22 AM.png



    @zombiegorilla
    All code joined I created myself, there is nothing happening on collision and "Effect" is anyway only instantiated when an attack button is pressed, which is not the case in this bug demo. Disabling "Effect" instantiation does not fix this issue. I think there is something deeper ...
     
    Last edited: May 13, 2015
  14. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,986
    That is truly bizarre.
    Could you share that scene? (Just a package of the contents would be fine.). Also what version of unity are you using?
     
  15. Willy-The-Kid

    Willy-The-Kid

    Joined:
    May 11, 2015
    Posts:
    32
    I'm using Unity Pro 5.0.1f1 + Iphone Pro (Subscrption license if that may change anything).

    It seems to have something to do with Riggibodies, if I remove this component, I don't have this bug anymore (but of corse I need this component ^^)

    I'll prepare a package for you and post it soon.
     
  16. Willy-The-Kid

    Willy-The-Kid

    Joined:
    May 11, 2015
    Posts:
    32
    This is a sample package. I removed any unnecessary code to make it easier to debug.
    Use arrow keys to move the sphere, enter in the box to get the show started ! :p

    PS : A scene with setup is included
     

    Attached Files:

  17. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,998
    atleast "Level" scale is not 1,1,1, so when player rotates it happens (as you collide and it starts to rotate)
     
    Willy-The-Kid likes this.
  18. Zerot

    Zerot

    Joined:
    Jul 13, 2011
    Posts:
    135
    The problem is not the collision, but the lookat. You do a lookat to a point that is determined by the speed. If the speed is 0, then it will try to look at its own position which would be basically all directions at the same time. This causes the resulting quaternion to not be a valid rotation, resulting in the stretching you see. You can see the same effect if you manually rotate the player using the inspector(just click drag on the x).

    Aside from that, you should always move rigidbodies using the rigidbody functions. In this case, MovePosition would be the correct one to use
     
  19. Willy-The-Kid

    Willy-The-Kid

    Joined:
    May 11, 2015
    Posts:
    32
    @Zerot,
    Following your idea, I removed LookAt from my code and changed
    Code (CSharp):
    1. this.transform.position = new Vector3 (x + xSpeed, y, z + zSpeed);
    to
    Code (CSharp):
    1. this.gameObject.GetComponent<Rigidbody>().MovePosition(new Vector3 (x + xSpeed, y, z + zSpeed));
    I still experience the same issue.

    @mgear
    Didn't got your point here. Note that level scale is the same as defined in my library object.
     
  20. Willy-The-Kid

    Willy-The-Kid

    Joined:
    May 11, 2015
    Posts:
    32
    Ok got it, thanks @mgear , you got the right clue.

    I used a box as level, and to get it on the right size, I scaled it to my needs. This caused this issue on child scales.
    To resolve this issue I had to change my hierarchy to get the level object being empty and having as child the grounds, instead of the level beeing the ground itself.

    Maybe this will be more clear :
    Old hierarchy
    Context
    Level (as ground)
    Characters
    New hierarchy
    Context
    Level
    Ground
    Characters

    Now all seems to works fine ! Thanks everyone for your precious help .
     
  21. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,986
    Hmm... the scene was empty, all that was included was scripts.
     
  22. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,986
    nevermind.

    Glad you got it working. ;)
     
    Willy-The-Kid likes this.