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

[SOLVED] Trying to create a "blast" (like Kamehameha)

Discussion in 'Scripting' started by Deleted User, Dec 7, 2014.

  1. Deleted User

    Deleted User

    Guest

    The problem is that the object extends behind the player as well as in front of the player when it should only extends in front.
    (Note that in the code I used a coroutine but that will change.)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. namespace Projectiles
    5. {
    6.         public class ElementBlast : Projectile
    7.         {
    8.                 public float rotationSpeed;  
    9.  
    10.                 protected override void Start ()
    11.                 {
    12.                         StartCoroutine ("InGrowth");
    13.                 }
    14.  
    15.  
    16.                 IEnumerator InGrowth ()
    17.                 {
    18.                         while (transform.localScale.x < 7) {
    19.                                 var g = GetComponent<CapsuleCollider> ().bounds.extents.x;
    20.                                 Debug.Log (g);
    21.                                 Debug.Log (transform.position);
    22.                                 transform.localScale += new Vector3 (.25f, 0, 0);
    23.                                 transform.position += new Vector3 (.25f * transform.parent.transform.parent.forward.x, 0, 0);
    24.                                 yield return new WaitForSeconds (.05f);
    25.                         }
    26.                 }
    27.         }
     
  2. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297

    What are you using for the "kamehame"? Is it a sprite or is it something else?
     
  3. Deleted User

    Deleted User

    Guest

    I'm just using a Unity Cylinder for testing.
     
  4. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    682
    Well what you have to remember is that when you scale something, it scales from the origin point. In the case of a Unity capsule, the origin is the center. So it's going to scale both ways.
     
  5. Deleted User

    Deleted User

    Guest

    Yea I know. I managed to fix it. It was a combination of badly set up Game Objects (strange rotations, etc.) and bad code.

    I fixed it by replacing to those lines in the while loop:

    Code (CSharp):
    1.                                         float step = growthSpeed * Time.deltaTime;
    2.                                         transform.localScale += new Vector3 (step, 0, 0);
    3.                                         transform.localPosition += new Vector3 (step / 2, 0, 0);
     
  6. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Better to not have a collider on it... recalculating the collider bounds every time it updates is expensive