- Home /
Wind Forcing on a CharacterController Object
I'm trying to figure out how to make "wind" force a character to a direction. I mainly want it so that if the player jumps over a canyon, the wind will guide him to the other side. I set something like this by doing this:
I set a trigger box and made it so that when the player collides with the box, it make the variable "inWind" to true in the player's ThirdPersonController script and this is the code I added to the ThirdPersonController's ApplyGravity function:
if (inWind)
{
moveDirection = constantForce.force = Vector3.forward * -1.5;
//constantForce.relativeForce = Vector3.up * -1.5;
//moveDirection = Vector3.forward * -1.5;
verticalSpeed = 0.0;
gravity = 0;
}
However, the player is able to "move" against the wind. I don't want to make the player unable to move at all, because I still want the player to still be able to glide themselves. Any suggestions?
Here is the code attached to the windzone.
function OnTriggerEnter (hit : Collider) { if (hit.transform.tag == "Player") { var controller : ThirdPersonController = hit.GetComponent(ThirdPersonController); Debug.Log("enter"); hit.gameObject.GetComponent(ThirdPersonController).inWind = true; //controller.isTalking = true; //controller.animation.Play("swim"); } }
function OnTriggerExit (hit : Collider) { var controller : ThirdPersonController= hit.GetComponent(ThirdPersonController); if (controller.IsGrounded !=null) hit.gameObject.GetComponent(ThirdPersonController).inWind = false; //controller.animation.Stop("swim"); controller.gravity = controller.defaultGravity; //20.0 //controller.isTalking = false; Debug.Log("Left the water"); }
UPDATE Here is the current windzone code. The windDirection variable shows up in the inspector and I am using the X and Z variable as the wind direction.
var windDirection = Vector3.zero;
function OnTriggerEnter (hit : Collider) { if (hit.transform.tag == "Player") { var controller : ThirdPersonController = hit.GetComponent(ThirdPersonController); Debug.Log("enter"); hit.gameObject.GetComponent(ThirdPersonController).inWind = true; //controller.isTalking = true; //controller.animation.Play("swim"); } }
function OnTriggerExit (hit : Collider) { var controller : ThirdPersonController= hit.GetComponent(ThirdPersonController); if (controller.IsGrounded !=null) controller.inWind = false; //controller.animation.Stop("swim"); controller.gravity = controller.defaultGravity; //20.0 //controller.isTalking = false; Debug.Log("Left the water"); }
function Update() { var controller : ThirdPersonController= GetComponent(ThirdPersonController);
if (controller.moveDirection != windDirection && controller.inWind)
{
controller.moveDirection = windDirection;
}
}
Could you elaborate on the "Can't walk against the force but you can jump" part?
With example 1, the player has already jumped off the ground, so while he is in the wind traveling across the crayon rift, he won't be able to jump. However, in example 2, since the player is still being affected by gravity and is on the ground, he will be able to jump up naturally, even if being affected by the wind.
Answer by Atnas1010 · Oct 28, 2010 at 06:14 PM
Assuming you are using the default ThirdPersonController.js, find this line
moveSpeed = Mathf.Lerp(moveSpeed, targetSpeed, curSmooth);
and add these lines after it
if (inWind)
moveSpeed *= 0.2;
Does that give the desired movement behavior?
Do you have some windzones that need to have one half where you can jump, and another half where you can't? If you don't I've slightly modified the code you posted to make it work as I think you want it to. And remember to remove the part you have added to the Apply section.
function OnTriggerEnter (hit : Collider) { if (hit.tag == "Player") { var controller : ThirdPersonController = hit.GetComponent(ThirdPersonController); Debug.Log("enter"); controller.inWind = true; if (gameObject.tag == "WindzoneNoGround") { controller.verticalSpeed = 0.0; controller.gravity = 0; } //cotroller.isTalking = true; //controller.animation.Play("swim"); } }
function OnTriggerExit (hit : Collider) { if (hit.tag == "Player") { var controller : ThirdPersonController= hit.GetComponent(ThirdPersonController); controller.inWind = false; if (gameObject.tag == "WindzoneNoGround") controller.gravity = controller.defaultGravity; //20.0 //controller.isTalking = false; //controller.animation.Stop("swim"); Debug.Log("Left the water"); } }
Does this work as it should?
I understand what you are saying and the logic behind it, but I don't how to do it in code. The way you explained it is how I want to program it, but the code part is the part I can't figure out...
Right now, inWind is just a flag. I posted the code attached to the find zone.
I had the gravity part working, But now my problem is that the Windzone isn't applying force constantly to the player. I added the code in my post.
Do you set the moveSpeed anywhere? What happens if something leaves the trigger, and it's not the player (look at the code I posted) Have you removed this line: moveDirection = constantForce.force = Vector3.forward * -1.5; since you set the moveDirection from the other script.
I assume you've made moveDirection public (otherwise it probably wouldn't compile)
Answer by TheDemiurge · Oct 29, 2010 at 02:22 PM
The ThirdPersonController.js script has this handy gem in it:
private var isControllable: boolean;
If you want the player to just wait out the wind, just disable this one. Also check out the CanJump variable in there. One of these two, or some variation thereof, should do the trick :)
Probably a few ways of doing this. If it has to be an actual physics force, You could set a trigger that will parent the player temporarily to an empty GameObject that has a rigidbody and is affected by this force. Remember, if you apply a tiny force every frame it gives acceleration, where applying a larger force for a single frame would yield a s$$anonymous$$dy speed. It depends how realistic you want it. (or your could set rigidbody's Velocity, etc)
Answer by HolBol · Oct 28, 2010 at 11:08 PM
you could use a rigidbody charcontroller and use .addforce
I'm using a character controller and for some reason it interferes with it.
Your answer
