Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
6
Question by SrBilyon · Oct 28, 2010 at 04:18 AM · gravityforcewind

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;
 }

}

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Atnas1010 · Oct 28, 2010 at 04:48 AM 1
Share

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

avatar image SrBilyon · Oct 28, 2010 at 05:42 AM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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?

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SrBilyon · Oct 28, 2010 at 07:50 PM 0
Share

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

avatar image SrBilyon · Oct 30, 2010 at 06:32 AM 0
Share

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

avatar image Atnas1010 · Oct 31, 2010 at 05:54 PM 0
Share

Edited my post again

avatar image SrBilyon · Oct 31, 2010 at 08:05 PM 0
Share

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.

avatar image Atnas1010 · Oct 31, 2010 at 10:38 PM 0
Share

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)

avatar image
1

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SrBilyon · Oct 30, 2010 at 06:30 AM 0
Share

But, what about the force part?

avatar image TheDemiurge · Nov 02, 2010 at 05:27 AM 0
Share

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)

avatar image
0

Answer by HolBol · Oct 28, 2010 at 11:08 PM

you could use a rigidbody charcontroller and use .addforce

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SrBilyon · Oct 29, 2010 at 01:39 AM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Custom Gravity issues 1 Answer

adding force every frame to control my ridigbody makes it shake ugly 4 Answers

How Do I Increase Gravity Overtime If My Player Stays In The Air Longer? 1 Answer

Good old trig and NaN 1 Answer

Applying colliders on Bike. 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges