- Home /
Need help with my script.
Hello everyone hope everything's fine. :) this is my script using UnityEngine; using System.Collections;
public class Rigidbodyforce : MonoBehaviour {
// Use this for initialization
void Start () {
}
void FixedUpdate()
{
Ray ray;
RaycastHit hit;
if (Input.GetMouseButton(0))
{
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit))
{
if (hit.collider.tag == "TouchBall")
{
rigidbody.AddRelativeForce (0, 200, 0);
}
}
}
}
}
now the problem is i want the sphere to go in random direction except the down direction it should go up in random direction. please tell me something what should i do. Thanks in advance. :)
Answer by Invertex · Feb 16, 2014 at 02:22 PM
Just use a random number generator in your function.
http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html
rigidbody.AddForce(Random.Range(-500f,500f),Random.Range(0,500f),Random.Range(-500f,500f);
Answer by gwubgwub · Feb 16, 2014 at 02:51 PM
rigidbody.AddRelativeForce (0,Random.Range(100,300), 0);
I haven't tested this, but it should work
200 now is going to be between 100 and 300 a number will be generated.
Your answer
![](https://koobas.hobune.stream/wayback/20220613133713im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
A node in a childnode? 1 Answer
Targetting error please help 0 Answers
please i beg of you help!!!!!!!!!!!!!!! 2 Answers
Having Problem with mouseposition. 2 Answers