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 MrPanth · May 17, 2016 at 01:47 PM · scripting problemcollisiongameobjectcollidercharactercontroller

GameObjects glitching through other Objects when being held

So I'm very new to coding and I am only slowly beginning to get the basics but I got this script from a video (https://www.youtube.com/watch?v=abp3PyOcy9g) but it doesn't work properly. I don't know if I did something wrong but I can't lift the gameobject off the ground and when ever I hold it infront of another gameobject It glitches through and spazzes out. If I let go it just flies away or goes through the object and is gone for ever. This is extremely inconvienent and I can't seem to find an answer or a better script. If you know what's wrong then please help or just give me a whole new script. I don't really know how to code and am still learning so it would be fantastic if you could post full codes and describe what is happening in it. It would help out a bunch. Thanks!

 #pragma strict
 
 var catchRange = 30.0;
 var holdDistance = 4.0;
 var minForce = 1000;
 var maxForce = 10000;
 var forceChargePerSec = 3000;
 var layerMask : LayerMask = -1;
      
 enum GravityGunState { Free, Catch, Occupied, Charge, Release};
 private var gravityGunState : GravityGunState = 0;
 private var rigid : Rigidbody = null;
 private var currentForce = minForce;
      
 function FixedUpdate() 
 {
    if(gravityGunState == GravityGunState.Free) 
    {
      if(Input.GetButton("Fire2")) 
      {
          var hit : RaycastHit;
         if(Physics.Raycast(transform.position, transform.forward, hit, catchRange, layerMask)) 
         {
             if(hit.rigidbody) 
             {
                 rigid = hit.rigidbody;
                 gravityGunState = GravityGunState.Catch;
              }
         }
       }
     }
 
         else if(gravityGunState == GravityGunState.Catch) 
         {
             rigid.MovePosition(transform.position + transform.forward * holdDistance);
             if(!Input.GetButton("Fire1"))
                 gravityGunState = GravityGunState.Occupied;
         }
         
         else if(gravityGunState == GravityGunState.Occupied) 
         {
             rigid.MovePosition(transform.position + transform.forward * holdDistance);
             if(Input.GetButton("Fire1"))
             gravityGunState = GravityGunState.Charge;
         }
         
         else if(gravityGunState == GravityGunState.Charge) 
         {
             rigid.MovePosition(transform.position + transform.forward * holdDistance);
             if(currentForce < maxForce) 
         {
                 currentForce += forceChargePerSec * Time.deltaTime;
         }
          
          else 
          {
                 currentForce = maxForce;
          }
          
          if(!Input.GetButton("Fire1"))
          gravityGunState = GravityGunState.Release;     
         }
         
         else if(gravityGunState == GravityGunState.Release) 
         {
             rigid.AddForce(transform.forward * currentForce);
             currentForce = minForce;
             gravityGunState = GravityGunState.Free;
         }
     }
      
     @script ExecuteInEditMode()

(this script is attached to the FPSController if you were wondering) Also I know how to use unity just not scripting. (just thought I'd add that incase)

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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

My Character controller is being push a little bit when i'm about the get the coins. How can i possibly detect collision and destroy the coins without actually getting affected by the colliding physics? 1 Answer

Collisions between two CharacterController objects? 1 Answer

Make isKinematic false when touching a Collider? 1 Answer

Making the collider change after being instantiate 2 Answers

How can I add the OnTriggerEnter function to all game objects that I instantiate? 1 Answer


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