Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Instanciate prefab with updated rotation

Discussion in 'Scripting' started by hdaMonkey, Jun 13, 2014.

  1. hdaMonkey

    hdaMonkey

    Joined:
    Jul 4, 2013
    Posts:
    14
    Hey there,

    I have a problem with the rotation of my instanciated prefabs:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FootprintR : MonoBehaviour {
    5.  
    6.     public GameObject footprint;
    7.     public Transform currentPos;
    8.  
    9.     private float nextActionTime = 0.8f;
    10.     public float period = 0.85f;
    11.  
    12.     void Update () {
    13.         if (Time.time > nextActionTime ) {
    14.             nextActionTime += period;
    15.             Footprint ();
    16.             }
    17.         }
    18.  
    19.         void Footprint () {          
    20.             Vector3 position = new Vector3 (currentPos.position.x, currentPos.position.y, currentPos.position.z);
    21.             Quaternion rotation = Quaternion.Euler (currentPos.rotation.x, currentPos.rotation.y, currentPos.rotation.z);
    22.             Instantiate (footprint, position, rotation);
    23.         }
    24. }
    To test it, I use a GameObject that's parented with the Player. The Footprints are instanciated in time, and basicly do what I want. The only problem is, the currentPos.rotation.x / y / z isnt effecting the footprints. What am I doning wrong?

    Cheers!
     
  2. earrgames

    earrgames

    Joined:
    Jan 27, 2014
    Posts:
    168
    When you say "parented with the Player." you are refering that the object is the parent or the the player is the parent? I'm almost sure the problem is about local space.
     
  3. hdaMonkey

    hdaMonkey

    Joined:
    Jul 4, 2013
    Posts:
    14
    Yes, the object that instanciates is a chil of the player. But thats just for testing, so i don't need a moving Object.
     
  4. earrgames

    earrgames

    Joined:
    Jan 27, 2014
    Posts:
    168
    Oh, i see the problem, you declared the variables but never asigned them, i mean

    publicTransform currentPos=transform;

    if you did not do this, you are refering to an empty transform.
    Try it, I'm almost sure it is the problem.
     
  5. earrgames

    earrgames

    Joined:
    Jan 27, 2014
    Posts:
    168
    You can also use LateUpdate() and modify the rotation manually refering to the player. It's an easy way.

    footprint = Instantiate(footprint, position, rotation);

    function LateUpdate(){
    footprint.transform.rotation.y=currentPos.rotation.y;

    }
     
  6. hdaMonkey

    hdaMonkey

    Joined:
    Jul 4, 2013
    Posts:
    14
    The Transform of the ''position" works. The footsteps appear where they should, but they always face the same direction, even though the object thats linked to the variable currentPos is rotated. /edit And the Quaternion rotation is called
     
  7. earrgames

    earrgames

    Joined:
    Jan 27, 2014
    Posts:
    168
    Oh, a recommendation when working with timed events, try to avoid using Update Whenever posible, it has many incovenients.
    1-performance: making unnecessary calls will decrease your fps
    2-it's unstable: it depends always on deltaTime.

    I recommend using coroutines or animations, with a coroutine you can jump from this:
    Code (CSharp):
    1. if (Time.time > nextActionTime ) {
    2.             nextActionTime += period;
    3.             Footprint ();
    4.             }
    5.         }
    to this:

    Code (JavaScript):
    1. while("your condition here"){
    2.    yield WaitForSeconds(Any);
    3.    Footprint();
    4. }