Total Noob

I’m having somewhat of a crisis, I have an enemy in my game that I just need to move along the x-axis until it hits a box collider and then waits before it respawns again. All my code keeps breaking can anyone help me? I think I’m getting tripped up on the RigidBody2D.

we can help (myb a other member) if you share your code
and share error :wink:

1 Like
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FlyBy : MonoBehaviour
{

    public float MoveSpeed = 1f;

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

        GetComponent<Rigidbody2D>(velocity) = new Vector2(MoveSpeed, Rigidbody2D.velocity.y);

    }
}

thats just the first part since I reworked it.

also here’s my code for the infinite background… i got it from a youtube video and its giving me troble too. bonus point for whoever can help.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Scroll : MonoBehaviour {

    float scrollspeed = 1f;
    Vector2 startPos;

    // Use this for initialization
    void Start () {

        startPos = transform.position;
    }
   
    // Update is called once per frame
    void Update () {

        float newPos = Mathf.Repeat(Time.time * scrollspeed, 20);
        transform.position = startPos + Vector2.right * newPos;

    }
}

Also thanks PapsQ I couldn’t figure out the code you gave me before but i really do appreciate the help.

it work for me,

the script is in your GameObject / component?

No I attached it directly to the sprite. Also it’s just giving me a few errors. So it wont compile.

show me the error pls.

3

An object reference is required to access non static member ‘UnityEngine.Rigidbody2D.velocity’

The name ‘velocity’ does not exist in the current context

The non-genaric method ‘UnityEngine.Component.GetComponent(System.Type)’ cannot be used with the type arguments

(Using Visual studio 2017)

The ‘scroll’ script is working now. Just need the enemies to move now.

i do a video for you and my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class enemiesController : MonoBehaviour {
    public float speed; //speed you set later
    public Vector3 startP; // save the start position for reset
    public static GameObject restePoint;  //get the point for reset after that point we do the reset
    public static Rigidbody2D RB;  // get the rigidebody2D for addForce
    // Use this for initialization
    void Start () {
        speed = 2; //set speed
        startP = transform.position; //save start position
        RB = GetComponent<Rigidbody2D>(); //get rb2D
        restePoint = GameObject.Find("resetPoint"); //get the reset Point
    }   
    // Update is called once per frame
    void Update () {
        RB.AddForce(transform.right * speed); //add force for move your Object
        if (transform.position.x >= restePoint.transform.position.x)  //if Object.X is bigger than ResetPoint.X
        {
            Instantiate(gameObject, startP, transform.rotation); // we create a new one like that Instanciate(TheGameObject for instanciate,  the postion for, the rotation for)
            //so we create a new enemie with the Start pos
            Destroy(gameObject); // and we destroy the first gameObject
        }
    }
}

my recorder bug so i you cant see the dropMenu in Unity.

  1. resetPoint is a empty GameObject with nothing component add
  2. the script is add on the Enemie GameObject
1 Like

Thank you for legit holding my hand and guiding me through this you’re a life saver.

100 times thank you PapsQ

Np :slight_smile:
Good luck now and have fun ^^