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 /
This question was closed Apr 20, 2016 at 01:35 AM by skylem for the following reason:

Probably a duplicate and very old.

avatar image
0
Question by skylem · Mar 10, 2014 at 03:12 PM · javascripterrorcharactercharactercontroller

How to fix? Character grabbing system

I wrote this last night, i am trying to recreate the grabbing function of the game little big planet (Those who haven't played it allows for grabbing of objects on keystroke whether it be a ball to drag around or a rope to swing from)

so everything is functional besides the Hinge im trying to apply and i am not sure why, as it is the first time i've ever tried to place a hinge dynamically, any input would be massively appreciated

the script will be applied to the object im trying to grab, if this is isn't enough information please let me know, i tried the source and google couldn't make sense of the source and couldn't find any legitimate guides through google so all i have left is to post and hope.

Script Revision:

 public var IsGrabbing : boolean = false;
 public var CanGrab : boolean = false;
 
 public var Player : Transform;
 public var c : Transform;
 
 function OnTriggerEnter(Other : Collider){
 if (Other.gameObject.tag == "Player"){
         CanGrab = true;
 
             
             }
 
         
     }
 function OnCollisionEnter(c : Collision) {
                             if (IsGrabbing == true) {
                 var joint = gameObject.AddComponent(HingeJoint);
     joint.connectedBody = c.rigidbody;
     }
 
 }
 
         
 function OnTriggerExit(Other : Collider){
 if (Other.gameObject.tag == "Player"){
         CanGrab = false;
 
             
             }
         }
 
 function Update() {
         if          (Input.GetKeyDown(KeyCode.G) && CanGrab == true) {
                     IsGrabbing = true;
                  }
         if          (Input.GetKeyUp(KeyCode.G)) {
                     IsGrabbing = false;
                  }
         if    (CanGrab == false) {
                 IsGrabbing = false;
                 }
                  
         
 
      if (IsGrabbing == false) {
                  Destroy (gameObject.GetComponent(HingeJoint));
             }
 
     }
 

EDIT: Thanks Chris for the pointers i will have to look into functions more as the only 3 i understand won't work for Adding the joint or at the very least i need to re examine what i'm trying to achieve

Thanks wibble i was planning to restrict its movement once have some basic functionality so that will be of use in the future, i definitely still have alot to learn because i had no ideas on that matter at all.

EDIT: Ok so i made some changes to the script and its somewhat functional (atleast now i know that the hinge is being placed) i believe i need to look into its constraints though because when i move my character the object doesn't, i added the revision if anyone has any additional feedback, Thanks again at the very least for the ideas on what to google and mostly for taking the time to reply.

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 stevethorne · Mar 10, 2014 at 03:26 PM 0
Share

Please use the code button to enter your code in, the formatting for this is hard to read.

1 Reply

  • Sort: 
avatar image
1

Answer by wibble82 · Mar 10, 2014 at 04:26 PM

Hi there

Couple of things:

  • you're using a fixed joint there, not a hinge joint - I'm guessing you knew that but thought it worth pointing out

  • I've tried the procedure you're doing and it does seem to work. Are you sure you're trying to add components linking the correct bodies - both obviously have to have rigid bodies and it'd get upset if they didn't. Try doing a Debug.Log of both object's rigid bodies and make sure they're present

  • It looks from your code like you're trying to re-add the joint every frame (if IsGrabbing==true). This should only be done once, when the grab is first instantiated

  • You will also need to set the anchor and connectedAnchor values (to say where the hinge is), and the axis (to make sure the constraint is operating in the correct direction)

Good luck with it - interesting to see it done using the 3d constraints - we had a 2d engine (operating with collision layers to make it appear 3d across 7 layers) in LBP, so it was more like a simple pin joint. If the hinge doesn't work well, might be worth looking at unity's 2d physics.

-Chris

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 wibble82 · Mar 10, 2014 at 05:59 PM 0
Share

Cool. If you're sticking with the 3d physics engine, but want to make a 2d style game (maybe with multiple layers), look out for the properties of the rigid body that let you restrict rotation and position. You can tell a rigid body 'only move in the xy plane', and 'only rotate about the z axis'. That gives you '2d' physics, but still using the 3d engine. Otherwise you can end up with objects very gradually drifting into odd orientations/positions due to tiny innacuracies building up over multiple frames.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Java Script Error since Unity 5? 1 Answer

Background & sprites are smearing towards the player during movement. Help determine the cause and solution? 0 Answers

CharacterController doesn't rotate on my script... 1 Answer

Translation of an object 2 Answers

How can I implement a SimpleMove max speed? 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