- Home /
Instantiated clones arent behaving the same as the prefab
(video of problem: link text)
I have a prefab that is tagged rotatewithPlayer, when I instantiate the prefab, they are positioned randomly, but are at set heights, and face the camera. Here is my code snippet.
for (i = 0; i < maxTrees; i++){ randX = Random.Range(minX,maxX); randZ = Random.Range(minZ,maxZ); Instantiate(tree1, new Vector3(randX,12.2f,randZ), transform.rotation);
and in my player controller script, i iterate through an array of all the objects that are tagged rotatewithPlayer and turn them 90 degrees accordingly. And the code snippet...
Start () {
controller = GetComponent<CharacterController>();
rotate_WithPlayer = GameObject.FindGameObjectsWithTag("rotateWithPlayer");
}//end start
void Update () {
//some code
if (Input.GetKeyDown("q")){
//rotate tagged objects 90deg left
foreach (GameObject x in rotate_WithPlayer){
x.transform.Rotate(0,-90,0,Space.World);
}
}if (Input.GetKeyDown("e")){
//rotate tagged objects 90deg right
foreach (GameObject x in rotate_WithPlayer){
x.transform.Rotate(0,90,0,Space.World);
` All of the tagged objects, when placed in the scene manually will rotate with the player. but the ones instantiated by the above script do not rotate at all.
The game im making is 2d in a 3d world, and its constrained to 90degree turns. every object is a texture on a plane. when the player turns right, all of the tagged objects should rotate 90 degrees in place so that they are all still visible to the player(camera). The script seems to work properly on the manually placed prefabs, but not the instantiated ones. Also, I've found that if I copy/paste my movement script into a new script file and use that on my the player, that the instantiated object will then rotate with the player as intended, but when unity regains focus (after i switch to another window) the instantiated objects dont rotate. im not sure why it seemingly works, then doesnt. any help or insight would be greatly appreciated. ps. im a newbie so, if i havent provided enough info please excuse me, and if its something really simple, then i appologize
Answer by Seizure · Aug 01, 2013 at 08:44 PM
Its very difficult to determine your issue from the given, what I gather is that they instantiate at an odd rotation but then continue on with the rotation script as intended? If that is the case I think its because of your
Instantiate(tree1, new Vector3(randX,12.2f,randZ), transform.rotation);
Try this:
Instantiate(tree1, new Vector3(randX,12.2f,randZ), Quaternion.Identity);
The quaternion.identity will apply the prefabs rotation whereas transform.rotation was using the rotation of whatever gameobject the script is attached to.
I have tried and changing the code doesn't do what I need. When i use Quaternion.identity, all of the object spawn parallel to the ground, while the manually placed prefabs in the scene are standing horizontally.
Seizure has given most of the answer. Just add a Quaternion variable, adjust the rotation from the Identity and then apply the variable.
Vector3 myVector = // The direction I want to face
Quaternion myRotation = Quaternion.Identity;
myRotation.LookRotation(myVector);
Then use:
Instantiate(tree1, new Vector3(randX,12.2f,randZ), myRotation);
trying this doesn't work for me either. the objects are instantiated, but do not rotate with the player like prefabs i have manually in the scene. i made a brief video showing the problem. please excuse my poor video editing skills.link text
here is my spawner code: using UnityEngine; using System.Collections; public class spawner : $$anonymous$$onoBehaviour { public GameObject tree1; public GameObject tree2; public GameObject bush; public GameObject shrub; public GameObject rock1; public GameObject rock2; public GameObject stump; public GameObject grass; static float randX; static float randZ; static int $$anonymous$$X = -94; static int $$anonymous$$Z = -95; static int maxX = 54; static int maxZ = 52; static int i; static int maxTrees = 300; static int maxBushes = 100; static int maxShrubs = 100; static int maxRocks = 75; static int maxStumps = maxTrees/6; static int maxGrass = 600; // Use this for initialization void Start () { //trees for (i = 0; i < maxTrees; i++){ //tree1 randX = Random.Range($$anonymous$$X,maxX); randZ = Random.Range($$anonymous$$Z,maxZ); Instantiate(tree1, new Vector3(randX,12.2f,randZ), transform.rotation); //tree2 randX = Random.Range($$anonymous$$X,maxX); randZ = Random.Range($$anonymous$$Z,maxZ); Instantiate(tree2, new Vector3(randX,14.6f,randZ), transform.rotation); }//end trees //etc for each object }//end start // Update is called once per frame void Update () { }//end update }//end class
.
and here is the script i have attached to my player: using UnityEngine; using System.Collections;
[RequireComponent(typeof(CharacterController))]
public class ball$$anonymous$$ove : $$anonymous$$onoBehaviour {
static Vector3 move;
float speed = 10f;
CharacterController controller;
GameObject x;
GameObject[] rotate_WithPlayer;
void Start () {
controller = GetComponent<CharacterController>();
rotate_WithPlayer = GameObject.FindGameObjectsWithTag("rotateWithPlayer");
}//end start
// Update is called once per frame
void Update () {
move = new Vector3(Input.GetAxis ("Horizontal"), 0f, Input.GetAxis ("Vertical"));
move = transform.TransformDirection(move);
controller.Simple$$anonymous$$ove(move * speed * Time.deltaTime * 10);
if (Input.Get$$anonymous$$ey("space")){
speed = 20f;
}else{
speed = 10f;
}if (Input.Get$$anonymous$$eyDown("q")){
//rotate tagged objects 90deg left
foreach (GameObject x in rotate_WithPlayer){
x.transform.Rotate(0,-90,0,Space.World);
}
}if (Input.Get$$anonymous$$eyDown("e")){
//rotate tagged objects 90deg right
foreach (GameObject x in rotate_WithPlayer){
x.transform.Rotate(0,90,0,Space.World);
}
}
}//update
}//class