- Home /
prevent object from falling to the left side when instantiated (spawned) ?
heres the problem , when i instantiate a prefab it falls to the left how can i fix this ? heres the code : public bool IsSelected = false; public GameObject RIGHT_SHU; public GameObject LEFT_SHU; public GameObject SplitShu;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);
RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero);
if (hit.collider != null)
{
Debug.Log(hit.collider.gameObject.name);
IsSelected = true;
}
else if (Input.GetMouseButtonDown(0) && IsSelected == true)
{
Debug.Log("Split -------------------------------------");
IsSelected = false;
SplitShu.SetActive(false);
Instantiate(RIGHT_SHU, transform.position, transform.rotation);
Instantiate(LEFT_SHU, transform.position, transform.rotation);
}
}
Answer by endasil_unity · Nov 11, 2020 at 09:58 AM
If you have a rigid body and don't want it to rotate on its own, freezing the rotation on an axis is the way to go. Just click in Freeze rotation on the rigid body on the axis you do not want it to rotate on, for example, the Y-axis like the picture below. You can also increase the "Angular Drag" on objects to make it harder for them to rotate from physics. You can still do a transform.rotation to manually rotate the object.
Your answer
Follow this Question
Related Questions
How to Make an Expanding Spawn Area for Prefabs? 2 Answers
How to spawn a new prefab when the last one was destroyed?? 2 Answers
How do you spawn multiple prefabs within a certain distance from each other from one spawn point? 2 Answers
SOLVED : Adding and retrieving instantiated objects from Array 1 Answer