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.
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”
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace S3
{
public class Item_SetScale : MonoBehaviour
{
private Item_Master itemMaster;
public Vector3 itemLocalScale;
public float xScale;
public float yScale;
public float zScale;
void OnEnable()
{
SetInitialReferences();
SetScaleOnPlayer();
itemMaster.EventObjectPickUp += SetScaleOnPlayer;
}
void OnDisable()
{
itemMaster.EventObjectPickUp -= SetScaleOnPlayer;
}
void SetInitialReferences()
{
itemMaster = GetComponent<Item_Master>();
}
void SetScaleOnPlayer()
{
if (transform.root.CompareTag(GameManagerReferences._playerTag))
{
transform.localScale = new Vector3(transform.localScale.y, zScale, transform.localScale.z);
transform.localScale = new Vector3(transform.localScale.z, xScale, transform.localScale.x);
transform.localScale = new Vector3(transform.localScale.x, yScale, transform.localScale.y);
}
}
}
}
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?
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?
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
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…
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?
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
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.
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?
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.
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.
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!
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: