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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to Move an enemy left? My script broke...

Discussion in '2D' started by foxvalleysoccer, Aug 23, 2015.

  1. foxvalleysoccer

    foxvalleysoccer

    Joined:
    May 11, 2015
    Posts:
    108
    The script to move my enemy "left" worked yesterday on my work PC.
    Opening to today on laptop and its spits out compiler errors?

    //GetComponent<Rigidbody2D> ().velocity = Vector2.left;
    Is vector2.left not supported in certain versions of unity?

    How can I modify this script below to move my enemy left right now it just moves right?
    GetComponent<Rigidbody2D>().velocity = new Vector2 (velocity, GetComponent<Rigidbody2D>().velocity.y);

    Is there a better way to move an enemy across the screen towards left? In fixed update?

    Thanks
     
  2. PatrickDuncan

    PatrickDuncan

    Joined:
    Jul 9, 2015
    Posts:
    11
    negative velocity?
    GetComponent<Rigidbody2D>().velocity = new Vector2 (-velocity, GetComponent<Rigidbody2D>().velocity.y)

    What error do you get when trying to do Vector2.left?
     
  3. foxvalleysoccer

    foxvalleysoccer

    Joined:
    May 11, 2015
    Posts:
    108
    - velocity did not work
    Am i missing a using statement for vector2 left?

    here is the error message i get.
    Assets/scripts/MoveEnemy.cs(14,65): error CS0117: `UnityEngine.Vector2' does not contain a definition for `left'
     
  4. foxvalleysoccer

    foxvalleysoccer

    Joined:
    May 11, 2015
    Posts:
    108
    Problem solved

    GetComponent<Rigidbody2D> ().velocity = -Vector2.right;



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MoveEnemy : MonoBehaviour {
    5.     public GameObject Player;
    6.     public float velocity = -1.0f;
    7.     void Start () {
    8.  
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void FixedUpdate () {
    13.  
    14.         GetComponent<Rigidbody2D> ().velocity = -Vector2.right;
    15.    
    16.     }
    17.     void OnTriggerEnter(Collider other){
    18.         if(other.gameObject.CompareTag("Player")){
    19.             Destroy (Player);
    20.             Debug.Log ("Hit");
    21.  
    22.         }
    23.  
    24.     }
    25. }