x


Is this possible?

Well,Is there some sort of an easy way in Unity how to make a zone where from time to time will a strong wind appear and will turn objects with rigidbody down?Like when you walk into a strong water flow.

more ▼

asked Jul 11 '12 at 01:25 PM

DOGY149 gravatar image

DOGY149
5 2 4 4

this is a very complex script but maybe look at the windzone effect... (I don't understand it but it's mybe a tip for u)

Jul 11 '12 at 01:51 PM bubblegumsoldier

Thanks for pointing me into the right direction dude.I think i understand it.But there is one problem. My char doesnt have a character controler.Its a simple sphere with some movement,jump scripts.Can i change the var to get component rigidbody?Because how will it recognize my ball?Thx again

Jul 12 '12 at 09:22 AM DOGY149

I have added an answer with some script that may help =]

Jul 13 '12 at 06:29 AM alucardj
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Yes it is possible. Maybe do a SphereCast in the direction of the flowing water, for any colliders in that cast that have a rigidbody, add force. I like this idea better than an invisible collider and OnTriggerEnter, but those are 2 methods.

http://docs.unity3d.com/Documentation/ScriptReference/Physics.SphereCast.html

http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnTriggerEnter.html

EDIT : I have made a script from the following Unity Script References :

http://docs.unity3d.com/Documentation/ScriptReference/Physics.RaycastAll.html

http://docs.unity3d.com/Documentation/ScriptReference/Physics.SphereCastAll.html

castDirection is a world-space Vector3; meaning if you put (-1,0,0), then the direction is pointing negatively along the X-axis; put (0,1,0) and the direction running positively along the Y-axis(pointing up). Try Different values for x,y and z, just keep them between -1 and 1.

I have now included a linedraw-er in the script, you can see what is affected by changing these values. You shouldn't include this in your build, it's just handy for showing where the spherecast is in the editor while designing and building.

castRadius, castDistance and amountOfForce should be self-explanatory =]

EDIT 2 : this script has been edited to include a time for the flow to be active and then time for the flow to be inactive. Set the flowTime (currently 10seconds on, 10seconds off) :

#pragma strict

private var castDirection : Vector3;
private var castRadius : float;
private var castDistance : float;
private var amountOfForce : int;
private var flowCounter : float = 0.0;
private var isFlowing : boolean = true;
private var flowTime : float = 10.0;

function Start()
{
    // build direction vector * Note : this is in World-Space
    castDirection = Vector3( -1, 0, 0 );

    // Radius of SphereCast
    castRadius = 3.0;

    // Distance of SphereCast
    castDistance = 9.5;

    // How much force to push on the rigidbody's
    amountOfForce = 100;
}

function Update() 
{
    flowCounter += Time.deltaTime;

    if (flowCounter < flowTime)
    {
        isFlowing = true;
    }

    if (flowCounter > flowTime)
    {
        isFlowing = false;
    }

    if (flowCounter > (flowTime * 2.0))
    {
        isFlowing = true;
        flowCounter = 0.0;
    }
}

function FixedUpdate() 
{
    if (isFlowing)
    {
        // Show SphereCast in editor (this is optional)
        ShowSphereCast();

        // create an array to store all rayhits
        var hits : RaycastHit[];

        // Physics.SphereCastAll( position : Vector3, radius : float, direction : Vector3, distance : float );
        hits = Physics.SphereCastAll( transform.position, castRadius, castDirection, castDistance );

        for (var i = 0;i < hits.Length; i++) {

            var hit : RaycastHit = hits[i];

            Debug.Log("hit " + hit.collider.transform.gameObject.name);

            if ( hit.collider.transform.gameObject.rigidbody != null )
            {
                hit.collider.transform.gameObject.rigidbody.AddForce( castDirection * amountOfForce * Time.deltaTime );
            }
        }
    }
}

function ShowSphereCast() // (this is optional)
{
    Debug.DrawRay( transform.position, castDirection * castDistance, Color.cyan );
    var origin : Vector3 = transform.position;
    var segments : int = 50;
    var calcAngle : float = 0;
    var posX : float[] = new float[segments + 1];
    var posY : float[] = new float[segments + 1];
    // Calculate Arc
    for (var i:int = 0; i < segments + 1; i ++)
    {
        posX[i] = ( Mathf.Sin( calcAngle * Mathf.Deg2Rad ) * castRadius ); //  + ( i * curvage );
        posY[i] = Mathf.Cos( calcAngle * Mathf.Deg2Rad ) * castRadius;
        calcAngle += 360.0 / parseFloat(segments);
        // Show outside of SphereCast
        Debug.DrawRay( Vector3( 0, posY[i], 0 - posX[i] ) + origin, castDirection * castDistance, Color.red );
        // Show origin of SphereCast
        Debug.DrawLine( Vector3( 0, posY[i], 0 - posX[i] ) + origin, origin, Color.red );
    }
}

I have a web build of an example to show it works. The above script is on an empty gameObject, just under the white pipe =]

wasd or arrows to move green sphere : rigidbodystream webbuild

more ▼

answered Jul 11 '12 at 07:54 PM

alucardj gravatar image

alucardj
13.6k 34 55 86

Wow thanks dude, this is exactly what i ment!I never could have done this alone.Thumbs up to this guy =]

Jul 13 '12 at 06:39 AM DOGY149

By the way.Which number represents the direction?This is a very usefull script which can be used in more things :).

Jul 13 '12 at 10:56 AM DOGY149

When you SphereCast, the direction is the 3rd number Vector3

// Physics.SphereCastAll( position : Vector3, radius : float, direction : Vector3, distance : float );

AddForce is also given as a Vector3

// ....rigidbody.AddForce( Vector3( -100 * Time.deltaTime, 0, 0) );
Jul 13 '12 at 11:30 AM alucardj

So change the number of the Vector right?In my movement script ive got left and right on Vector3. So i suppose Vector2 is for up and down?

Jul 13 '12 at 04:15 PM DOGY149

I have re-written my original answer. It is much more detailed, and exposes some variables for you to see what is happening. Also, I am quite proud of the Debug LineCaster to show where the SphereCast is affecting the world, only seen in the editor view. Check out my edited answer =]

Jul 13 '12 at 07:03 PM alucardj
(comments are locked)
10|3000 characters needed characters left

Everything is possible, you just need to know how :)

more ▼

answered Jul 11 '12 at 08:48 PM

kalekip1 gravatar image

kalekip1
1 1 1 2

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

x839
x66

asked: Jul 11 '12 at 01:25 PM

Seen: 561 times

Last Updated: Jul 31 '12 at 08:57 AM