Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by somerandomhacker · Jan 26 at 01:24 PM · gunpick up

Why is my gun stretching out when I pick it up?

When my gun is picked up, the transform changes from 0.02 on all axes to 1 on all axes. If it helps, here is my pick up system script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PickUpSystem : MonoBehaviour
 {
     public GunSystem gunScript;
     public Rigidbody rb;
     public BoxCollider coll;
     public Transform player, gunContainer, fpsCam;
 
     public float pickUpRange;
     public float dropForwardForce, dropUpwardForce;
 
     public bool equipped;
     public static bool slotFull;
 
     private void Start()
     {
         //Setup
         if (!equipped)
         {
             gunScript.enabled = false;
             rb.isKinematic = false;
             coll.isTrigger = false;
         }
         if (equipped)
         {
             gunScript.enabled = true;
             rb.isKinematic = true;
             coll.isTrigger = true;
             slotFull = true;
         }
     }
 
     private void Update()
     {
         //Check if player is in range and "E" is pressed
         Vector3 distanceToPlayer = player.position - transform.position;
         if (!equipped && distanceToPlayer.magnitude <= pickUpRange && Input.GetKeyDown(KeyCode.E) && !slotFull) PickUp();
 
         //Drop if equipped and "Q" is pressed
         if (equipped && Input.GetKeyDown(KeyCode.Q)) Drop();
     }
 
     private void PickUp()
     {
         equipped = true;
         slotFull = true;
 
         //Make weapon a child of the camera and move it to default position
         transform.SetParent(gunContainer);
         transform.localPosition = Vector3.zero;
         transform.localRotation = Quaternion.Euler(Vector3.zero);
         transform.localScale = Vector3.one;
 
         //Make Rigidbody kinematic and BoxCollider a trigger
         rb.isKinematic = true;
         coll.isTrigger = true;
 
         //Enable script
         gunScript.enabled = true;
     }
 
     private void Drop()
     {
         equipped = false;
         slotFull = false;
 
         //Set parent to null
         transform.SetParent(null);
 
         //Make Rigidbody not kinematic and BoxCollider normal
         rb.isKinematic = false;
         coll.isTrigger = false;
 
         //Gun carries momentum of player
         rb.velocity = player.GetComponent<Rigidbody>().velocity;
 
         //AddForce
         rb.AddForce(fpsCam.forward * dropForwardForce, ForceMode.Impulse);
         rb.AddForce(fpsCam.up * dropUpwardForce, ForceMode.Impulse);
         //Add random rotation
         float random = Random.Range(-1f, 1f);
         rb.AddTorque(new Vector3(random, random, random) * 10);
 
         //Disable script
         gunScript.enabled = false;
     }
 }


If anyone could help me that would be great :)

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image andrew-lukasik · Jan 26 at 12:38 PM 0
Share

make sure that gunContainer's scale is ( 1 , 1 , 1 ) or ( n , n , n ) (and all it's parents'), it will stretch otherwise

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by privatecontractor · Jan 26 at 02:38 PM

Hi @somerandomhacker,

because in void Update() You order you gun to: transform.localScale = Vector3.one; So if you don't know scale is just another Vector3... and Vextor3.one = (1,1,1).

If you don't need this to happen just cut this out...


If issue will still occur you could also SetParent for:

transform.SetParent(gunContainer, false);

SetParent -> have one parameter "hidden", by hidden you need to understand that it have default value and if you not assign it during call, it will act according to default value... this parameter is:


worldPositionStays "If true, the parent-relative position, scale and rotation are modified such that the object keeps the same world space position, rotation and scale as before". Hope will do, let me know...

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image somerandomhacker · Jan 26 at 11:47 PM 0
Share

Thanks! The gun no longer becomes bigger, but it still stretches. I deleted transform.localScale = Vector3.one; and transform.SetParent(gunContainer, false); doesn't seem to do anything. It also stretches further every time I drop and pick it up. Anything else that might fix this would be awesome. Thanks! EDIT: I came across a very simple fix in the tutorial comment section. All I had to do was move the gun and the gun container to 0 on all axes. Thank you so much for your help with the stretching, though!

avatar image
0

Answer by Chimer0s · Jan 27 at 04:33 AM

It's likely that the parent object you're assigning it to has an asymmetrical scale and that's used as a multiplier when childing the gun to it. If the object has a parent in its hierarchy with a scale where the x,y,z values don't match each other, then depending on the rotation of its children, they'll get stretched out according to the differently sized axes.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

134 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

pick up ammo from specific gun? 1 Answer

Pick up and Drop a gun 1 Answer

Character Rotation Doesn't work 0 Answers

hey here wesley again sound for my gun 1 Answer

FPS Gun Attached To Camera Weirdness 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges