above is a video my buddy did that shows his problem with a rope. The rope is made using character joints, except for the top (which has its mesh unrendered) which uses a hinge joint. All the segments are triggers, rigidbodies using gravity and have a “ladder” tag on each segment except for the top and bottom (The reason for this is shown in the video as to what happens when the bottom segment is tagged ladder). The player climbs the rope by pressing the W or S key.
Everything is done in the player’s script. What we don’t know is if the problem is in the player, the way the rope is made or both. An alternative method to making the rope or rope climb/swing script is also welcome.
The player does not use a character controller. Below is a snippet of the code. All the climbing variable does is keep the player from moving left and right as if they were on the ground.
I have done my own rope stuff, but from looking at your code you have taken a different approach. my problem was similar, the reason was that OnTriggerEnter would sometimes be called more often than once if parenting was involved. My script was keeping track of the rope segments the player was touching, so the number got out of control, causing the player to never get unparented. Maybe your problem is related to that phenomenon? This infinite OnTriggerEnter loop can prevent OnTriggerExit from being called. I have no idea how or why though.
Maybe you can make something like this
OnTriggerStay (other: Collider){
//your code
if(transform.parent != other.transform){
transform.parent = other.transform;
}
//rest of your code
}
BTW, how did you limit the swinging of the ropes? With my ropes the player can swing all he likes and the ropes can get really weird if the player swings to much. Your swinging is really smooth and nice.
Ah, I didn’t know that. So this is actually Unity’s fault. Oh and to answer your question, what I do is first use the rigidbody constraint and lock it’s z position and xy rotation. I then have the character joints swing1 limit set to 10. I also make it so that if the player presses the horizontal keys, it adds relative force to the parent. However the rope can still overlap itself if it’s too long or if the swing limit is too high.
Now I think you’re on to having a perfectly working 2d rope swing but the problem is that the player detaches itself midswing and I think that’s because OnTriggerExit is being done despite OnTriggerStay. While swinging, it stays on but you can notice it slowly move down the rope. I need the code to be written in a way so that the solution you gave can keep the player on the rope while swinging but doesn’t automatically grab the rope or prevent the character from being able to jump off it. BTW, Thank you for your time and help.
That’s fine for me. It still feels way better than the default 40 and I think it’s good to allow some illogical stuff to be able to happen, as long as it does not beak the game.
No problem The whole infinite OnTriggerEnter loops is a bug introduced in one of the latest two updates of Unity, it certainly was not there when i originally came up with my rope. I filed a bug report, we’ll have to wait and see.
I am really busy with other stuff right now, I can’t do any game realated work right now, so I’m afraid I can’t be of much help. In fact it is just coincidence I stumbeled upon this thread. But from looking at your code, I would guess that he problem might lie here:
function OnTriggerExit ( other : Collider)
{
if (other.gameObject.tag == "ladder")
{
//rigidbody.isKinematic = false;
rigidbody.useGravity=true;
transform.rotation = Quaternion.identity;
Climbing = false;
transform.parent = null;
}
//rest
}
Looking at your video, it seems the player is for a short moment in between segments or the order in which Unity processes commands at that very moment are such that he player loses connection to its old parent but does not get parented to another segment. try throwing out that bit. From what I understend you have an invisible “mesh”-tagged segment at the top and bottom. Normally the player can only exit the rope at the top and bottom, so exiting the rope in the middle is unnecessary. If you want to jump off, that could be handeled by a jumping function or something like that, though that might take a lot of rewriting your old code.
I’m just writing off the top of my head, I am by no means a good programmer, I am just an amateur. Hopefully someone else can help you guys out. Good luck
I got it to work!!! So I did what you said and removed the function in the triggerExit but what I also did was copy and paste the code I already had on the OnTriggerStay in the OnTriggerEnter function and now the player hangs on tighter than a baby on a mother’s tit and is able to jump off just fine (it’s almost insulting that that’s all I needed to do). I didn’t even have to write new code (btw, it doesn’t use the bit of code you gave me). Why that worked, for now I don’t entirely know but as of now I haven’t found anything wrong with it.