Search Unity

Camera scales objects attached to it weirdly

Discussion in 'Physics' started by XChillX, May 4, 2017.

  1. XChillX

    XChillX

    Joined:
    Jan 9, 2016
    Posts:
    8
    So i have a weapon that you can pick up in the game, but when it goes from being a game object in the game, to in the players "arms" (actually just attached to a secondary camera in the hierarchy) See pics below. This is REALLY bad, as it stretches the gun so much and its terrible, I have a script that makes it so when you pick up the gun the scale resets, but when you drop the gun, its still stretched. Please help, this really break the game.
     

    Attached Files:

  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
  3. XChillX

    XChillX

    Joined:
    Jan 9, 2016
    Posts:
    8
    I am new to unity, so no, i am not using that parameter, i am also new to c# but im trying to learn it, so i'll post what code have with setting the game objects scale. When the player presses "E" on a weapon it automatically becomes a child of the first person camera, but i also have a secondary camera which is a child of the first person camera, but the secondary camera renders above the first person camera (Secondary cam has depth 1) And it also only renders items set to the layer "Weapon"

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. namespace S3
    6. {
    7.     public class Item_SetScale : MonoBehaviour
    8.     {
    9.         private Item_Master itemMaster;
    10.         public Vector3 itemLocalScale;
    11.         public float xScale;
    12.         public float yScale;
    13.         public float zScale;
    14.  
    15.         void OnEnable()
    16.         {
    17.             SetInitialReferences();
    18.             SetScaleOnPlayer();
    19.             itemMaster.EventObjectPickUp += SetScaleOnPlayer;
    20.         }
    21.  
    22.         void OnDisable()
    23.         {
    24.             itemMaster.EventObjectPickUp -= SetScaleOnPlayer;
    25.         }
    26.  
    27.  
    28.         void SetInitialReferences()
    29.         {
    30.             itemMaster = GetComponent<Item_Master>();
    31.         }
    32.  
    33.         void SetScaleOnPlayer()
    34.         {
    35.             if (transform.root.CompareTag(GameManagerReferences._playerTag))
    36.             {
    37.                 transform.localScale = new Vector3(transform.localScale.y, zScale, transform.localScale.z);
    38.                 transform.localScale = new Vector3(transform.localScale.z, xScale, transform.localScale.x);
    39.                 transform.localScale = new Vector3(transform.localScale.x, yScale, transform.localScale.y);
    40.             }
    41.         }
    42.     }
    43. }
    I don't know if this helps, but I hope it does!
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, since you're new and learning, I wonder if you took this code from elsewhere because it's using a few things that aren't truly beginner (but I suppose if you're reading, you could have learned them and put them in..)
    Anyways, beyond that, : "SetScaleOnPlayer" sets the localScale 3 times in a row. Each assignment overrides the previous one, meaning that only the last one is ever used (first 2 are essentially doing nothing).
    If you take that knowledge, can you re-write it so it works as you hope?
     
  5. XChillX

    XChillX

    Joined:
    Jan 9, 2016
    Posts:
    8
    When i remove the first 2 setlocalscales's , the objects scale gets reset but only on some axis. Yeah i am getting the code from somewhere else, but I write it out as i go so i can understand it. Also, In game, the objects scale gets changed in real time as the camera moves up and down, so I feel as though editing my script (Which only changes the object once it has been picked up) wouldn't do anything. Any suggestions?
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, if those xScale, yScale zScale are the ones you want to change to, on each axis, re-write the :
    Code (csharp):
    1. transform.localScale = new Vector3(xScale, yScale, zScale);
     
  7. XChillX

    XChillX

    Joined:
    Jan 9, 2016
    Posts:
    8
    I've done that, but now it just does the same thing to a lesser scale. Also, just noticed, if i try and make the secondary camera not a part of the first camera, and just a part of the FPSController, it rly stuffs up. Don't know if thats a thing that should effect anything, i think its just the second camera not being able to keep up with first camera.

    EDIT: I tried to make the sensitivity of the first camera and unparerenting the second camera from the first, its the same result as above when i tried to remove the second camera from the first
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Not really sure why you need two cameras to begin with, but I guess I don't know your situation.
    As for why the scale is messing up, I have no idea..
    Well, the code you changed, was that when the weapon was picked up/dropped?
    Did the values you set look the same in the inspector for its scale?
    If any parent object is scaled at all, btw, then that will affect (any) child's scaling.. So, if that's an issue, you have to change/adjust for that.
    Beyond that, I'm not sure what to say..
     
  9. XChillX

    XChillX

    Joined:
    Jan 9, 2016
    Posts:
    8
    I need too camera's so that U can't see the gun going through objects, such as the "weapon camera" only renders game objects on the "Weapon" layer with a high depths than the other camera, so it appears as though the weapon is always in front of u, and never in objects. Yeah the values I set are the same in the inspector (Direct Copy Pastes) Also I did scale the Fps Controller ( The parent of all the camera's) So idk if that is effecting the scaling of the weapon, as its only when i look up or down that the weapon scales. As for the code i changed, The code applies to objects when They are "picked up", so the code doesn't run in real time. Do you think putting the code under void Update so that it runs once a frame is a good idea? will it work?
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    That's completely unnecessary in this case, to update the scale every frame. Once the parent is changed, and then the scale set.. it stays forever until parent and/or scale is changed.

    I was thinking something else when I asked about the inspector values, but I think my train of thought isn't relevant anymore...
    I get what you're saying about the 2 cameras, I think.. whatever's working :)

    I didn't know/remember that the scaling was only broken when you went up/down..
    Makes me wonder like is it because only 1 camera moves (do they both move)?.. never written anything quite like that before, so I don't have any real experience to fall back on :)
     
  11. XChillX

    XChillX

    Joined:
    Jan 9, 2016
    Posts:
    8
    As you can see the camera trick here with the two camera's, the weapon is clearly going through the barrier, but the game scene doesn't see that, it sees the weapon in front of it, not obscured. You can see in the pictures too, the rifle is huge when i look up or down, but normal when i look straight. bandicam 2017-05-05 21-11-03-081.jpg
     

    Attached Files:

  12. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You didn't have to prove it to me, but I get it :) I dunno if it's my eyes or what.. but it kinda looks like you just see more of the gun when you're facing up/down.. Could just be me. :)
    I think I take that back.. ya it looks strange.. hmm

    Weird.. Do your cameras change anything about themselves when you're looking up/down?
     
  13. XChillX

    XChillX

    Joined:
    Jan 9, 2016
    Posts:
    8
    No they don't, Rotation is all, (of course), no scaling in the camera, nothing. This just, happens...
     
  14. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I do hope someone else can help you :)
     
  15. Evil_Moo

    Evil_Moo

    Joined:
    Jan 23, 2014
    Posts:
    25
    I'd recommend keeping your character controller transform at scale(1,1,1), or at least uniform scale. At non-uniform scales rotating children will stretch them according to the parent's scale (so if your Y is higher scale than your X or Z, looking up will stretch the child object proportionately).

    If you want to change the character controller size you should use the height and radius values on the controller/collider component, not the transform's scale.
     
    GlorifiedPig and XChillX like this.
  16. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Hey cool, hopefully that fixes his issue :)
     
  17. sgower

    sgower

    Joined:
    Mar 9, 2015
    Posts:
    316
    If your parent object can't have a 1,1,1 scale, and you want to avoid the weird stretching of the children, you can work around the problem by creating an empty game object with 1,1,1 scale between the parent and the children. I've ran around this problem before, and this workaround fixed the issue. I wish this wasn't necessary because it's a hassle, but it does work.
     
    Matt_Raph and XChillX like this.
  18. XChillX

    XChillX

    Joined:
    Jan 9, 2016
    Posts:
    8
    Thanks heaps both of you! I didn't know that scaling the parent meant that all children were scaled too >.> Haha I'll remember that for next time! Thanks heaps!
     
  19. Goaf

    Goaf

    Joined:
    Dec 29, 2017
    Posts:
    2
    Hey, I know that this post is a bit old, but I had exactly the same problem! Using the advice from the previous commenters, I managed to come up with something that ...funnily... worked for me!

    I used the same technique mentioned by Spanky11. My camera and "rifle" gun were a child to the player. So I added an empty gameObject between the two.

    However, when I tried to scale the player now, even with the gameobject between them, the gun was still really stretched. So, on the intermediary gameObject between the camera and the player, I scaled that to half its normal scale (while the player was scaled to 2). These somehow cancelled each other out. It looked like:

    Player; scale: 2
    ----- empty Gameobject; scale: 0.5
    -------------- Camera; scale: 1
    --------------------------Rifle; scale: 1

    Hope this helps!
     
    Meow_meow23 and LimonNumber1 like this.
  20. biohazard_4540

    biohazard_4540

    Joined:
    Oct 16, 2019
    Posts:
    1
    its work for me heheh thanks
     
  21. General_Pierogi

    General_Pierogi

    Joined:
    Jan 19, 2021
    Posts:
    1
    didnt worked for me meh.... i had to set parent to 1,1,1 for streching to stop cuz even when i created empty object with 1,1,1 scale it changed absolutely nothing :/
     
  22. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    I think a more robust approach is to only parent to unscaled objects. You can do this by adding unscaled "carrier" objects in your game object hierarchy next to anything you scale and using them as parents for descendent objects.

    Example:
    Hips (unscaled)
    ->ThighVisual (scaled) [parent: Hips]
    ->ThighCarrier (unscaled) [parent: Hips]
    -->LowerLegVisual (scaled) [parent: ThighCarrier]
    -->LowerLegCarrier (unscaled) [parent: ThighCarrier]
    --->FootVisual(scaled) [parent: LowerLegCarrier]
    --->FootCarrier (unscaled) [parent: LowerLegCarrier]