This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
Hey all, this has been driving me crazy but right now I'm getting a frowny-face swing, with bouncing on the ends (after adjusting for the line length; otherwise it just slowly moves away from the pivot until the bob is resting on the ground) rather than a smiley-face swing. As you can see from the commented code, I've tried various debugs and alternatives for calculating this, but haven't had any luck so far. It seems to me that something simply needs to be inverted because I just need my character going up at the ends of the arc, rather than down (or straight across), but I can't for the life of me figure it out - anything that I invert sends him flying off in the opposite direction. What is wrong with my code?
I've looked at some tutorials but haven't been able to find one for unity that accomplishes a simple swing via setting the velocity of the bob in Update(), and everything I've got working so far is buggy and/or difficult to limit. I don't want to have to leave this weird stretch/bouncing thing in but so far it's all I've got.
(initial values for code initialized elsewhere start at 0)
void SimpleSwing()
{
Vector2 bob_p = player.transform.position;
Vector2 pivot_p = pivot.position;
float ropeAccelerationRate = 1f;
float airResistanceDampener = .99f;
float playerXInfluence = .15f;
ropeAngleRads = Vector2.SignedAngle(pivot_p - bob_p, Vector2.right) * Mathf.Deg2Rad;
var ropeAngleAcceleration = -ropeAccelerationRate * Mathf.Cos(ropeAngleRads);
ropeAngleAcceleration -= playerXInfluence * player.PlayerInput.normalInputX;
ropeAngleVelocity = ropeAngleAcceleration;
ropeAngleRads = ropeAngleVelocity;
ropeAngleVelocity *= airResistanceDampener;
Vector2 currentVelocity = -Calculations.GetVectorFromAngle(ropeAngleRads) * ropeAngleVelocity;
Vector2 nextPos = bob_p (currentVelocity * player.GetPersonalDeltaTime());
Vector2 nextVectorToPivot = pivot_p - nextPos;
//Vector2 adjustedNextPosDiff = nextVectorToPivot.normalized * lineLength;
//Vector2 adjustedNextPos = pivot_p - adjustedNextPosDiff;
//Debug.Log("currPos " bob_p " currVel " currentVelocity "line length " Vector2.Distance(bob_p, pivot_p) " nextPos " nextPos " line length " nextVectorToPivot.magnitude
// " adjustedNextPos " adjustedNextPos " line length " Vector2.Distance(adjustedNextPos, pivot_p));
//Vector2 adjustedVelocity = adjustedNextPos - bob_p ;
//player.velocity = adjustedVelocity;
//Debug.Log(vectorToPivot.magnitude " compared to " lineLength);
if (nextVectorToPivot.magnitude != lineLength)
{
float dist = nextVectorToPivot.magnitude - lineLength;
Vector2 movementAdjustment = dist * nextVectorToPivot.normalized;
//Debug.Log(" adjusted by " movementAdjustment);
currentVelocity = movementAdjustment;
//Debug.Log("velocity " player.velocity " UpdatedVel " currentVelocity " adjustedVel " adjustedVelocity);
}
player.velocity = currentVelocity;
}
Subreddit
Post Details
- Posted
- 1 year ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/Unity2D/com...