- Home /
OnTriggerEnter (Slow Player Speed) Need Help!
Okay so I'm trying to code an OnTriggerEnter event for a "SlowingSludge" of sorts, so I have my own CharacterController script called "Character_Controls" and I have a plane with a sludge diffuse texture and a box collider attached thats set as a trigger but I dont know where to begin on referencing my walkspeed variable and lowering the float value from my controller when the player enters. (Feel free to use my controller code if you like) Thanks!!!
private var walkSpeed : float = 0.5;
private var gravity = 50.0; //New variable for code involving gravity private var moveDirection : Vector3 = Vector3.zero; private var charController : CharacterController;
private var strength = 0.5; //New variable for code involing objects the player collides with
//function that enables the keyboard cursors to transform the player direction + play associated annimations function Start(){ charController = GetComponent(CharacterController); }
function Update () { if(charController.isGrounded == true) //using this code, the character animations should only play while the character is grounded { if(Input.GetAxis("Vertical") > .1)//input = upward cursor key or W key { walkSpeed = 0.6; } if(Input.GetAxis("Vertical") < -.1)//input = downward cursor key or S key { walkSpeed = 0.5; } transform.eulerAngles.y += Input.GetAxis("Horizontal"); moveDirection = Vector3(0,0, Input.GetAxis("Vertical")); moveDirection = transform.TransformDirection(moveDirection); } moveDirection.y -= gravity * Time.deltaTime; charController.Move(moveDirection*walkSpeed); }
function OnControllerColliderHit (hit : ControllerColliderHit) //moves any game object with a rigidbody+collider component based on the strength variable { var body : Rigidbody = hit.collider.attachedRigidbody; var pushDir = Vector3 (hit.moveDirection.x, 0, hit.moveDirection.z);
body.velocity = pushDir*strength*10;
}
Answer by aldonaletto · Sep 24, 2011 at 12:56 AM
You could place OnTriggerEnter/Exit functions in your character script - triggers call this function in the trigger script and in the object that entered the trigger:
function OnTriggerEnter(col: Collider){ walkSpeed = 0.3; // slow down when entering }
function OnTriggerExit(col: Collider){ walkSpeed = 0.5; // restore speed when exiting } NOTE: You should check in your function OnControllerColliderHit if the object has a rigidbody before using it to avoid Null Reference errors at runtime:
var body : Rigidbody = hit.collider.attachedRigidbody; if (body){ var pushDir = Vector3 (hit.moveDirection.x, 0, hit.moveDirection.z); body.velocity = pushDir*strength*10; }
Answer by RaiderA528 · Sep 24, 2011 at 03:49 PM
Thank you very much, I'm still having a little bit of trouble with the slowing sludge, it just refuses to work, but I think that's just my machine or the way my code is layed out, think I'll need to use tags so only specific colliders that the code can call on so it will slow down. But I was bewildered by that Null reference error so thank you very much. : ) the reply time was fantastic, I'll be on here more often. :P
Your answer
![](https://koobas.hobune.stream/wayback/20220612223012im_/https://answers.unity.com/themes/thub/images/avi.jpg)