Instantiated object start rotation
I am have problem in my endless game in which i am Instantiating traffic from a 2 points and every thing looks good except sometime some Instantiated models come towards me rolling as you can see in these pictures below. These models has been attached with MovingTraffic Script which just add velocity to the Instantiated model and those two points is attached with InstantiatingTraffic script through which these models are Instantiated and some models angles are not in there position therefore i have to assign those rotation angle through script please have a look on these script and models are attached with box collider, mesh collider, rigid body and movingTraffic Script
//MovingTraffic Script
public class MovingTraffic : MonoBehaviour {
private BikeControl bikecontroll;
// Use this for initialization
void Start () {
bikecontroll = GameObject.Find("Motorbike1").GetComponent<BikeControl>();
}
// Update is called once per frame
void Update () {
GetComponent<Rigidbody>().velocity = new Vector3(0, 0, -10);
}
}
//InstantiatingTraffic Script
public class instanitaeingTraffic : MonoBehaviour {
public GameObject car1;
public GameObject car2;
public GameObject car3;
public GameObject car4;
public GameObject car5;
float time;
public GameObject swampPoint1;
public GameObject swampPoint2;
public GameObject moterBike;
public BikeControl bikeController;
public float timeDecider;
// Use this for initialization
void Start () {
// GetComponent<Rigidbody>().freezeRotation = true;
}
// Update is called once per frame
void Update()
{
var distance = Vector3.Distance(this.transform.position, moterBike.transform.position);
// Debug.Log(bikeController.speed);
if ((bikeController.speed >= 1) && (bikeController.speed <= 50) && (distance >= 150))
{
GetComponent<Rigidbody>().velocity = new Vector3(0, 0, 10);
}
else if ((bikeController.speed > 50) && (bikeController.speed <= 100) && (distance >= 150))
{
GetComponent<Rigidbody>().velocity = new Vector3(0, 0, 50);
}
else if ((bikeController.speed >= 100) && (distance >= 150))
{
GetComponent<Rigidbody>().velocity = new Vector3(0, 0, 75);
}
else
{
GetComponent<Rigidbody>().velocity = Vector3.zero;
}
time += 0.01f;
if (bikeController.speed >= 50)
{
timeDecider = 1;
}
else if (bikeController.speed >= 100)
{
timeDecider = 0.8f;
}
else if(bikeController.speed >= 150)
{
timeDecider = 0.5f;
}else
{
timeDecider = 2;
}
// Debug.Log(time);
if ((time >= timeDecider))
{
int rendomCar = Random.Range(1, 6);
int rendompoint = Random.Range(1, 3);
if ((rendompoint == 1))
{
time = 0;
if (rendomCar == 1)
{
Instantiate(car1, swampPoint1.transform.position, transform.rotation);
}
else if (rendomCar == 2)
{
Instantiate(car2, swampPoint1.transform.position, transform.rotation);
}
else if (rendomCar == 3)
{
Instantiate(car3, swampPoint1.transform.position, Quaternion.Euler(0, 180, 0));
}
else if (rendomCar == 4)
{
Instantiate(car4, swampPoint1.transform.position, Quaternion.Euler(-90, 90, -270));
}
else if (rendomCar == 5)
{
Instantiate(car5, swampPoint1.transform.position, transform.rotation);
}
}
else if ((rendompoint == 2))
{
time = 0;
if (rendomCar == 1)
{
Instantiate(car1, swampPoint2.transform.position, transform.rotation);
}
else if (rendomCar == 2)
{
Instantiate(car2, swampPoint2.transform.position, transform.rotation);
}
else if (rendomCar == 3)
{
Instantiate(car3, swampPoint2.transform.position, Quaternion.Euler(0, 180, 0));
}
else if (rendomCar == 4)
{
Instantiate(car4, swampPoint2.transform.position, Quaternion.Euler(-90, 90, -270));
}
else if (rendomCar == 5)
{
Instantiate(car5, swampPoint2.transform.position, transform.rotation);
}
}
}
}
}
this is what i am face please help me out on this problem and thanks in advance
Your answer
Follow this Question
Related Questions
ForceMode.VelocityChange not behaving as expected 1 Answer
Speed limit to AddForce (without limiting velocity of rigidbody) 0 Answers
2D Rigidbody Velocity Being Weird 1 Answer
my 2d character isnt jumping with constant velocity on applying force 1 Answer
some Instantiated models are start rolling after getting Instantiated. 0 Answers