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

Help please with Script

Discussion in 'Scripting' started by mister_thuesday, Dec 28, 2014.

  1. mister_thuesday

    mister_thuesday

    Joined:
    Nov 12, 2014
    Posts:
    3
    Why enemy falling? And why not working Flip(0 method?

    Enemy have 2 Colliders(one trigger, one not)

    EnemyBehaviorScript : MonoBehaviour {
    private Animator animator;
    public int cMaxDistance = 10;
    private bool isDead;
    public bool isFacingRight=true;
    public float speed;
    public Transform explosionSpawn;
    public GameObject explosion;
    public Transform player;
    public bool gotcha;

    void Start(){

    Flip ();

    GameObject playerDistance = GameObject.FindWithTag ("Player");
    if (playerDistance != null) {
    player = playerDistance.GetComponent<Transform> ();
    }

    animator = GetComponent<Animator>();
    isDead = false;
    }

    private void FixedUpdate(){
    animator.SetBool ("isDead", isDead);
    if(!isDead)
    rigidbody2D.velocity = new Vector2 (speed, rigidbody2D.velocity.y);
    if(speed > 0 && !isFacingRight)//&&!isDead)
    //отражаем персонажа вправо
    Flip();
    //обратная ситуация. отражаем персонажа влево
    else if (speed < 0 && isFacingRight)//&&!isDead)
    Flip();
    }
    void Update()
    {
    PlayerSearch ();
    if (isDead) {
    if (!(GameObject.Find("headExplosion(Clone)")))
    Instantiate (explosion, explosionSpawn.position, explosionSpawn.rotation);
    }
    }

    void OnTriggerEnter2D(Collider2D col){

    if (col.gameObject.tag == "WeaponTree") {
    isDead = true;

    Destroy (gameObject,1.0f);
    }
    if (col.gameObject.tag == "triggerChangeDirection") {


    speed = - speed;
    }
    }

    private void Flip()
    {

    //меняем направление движения персонажа
    isFacingRight = !isFacingRight;
    //получаем размеры персонажа
    Vector3 theScale = transform.localScale;
    //зеркально отражаем персонажа по оси Х
    theScale.x *= -1;
    //задаем новый размер персонажа, равный старому, но зеркально отраженный
    transform.localScale = theScale;
    }
    void PlayerSearch(){

    float dist = (player.position - transform.position).sqrMagnitude;
    gotcha = dist <= cMaxDistance * cMaxDistance; //квадрат расстояния сравниваем с квадратом заданного предела!
    if(gotcha)
    Debug.Log("F*** YOU!");
    }
    }
     
  2. jaredic

    jaredic

    Joined:
    Jan 4, 2015
    Posts:
    8
    Hello,you schould write this: speed-=speed,that is what y can see...hope y help you
     
  3. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Firstly, can I ask that when posting code, please use the code formatting option in the toolbar when entering post, it just makes it soo much easier to read code :)

    anyhoo, just to get a feel, I take it this moves an ememy, then when colliding it changes direction? is this movement automatic left and right based on direction facing, ie, based on your value of speed?
    would it be easier to alter the speed value in the Flip Function?

    doing this would be the equivalent of saying
    speed = speed - speed, in other words 0, or a negative number.
    the OP i think was right in negating the speed value, or just multiply it by -1 same as the Flip function does for the localX scale. just thinking out loud

    not sure about the falling, is this along a flat horizontal surface?

    the Flip() function, just to get the ball rolling
    im guessing that this is a 2D game, so for the Flip Function, use a Vector2 for theScale
    Code (CSharp):
    1.     void Flip()
    2.     {
    3.         isFacingRight = !isFacingRight;
    4.    
    5.         Vector2 theScale = transform.localScale;
    6.         theScale.x *= -1;
    7.         transform.localScale = theScale;
    8.     }
     
    Last edited: Jan 5, 2015
  4. jaredic

    jaredic

    Joined:
    Jan 4, 2015
    Posts:
    8
    What y wrote,hmmm,shame on me