- Home /
Question by
quieresss · Mar 08, 2021 at 10:05 PM ·
2dpositiontransform.translate
manipulating object's position without RigidBody2D component
I'm using this script to create some sort of neverending background:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class infiniteback : MonoBehaviour
{
public GameObject col;
void start ()
{
}
void OnCollisionEnter2D (Collision2D col)
{
switch (col.gameObject.name)
{
case "GameObject1":
transform.position = new Vector3(transform.position.x, transform.position.y - 51.84f, transform.position.z);
break;
case "GameObject2":
transform.position = new Vector3(transform.position.x, transform.position.y + 51.84f, transform.position.z);
break;
case "GameObject3":
transform.position = new Vector3(transform.position.x - 92.16f, transform.position.y, transform.position.z);
break;
case "GameObject4":
transform.position = new Vector3(transform.position.x + 92.16f, transform.position.y, transform.position.z);
break;
}
}
}
For some reason, this script works only if the background object has an rigidbody. I'm not really sure why is this happening, or how i can work around it. Is there any function that does the same thing and doesn't require rigidbody? Please let me know.
Comment
Your answer
Follow this Question
Related Questions
Convert Input.mousePosition to RectTransform pivot position 2 Answers
Enemy chasing the player ? 2 Answers
Changing player's moving direction 0 Answers
2D Position of a game object. 1 Answer