Hello im having a problem where all instances are pushed forward when i click to mouse button, and would like to know how to prevent objects that haven’t been clicked on to not leap forward.
in my project, i have this script which controls the dragging and throwing of enemy gameObjects.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragObject : MonoBehaviour
{
[SerializeField] float moveSpeed;
//[SerializeField] float force = 100f;
Vector2 lastPosition;
[SerializeField] float saveDelay = 0.2f;
[SerializeField] float power = 5f;
bool nextSave = true;
private bool following;
Rigidbody2D rb;
Vector2 dir;
bool canBePushed;
DestroyObject inAir;
// Use this for initialization
void Start()
{
following = false;
lastPosition = transform.position;
rb = GetComponent<Rigidbody2D>();
inAir = FindObjectOfType<DestroyObject>();
}
// Update is called once per frame
void Update()
{
if (!inAir.IsFallingTrue()) {
}
MouseClicksToDrag();
}
private void MouseClicksToDrag()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector3.forward);
if (hit.collider != null && hit.collider.gameObject == this.gameObject)
{
following = true;
}
}
if (Input.GetMouseButtonUp(0))
{
following = false;
dir = (Vector2)transform.position - lastPosition;
canBePushed = true;
}
if (following)
{
transform.position = Vector2.Lerp(transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition), moveSpeed);
}
if (nextSave)
{
StartCoroutine("SavePosition");
}
}
void FixedUpdate()
{
IfCanBePushed();
}
//when instantiated, cannot be effected by IfCanBePushed
private void IfCanBePushed()
{
{
if (!inAir.IsFallingTrue())
{
if (canBePushed)
{
canBePushed = false;
rb.velocity = dir * power;
}
}
}
}
IEnumerator SavePosition()
{
nextSave = false;
lastPosition = transform.position;
yield return new WaitForSeconds(saveDelay);
nextSave = true;
}
}
and a simple enemy spawner:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyMovement : MonoBehaviour {
GameObject gameObjectEnemy;
DestroyObject destroyObject;
[SerializeField] float speed = .1f;
// Use this for initialization
void Start () {
destroyObject = FindObjectOfType<DestroyObject>();
}
// Update is called once per frame
void Update () {
StopMovement();
transform.Translate(speed, 0f, 0f);
}
private void StopMovement() {
if (destroyObject.IsFallingTrue() == true){
speed = 0;
}
}
}
however, whenever my enemies are instantiated which is from this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnEnemy : MonoBehaviour {
[SerializeField] GameObject enemyType;
[SerializeField] float spawnRate = 4f;
float nextSpawn = 0.0f;
float randX;
Vector2 whereToSpawn;
void Update ()
{
if (Time.time > nextSpawn) {
nextSpawn = Time.time + spawnRate;
//randX = Random.Range(-8.4f, 8.4f);
whereToSpawn = new Vector2(transform.position.x, transform.position.y);
Instantiate(enemyType, whereToSpawn, Quaternion.identity);
}
}
no matter where i click, it powers all the enemies forwards; as if i had already thrown them.
I know it has something to do with this either these parts of the script:
if (Input.GetMouseButtonUp(0))
{
following = false;
dir = (Vector2)transform.position - lastPosition;
canBePushed = true;
}
or:
void FixedUpdate()
{
IfCanBePushed();
}
//when instantiated, cannot be effected by IfCanBePushed
private void IfCanBePushed()
{
{
if (!inAir.IsFallingTrue())
{
if (canBePushed)
{
canBePushed = false;
rb.velocity = dir * power;
}
}
}
}
whenever i add something to prevent canBePushed from occurring before the mouse click is let up, the ball won’t leave the mouse position.
Ive been stuck on this for 3 days now, and i can’t seem to come up with anything. any help would be appreciated! let me know if it would be easier to see a video of the problem.