Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 sidvod91 · Mar 20, 2018 at 11:34 PM · destroysizecubegrab

Grabbing a cube makes it expand and increase in size.

I have a cube in a game which I can grab and drop. The code below to do so works fine in that I can grab and drop the cube (with a key trigger or with a Touch controller). But every time I grab the cube, it's like it's being "stretched" and it increases in size to become a bigger cube, which I don't want to happen. I have executed this code before without this problem in a different project but now I can't figure out why the code to grab an object "stretches" its size and increases its volume. The cube is a rigid body with a box collider around it. Any help appreciated!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Grab_supermrkt : MonoBehaviour
 {
 
 
     public OVRInput.Controller controller;
     public string buttonName;
 
     public float grabRadius;
     public LayerMask grabMask;
 
     private GameObject grabbedObject;
     private bool grabbing;
 
     void GrabObject()
     {
 
         grabbing = true;
         RaycastHit[] hits;
         hits = Physics.SphereCastAll(transform.position, grabRadius, transform.forward, 0f, grabMask);
 
         if (hits.Length > 0)
         {
             int closestHit = 0;
 
             for (int i = 0; i < hits.Length; i++)
             {
                 if (hits[i].distance < hits[closestHit].distance) closestHit = i;
 
             }
 
             grabbedObject = hits[closestHit].transform.gameObject;
             grabbedObject.GetComponent<Rigidbody>().isKinematic = true;
             grabbedObject.transform.position = transform.position;
             grabbedObject.transform.parent = transform;
         }
     }
 
     void DropObject()
     {
         grabbing = false;
         if (grabbedObject != null)
         {
             grabbedObject.transform.parent = null;
             grabbedObject.GetComponent<Rigidbody>().isKinematic = false;
             grabbedObject.GetComponent<Rigidbody>().velocity = 2 * (OVRInput.GetLocalControllerVelocity(controller));
             grabbedObject.GetComponent<Rigidbody>().angularVelocity = OVRInput.GetLocalControllerAngularVelocity(controller);
             grabbedObject = null;
         }
     }
     // Update is called once per frame
     void Update()
     {
 
         if (!grabbing && Input.GetAxis(buttonName) == 1)
         {
             GrabObject();
         }
       
             if (grabbing && Input.GetAxis(buttonName) < 1)
             {
                 DropObject();
 
             }
     }
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Bunny83 · Mar 21, 2018 at 12:20 AM

Just make sure the object where this script is attached to (as well as all parents of this object) has a scale of (1,1,1). Any non uniform scale can mess up an object attached as child.

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 sidvod91 · Mar 22, 2018 at 05:19 PM 0
Share

That was indeed the issue, thank you!

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

80 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

Related Questions

Destroy Object on Mouse Click 5 Answers

Explosion before destroying 2 Answers

Something wrong with destroying script 0 Answers

How to display a picture with his own pixel size on a cube? 0 Answers

Moving the grabbed objects changes their scale 0 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