• 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 collinator7 · Jul 30, 2014 at 08:33 PM · 2dphysicsteleporthingejointhingejoint2d

HingeJoint2D Teleportation

I'm making a game where the character sways back and forth on a hinge joint, and when the player presses space, it releases the hinge joint, and fires you in the direction you were pointing when you pressed space. I have a circle collider on the player, and I have a statement that checks for when the player is upright after rolling for a while. What I'm trying to get it to do is when the player rolls into an upright position, I want it to reset the hinge joint at that location, right under the player's feet (at the groundCheck pos). What it ends up doing is teleporting the character very far along the x axis. Help? Thanks!

Code:

 public HingeJoint2D hinge;
     public float force = 150f;
 
     private JointMotor2D motor;
     private JointAngleLimits2D limits;
     
     public Transform groundCheck;
     public LayerMask whatIsGround;
     private bool grounded = false;
     private float groundradius = 0.2f;
 
     private bool anchorDone = false;
 
     public float speed;
 
     void Start () {
         //Motor changer
         motor = hinge.motor;
         limits = hinge.limits;
         limits.min = -30;
         limits.max = 30;
     }
 
     void FixedUpdate () {
         //Ground checking
         grounded = Physics2D.OverlapCircle(groundCheck.position, groundradius, whatIsGround);
 
         //Wobbling
         if(hinge.limitState == JointLimitState2D.UpperLimit) {
             motor.motorSpeed = -force;
             
             hinge.motor = motor;
         } else if (hinge.limitState == JointLimitState2D.LowerLimit) {
             motor.motorSpeed = force;
             
             hinge.motor = motor;
         }
 
         //Jump
         if (grounded && Input.GetKeyDown(KeyCode.Space)) {
             hinge.enabled = false;
     
             rigidbody2D.AddForce(new Vector2 (0, 500));
 
             anchorDone = false;
             grounded = false;
         }
     }
 
     void Update () {
         //Reset wobble
         if (grounded && !anchorDone) {
             transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.identity, speed * Time.deltaTime);
 
             if (transform.rotation == Quaternion.identity) {
                 hinge.limits = limits;
                 hinge.anchor = groundCheck.position;
                 hinge.connectedAnchor = groundCheck.position;
                 hinge.enabled = true;
 
                 anchorDone = true;
             }
         }
     }

Picture of what is happening when you become upright:alt text

screen shot 2014-07-30 at 1.30.57 pm.png (13.4 kB)
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
0
Best Answer

Answer by collinator7 · Aug 02, 2014 at 05:08 AM

SOLVED I had the a random rigidbody2d attached... Derp.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

HingeJoint with no rotation? 0 Answers

Why does hingejoint push part upward?,Why does hinge joint make the part move up? 0 Answers

Using HingeJoint2D to Join Objects at Specific Points 2 Answers

Rigidbody2D characters push each other 0 Answers

pinball flipper using Hinge spring not strong enough 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges