- Home /
My Character Controller Is Warping Randomly (/w video)
Since a picture is worth a thousand words, I uploaded a movie.
http://www.youtube.com/watch?v=E3svM38kHm4
Seconds 7-18 show desired, correct response with the character controller. Seconds 18 onwards show the incorrect response.
I did not have any issues with my code (random warping, etc.) until I added in the vehicle hit/recoil feature, but I still don't know what would be causing it. My code gets triggered in OnTriggerEnter() and then uses a CoRoutine to follow through with my code.
EDIT: In the "hitVehicle" function, it's the player that's moving - not the vehicle! It's a poorly named function with a poorly named variable on the inside that I'll be sure to correct :)
EDIT2: So, I discovered when the problem crops up. When the player gets flung (during the coroutine), if the player moves surfaces - from one collider onto another - than the problem is more likely to crop up.
Also, if I don't resume normal gameplay (e.g., leave 'hitVehicle' as active even after the coroutine is finished), the player stays in the same place. So something in my normal code is causing issues with everything... though I'm not sure what. OnTriggerEnter:
public void OnTriggerEnter(Collider other)
{
//check to see if the trigger entered is a vehicle
if (other.gameObject.layer == 15)
{
//rem yes it is, check to see if we're not standing on a vehicle
if ((activePlatform == null)||(activePlatform.gameObject.layer != 15))
{
Vector3 direction;
direction = transform.position - other.transform.position;
direction.y = 0.0f;
StartCoroutine(HitVehicle(direction));
}
}
}
Coroutine HitVehicle:
public IEnumerator HitVehicle(Vector3 direction)
{ float currentTime = 0.0f; float gravity = direction.y; Vector3 hitVelocity; direction.y = 0.0f;
//set character status to hit vehicle so that nothing else (normal character control) //can resume until set to false hitVehicle = true;
while (currentTime < 1.5) { if (!LevelManagerScript.Instance.GetFrozen()) { hitVelocity = direction * Time.deltaTime;
if (character.isGrounded) { gravity = 0; } else { gravity += (Physics.gravity.y * Time.deltaTime); }
hitVelocity.y = gravity; //convert to real world coordinates transform.TransformDirection(hitVelocity); //move the character character.Move(hitVelocity); //update timer currentTime += Time.deltaTime; Debug.Log("moving");
//update active platform based on collisions if (activePlatform != null) { //get values activeGlobalPlatformPoint = transform.position; activeLocalPlatformPoint = activePlatform.InverseTransformPoint(transform.position); }
//Verify we're ontop of a platform VerifyAbovePlatform(); }
yield return null; }
//reset velocity velocity = Vector3.zero;
//set forward speed to nothing forwardSpeed = 0.0f;
//let the player get hit by the vehicle again hitVehicle = false;
//clear out last line of the log so it looks 'clear' Debug.Log(" "); }
And here's the tail beginning of my Update function.
void Update (){
//leave if paused
if (LevelManagerScript.Instance.GetFrozen())
return;
//leave function if hit by vehicle
if (hitVehicle)
return;
....
}
I just implemented this last night, and I know there are some more improvements that could be made. But I don't understand where the 'warping' part comes into play? Does anyone else know what might cause this to happen?
Hmm... the only reason I could see this beco$$anonymous$$g a problem is if you had crossed "transform.position" with "transform.localPosition". Given that you didn't do that, at least not in the script you've got posted here, I can't rightly say what would cause this. It certainly isn't normal, that much is for sure.
However, it may have something to do with this:
"character.$$anonymous$$ove(carVelocity);"
If you look in the documentation for CharacterController.$$anonymous$$ove, you'll note that they use "*Time.deltaTime" to modify the rate.
Then again, it may just be something completely different, within some script that would affect the playercontroller, that you've put in. If you've got any additional information on that, it may help to get an answer.
Hey Desktruktorr, I found some code snips for letting the player slide off of terrain and also be locked onto terrain (e.g., moving platforms). I noticed that I wasn't updating those transform points during the recoil sequence (because they weren't used at all during recoil).
Could that also be the issue? Like I said in an edit, the player moves correctly during recoil UNTIL it goes back into normal gameplay mode, where those transform points then become used as a part of movement again.
That is the only thing I can possibly think of - that if being hit on PlatformA knocks you back on to PlatformB, when the first frame of normal character movement code starts up again, it thinks you're on PlatformA still and tries to move you accordingly.
This would also explain why getting hit on a single collider works fine, it's moving between colliders that's an issue.
Your answer
Follow this Question
Related Questions
Character Controller Collision 4 Answers
2.5D platformer main character collision 1 Answer
Problem with Movement Script 0 Answers
Unity 4 to 5 Character Controller Issue 1 Answer
My Character Controller is falling through... everything... 4 Answers