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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

how to move your enemy?

Discussion in '2D' started by hadou357, Nov 28, 2018.

  1. hadou357

    hadou357

    Joined:
    Aug 28, 2018
    Posts:
    22
    So I have a script where my enemy moves up and down. then I wrote a script for it to move to the left. I would like my enemy to move up and down as it moves to the left however if I use the script where he moves up and down he won't move to left and if I use the script where my enemy moves to the left he won't move up and down I was hoping is someone can point me in the right direction to fix this dilemma.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Pole : MonoBehaviour
    6. {
    7.       public float speed;
    8.     Vector3 _startingPos;
    9.     Transform _trans;
    10.     void Start()
    11.     {
    12.         _trans = GetComponent<Transform>();
    13.         _startingPos = _trans.position;
    14.     }
    15.     void Update()
    16.     {
    17.    
    18.     _trans.position = new Vector3(_startingPos.x, _startingPos.y + Mathf.PingPong(Time.time, 3), _startingPos.z);
    19.  
    20.         transform.Translate(Vector2.left * speed * Time.deltaTime);
    21.  
    22.     }
    23.  
    24. }
    25.  
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    you can let the x-position stay the same here and don't change it twice.
    _trans.position = new Vector3(_trans.position.x, _startingPos.y + Mathf.PingPong(Time.time, 3), _startingPos.z);
     
    hadou357 likes this.
  3. hadou357

    hadou357

    Joined:
    Aug 28, 2018
    Posts:
    22
    You sir are a master Among Masters once again you have helped me thank you.