- Home /
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.
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)
Thanks for pointing me into the right direction dude.I think i understand it.But there is one problem. $$anonymous$$y 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
I have added an answer with some script that may help =]
Answer by AlucardJay · Jul 11, 2012 at 07:54 PM
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
Wow thanks dude, this is exactly what i ment!I never could have done this alone.Thumbs up to this guy =]
By the way.Which number represents the direction?This is a very usefull script which can be used in more things :).
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) );
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?
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 =]
Your answer
Follow this Question
Related Questions
Cloud recognition in Vuforia 0 Answers
is it possible to create script for wind zone? 1 Answer
Animated clothes 1 Answer
Accessing a Variable in another script for (SLIDER) 1 Answer
How can I give an object realistic wind? 0 Answers