x


Wind Forcing on a CharacterController Object

alt text 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;
    }
}
more ▼

asked Oct 28 '10 at 04:18 AM

SirVictory gravatar image

SirVictory
1.7k 66 77 104

Could you elaborate on the "Can't walk against the force but you can jump" part?

Oct 28 '10 at 04:48 AM Atnas1010

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.

Oct 28 '10 at 05:42 AM SirVictory
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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?

more ▼

answered Oct 28 '10 at 06:14 PM

Atnas1010 gravatar image

Atnas1010
1.1k 6 10 26

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...

Oct 28 '10 at 07:50 PM SirVictory

Right now, inWind is just a flag. I posted the code attached to the find zone.

Oct 30 '10 at 06:32 AM SirVictory

Edited my post again

Oct 31 '10 at 05:54 PM Atnas1010

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.

Oct 31 '10 at 08:05 PM SirVictory

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)

Oct 31 '10 at 10:38 PM Atnas1010
(comments are locked)
10|3000 characters needed characters left

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 :)

more ▼

answered Oct 29 '10 at 02:22 PM

TheDemiurge gravatar image

TheDemiurge
571 2 3 14

But, what about the force part?

Oct 30 '10 at 06:30 AM SirVictory

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 steady speed. It depends how realistic you want it. (or your could set rigidbody's Velocity, etc)

Nov 02 '10 at 05:27 AM TheDemiurge
(comments are locked)
10|3000 characters needed characters left

you could use a rigidbody charcontroller and use .addforce

more ▼

answered Oct 28 '10 at 11:08 PM

Fishman92 gravatar image

Fishman92
2.4k 101 113 128

I'm using a character controller and for some reason it interferes with it.

Oct 29 '10 at 01:39 AM SirVictory
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x462
x292
x69

asked: Oct 28 '10 at 04:18 AM

Seen: 3012 times

Last Updated: Oct 31 '10 at 08:06 PM