- Home /
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); } }
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 :)
Your answer
Follow this Question
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