0
\$\begingroup\$

I've spent about half a day trying to get my head round this and I'm not having too much luck, essentially what I want to do is play an animation and then once the animation has finished create a flat disk shaped object in front of the current player, the code I have is as follows, please see my comments below for a list of problems:

Note this is cut down code to make the example easy.

static int idleState = Animator.StringToHash("Base Layer.idle");
static int portalState = Animator.StringToHash("Base Layer.Portal");

private RenderTexture portal_texture = Resources.Load("Portal") as RenderTexture;

void Start () {
    portal_texture = Resources.Load("Portal") as RenderTexture;
    anim = GetComponent<Animator>();
    if(anim.layerCount ==2){
        anim.SetLayerWeight(1, 1);
    }
}

void Update () {

    if(Input.GetKey("p")){
        anim.SetBool("cast_portal", true);
    }

    if(Input.GetKeyUp("p")){
        sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
        sphere.transform.position = transform.TransformPoint(Vector3.zero);
        sphere.transform.localScale = new Vector3(3,3,1);
        Vector3 temp = sphere.transform.localScale;
        temp.x = (sphere.transform.localScale.x + 12.0f) * 0.5f;
        sphere.transform.localScale = temp;   
        sphere.renderer.material.mainTexture = portal_texture;    
    }
}

Problems

  1. The render texture doesn't appear on the object

  2. I need to have the object appear about 10 meters infront of where the character is facing, note I'm using a third person controller so camera position won't cut it.

  3. How come in the inspector I can set any of the scaling options to 0.001 but if I try it in vector3 it throws an exception when compiling?

  4. Where do I need to put this code so it only adds the sphere once the animation has finished?

I only started with unity this week so I'm probably missing something very obvious, I'm just having a bit of a brain freeze so any help would be greatly appreciated.

Thanks

\$\endgroup\$
2
\$\begingroup\$

You can utilize prefabs and public fields on a MonoBehaviour to make this a lot easier:

public class YourObject : MonoBehaviour {

    // The prefab we want to instantiate when we press p.
    // This needs to be set in the inspector; you'll see a field pop up in
    // the inspector where you can select the prefab to instantiate.
    // Always be sure to make this public so the inspector can see it.
    public GameObject myPrefab;

    void Start() {
        // Animation stuff
    }

    void Update() {

        if (Input.GetKey("p")) {
            // Play the animation
        }

        if (Input.GetKeyUp("p")) {
            // Here we just instantiate our prefab.
            // We can use transform.forward to get the direction our character is facing.
            // In this case, we mulitply it by 10 to create the prefab 10 units away.
            GameObject.Instantiate(myPrefab, transform.forward * 10, Quaternion.identity);
        }
    }
}

This makes your code much simpler and you can modify the prefab however you want.

I sadly do not know a whole lot about animation in Unity, so I cannot answer that part of your question. However, something tells me you might want to try using Animation.Play() instead of setting some kind of value to play an animation :)

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Thanks for your help matey for extra love How would you go about tranforming forward and up? \$\endgroup\$ – Mrk Fldig Jan 11 '14 at 10:34
  • \$\begingroup\$ @MrkFldig What exactly do you mean? Spawning the object above you instead of in front (use transform.up)? Or moving your character or the spawned prefab? \$\endgroup\$ – Lokkij Jan 12 '14 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.