• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Tariq-mehmood · Aug 10, 2017 at 02:55 AM · instantiaterigidbody physics

some Instantiated models start rolling after getting Instantiated.

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

alt text

alt text

sss.png (148.9 kB)
sadasdsa.png (354.4 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image spooneystone · Aug 10, 2017 at 03:48 AM 0
Share

First thing I would do is sort out those car with the different rotation. Go into your 3d modelling program of choice and import the models and reset the rotations. Or you could give each of the cars a empty gameobject parent and adjust the cars rotation how you need it and make sure all of the rotations are at 0 on the parent. Then when you instantiate the car don't call "transform.rotation" but call "Quadternion.identity" ins$$anonymous$$d. Give that a go first.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

82 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Checking if object intersects? 1 Answer

OnTriggerEnter() not working 1 Answer

Loading Prefab from Script 1 Answer

can't parent an Object, a second time. 0 Answers

Setting the variable on a GameObject to be instantiated (not yet existent) cs and js 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges