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 Shikomizue · Apr 24, 2015 at 06:01 PM · movementplayerswingswinging

Swinging the player around a bar

Hi there,

Im making an FPS Platformer and want du implement a funkcion where the Player can swing around a bar using a rope. What i want is to press a button and as long as its pressed i want the player to swing back and forth until i release the button and if the button is released while swinging eg. forward gets some push toward that direction. So to be clear i dont need a complete free swinging motion, but only in the specific axis the bar is positioned along. I already tried a lot, but couldnt get the result i wanted. Btw if you have a solution with using hingejoints, i would appreciate an explanation of the concept of using it, because i cant grasp it that well. ;D I thank in advance to any kind of answer that might solve the problem in any way. :)

Here is the script i have so far, but obviously it doesnt work :

The raycast is just to do get distance, but that can wait till i get the swinging motion alright :)

pragma strict

var distance : float; var maxDistance : float = 30; var TheSystem : Transform ; var Player : Transform; var AnkerPunkt : Transform;

var hingeObj : Transform; var hinge : HingeJoint; var hingeLength : float = 0f; var transitionLength : float = 1f; var transitionStartTime : float = 0f; var rb : Rigidbody; var playerBody : GameObject;

function Start() {

   rb = playerBody.GetComponent (Rigidbody);
 

}

function Update () { if( Input.GetButtonDown("Fire1")) {

   rb.isKinematic = false;
      rb.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
  //Schwung();
   hinge = playerBody.AddComponent (HingeJoint);
   hinge.anchor = transform.InverseTransformPoint(hingeObj.position);
   hingeLength = hinge.anchor.magnitude;
   hinge.axis = transform.InverseTransformDirection(Vector3.forward);
   hinge.autoConfigureConnectedAnchor = false;
   transitionStartTime = Time.time;    

}

if ( Input.GetButtonUp("Fire1")) { Destroy(hinge); transitionStartTime = Time.time; rb.isKinematic = true; }

}

function Schwung () { var hit : RaycastHit; if (Physics.Raycast (TheSystem.transform.position, TheSystem.transform.TransformDirection (Vector3.forward), hit)) { distance= hit.distance; if(distance < maxDistance) { // AnkerPunkt.position=hit.point ; // AnkerPunkt.transform.rotation = Quaternion.identity ; // Schwingen (AnkerPunkt);

      }
  }

}

function FixedUpdate(){ rb.AddForce(new Vector3(Input.GetAxis("Horizontal")*10f, 0, 0)); if(hinge){ hinge.anchor = transform.InverseTransformPoint(transform.position-(transform.position-hingeObj.position).normalized*hingeLength); transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(Vector3.forward, hingeObj.position - transform.position), (Time.time-transitionStartTime)/transitionLength); }else{ transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(Vector3.forward), (Time.time - transitionStartTime)/transitionLength); } }

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 siaran · Apr 24, 2015 at 06:52 PM 0
Share

Your code formatting is totally borked, you may want to fix that. I've never worked with hingejoints and don't know how they work so cannot really think of anything that could help you. But maybe if your code was formatted correctly I could get something out of it :)

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

How can I get player to move when only touching an object 0 Answers

Have a character face their moving direction? 1 Answer

Player Controller moves automatically forward. cant stop. 1 Answer

Rotate game object and then return to its original rotation 1 Answer

Problem with camera and character controller (fighting game) 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