Question by
mafiarehan · Feb 24, 2019 at 02:25 PM ·
movement scriptobstacleobstacle avoidance
obstacles movement please HELP!!
This is my first ever game I am making so maybe my question is going to be silly. i have like 50 obstacle objects in the scene and i want them to share a script which moves them from left to right.
//here is my code for movements
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class sidemove : MonoBehaviour
{ public float sideForce;
public Rigidbody rbo;
public Transform tr;
// Use this for initialization
void Start ()
{
rbo.AddForce(sideForce * Time.deltaTime,0,0);
}
// Update is called once per frame
void Update () {
if(rbo.position.x==-10)
{
rbo.AddForce(-sideForce * Time.deltaTime, 0, 0);
}
if (rbo.position.x == 10)
{
rbo.AddForce(sideForce * Time.deltaTime, 0, 0);
}
}
}
Note: all obstacles are placed at different x,y,z position throughout the ground and they share the same script.
Pls help me with this . Thanks in advance :)
Comment
Answer by tecnosinis · Jun 15, 2020 at 09:38 PM
Global variables
float lado= 10f;//Move Side
bool estado=false; //Status
float limite = 6f; //Limit
void Start()
estado = true;
void FixedUpdate()
if(transform.position.x > limite)
{
estado = false;
}
if(transform.position.x < -limite)
{
estado = true;
}
if (estado == true)
{
transform.Translate(lado*Time.deltaTime,0,0);
}
if (estado == false)
{
transform.Translate(-lado*Time.deltaTime,0,0);
}