- Home /
Excesive bumping
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);
}
}
The easiest thing would be to set the player character's transform as a child of the bar, and rotate the bar to create the effect of "swinging". You should probably deactivate stuff like movement when you do that.
Follow this Question
Related Questions
attach a rag doll to a moving/spinning object? 0 Answers
Swinging the player around a bar 0 Answers
How to climb a rope? 1 Answer
PENDULUM EFFECT 0 Answers
stop character join swing 0 Answers