Hello everyone,
I’m making a game where we have to throw tubes on the drowning people in the water. To increase the difficulty, I want to add wind. I created an arrow in Blender as an indicator of wind.
Now, I want this arrow to look in any direction after I load the game (I want the direction to be random). And, I want the tube to go in the direction which the arrow faces. I used AddForce but it is not behaving as it should.
Any help would be appreciated.
Cheers.
Ok so first on the wind object create a C# script, I have called this WindDirection.
using UnityEngine;
using System.Collections;
public class WindDirection : MonoBehaviour {
void Start () {
transform.rotation = Random.rotation;
}
}
This will set a random rotation.
On the tube object create a C# script. In this case I have called it force.
using UnityEngine;
using System.Collections;
public class Force : MonoBehaviour {
public GameObject windObject;
Rigidbody rigidbody;
void Start () {
rigidbody = gameObject.GetComponent<Rigidbody> ();
}
void FixedUpdate () {
rigidbody.AddForce (windObject.transform.forward * 10);
}
}
Now you will need to set the wind object for this script in the inspector. You can also use GameObject.Find() if you need to do it by script instead.
One thing that might be a problem is that the direction will be completely random; you may not wind to be going upwards or downwards for example.
so we could instead set it to a random rotation on the Y axis. If that’s what you want to do modify the WindDirection script to:
using UnityEngine;
using System.Collections;
public class WindDirection : MonoBehaviour {
void Start () {
var tempRotation = Quaternion.identity;
var tempVector = Vector3.zero;
tempVector = tempRotation.eulerAngles;
tempVector.y =Random.Range (0, 359);
tempRotation.eulerAngles = tempVector;
transform.rotation = tempRotation;
}
}
Hope this is of some help, let me know if you need any more detail 
Make sure to check out the script reference if you need to check how to use anything Unity - Scripting API: