Problem: Addforce affects wrong game ogject?
Hello, I am making a simple 2D game and I encountered a 2D physics problem I can't solve my own and I cannot find a similar problem/solution online.
A sprite is falling from a pipe. This sprite has not any script attached yet, and falling by rigidbody2D gravity. The player controls a train bellow by moving only at horizontal axis. The wagons of the train are connected with springs that bounce the falling sprite up. Firstly I put a collider at spring with bouncing physic 2D material, but the sprite only bounced once on the spring. So I wrote a script that gives AddForce to sprite to up direction every time the sprite collides with the spring. The scripted worked but appeared another problem. Now the sprite is falling and accelerating faster like some AddForce is pushing it downwards but only if the player holds down the directional keys to move the train.
I provide screenshots, animations and parts of the code. I hope you can help me stop this new behavior of the sprite and start falling again like before I put the spring bouncing logic, no matter what keys the player press to control the train.
Here you can see the normal fall before I applied the script
Here you can see the fall broken as I describe above, It broken after I put the script on the spring. Please note that I put the animation of the spring many days ago and it's not part of the problem.
// This script is attached to spring prefab (part of the train that connects the wagons
using UnityEngine;
using System.Collections;
public class IfActorTouchesSpring : MonoBehaviour {
public float SpringForce = 0.3f;
private Animator anim;
void OnTriggerEnter2D(Collider2D coll)
{
anim = GetComponent<Animator>();
anim.SetBool("IsDownAndUp", false);
anim.SetTrigger("SpringDown");
GameObject actor = GameObject.FindGameObjectWithTag("Actor");
Rigidbody2D rgActor = actor.GetComponent<Rigidbody2D>();
rgActor.AddForce(new Vector2(0f, -SpringForce));
}
void OnTriggerExit2D(Collider2D coll)
{
anim = GetComponent<Animator>();
anim.SetTrigger("SpringUp");
anim.SetBool("IsDownAndUp", true);
}
// This script controls the train instantiation and the movement affected on train by the player. Sorry for the greek comments bellow, I can translate you anything if necessary...
using UnityEngine;
using System;
using System.Collections.Generic;
public class TrainController : MonoBehaviour
{
public float trainSpeed; //H ταχύτητα του τρένου
public GameObject[] wagon; //Πίνακας με τα αντικείμενα που θα αποτελούν το τρένο, δηλαδή τα βαγόνια (wagons)
private GameObject currentWagon; // To wagon που θα επιλεχθεί με βάση τον int trainIndex (1-14). To 0 είναι reserved για το spring
private int trainIndex; // int trainIndex θα οργανώσει ποιο βαγόνι
private GameObject train;
private Rigidbody2D rbTrain; //Αναφορά στον Rigidbody2D για χρήση 2D Physics.
void Start()
{
train = GameObject.FindGameObjectWithTag("Train");
rbTrain = train.GetComponent<Rigidbody2D>(); // καβάτζωμα του Rigidbody2D
TrainConstractor (6, 10, 9, 8, 1); // δημιουργία του τρένου, κάθε αριθμός αντιστοιχεί σε βαγόνι
}
//FixedUpdate is called at a fixed interval and is independent of frame rate. Put physics code here.
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal"); // το οριζόντιο Input σε float
Vector2 movement = new Vector2(moveHorizontal, 0); // δημιουργία vector2 μεταβλητής για να αποθηκευτεί η κίνηση του τραίνου
rbTrain.AddForce(movement * trainSpeed); //Εφαρμογή των Physics στο τραίνο επί την ταχύτητά του
}
void TrainConstractor(int wagon1, int wagon2, int wagon3, int wagon4, int wagon5)
{
int counter = 1;
for (int x = -24; x < 30; x += 12) // το κάθε βαγόνι-sprite είναι 6 units στον χ άξονα //6 units είναι και το spring-spite (συνδετικό sprite των wagons).
{ // άρα το επόμενο βαγόνι θα πρέπει να τοποθετηθεί 12 μονάδες παραπέρα στον χ
if (counter == 1)trainIndex = wagon1;
if (counter == 2)trainIndex = wagon2;
if (counter == 3)trainIndex = wagon3;
if (counter == 4)trainIndex = wagon4;
if (counter == 5)trainIndex = wagon5;
currentWagon = (GameObject)Instantiate(wagon[trainIndex], new Vector2(x, -29), Quaternion.identity); //instatiate το wagon
currentWagon.transform.parent = transform; //το instatiated wagon γίνεται child του Train (GameObject στο Hierarchy)
currentWagon = (GameObject)Instantiate(wagon[0], new Vector2(x + 6, -28), Quaternion.identity); //instatiate το Spring 6 units στον χ παρακάτω
currentWagon.transform.parent = transform; //το instatiated spring γίνεται child του Train (GameObject στο Hierarchy)
counter++;
}
counter = 1;
}
}
To summarize, this bug appears when I put the following 3 lines of code in IfActorTouchesSpring script inside the OnTriggerEnter2D:
GameObject actor = GameObject.FindGameObjectWithTag("Actor");
Rigidbody2D rgActor = actor.GetComponent<Rigidbody2D>();
rgActor.AddForce(new Vector2(0f, -SpringForce));
Answer by harpwood · Apr 19, 2016 at 01:14 PM
[SOLVED]
I removed the line : GameObject actor = GameObject.FindGameObjectWithTag("Actor");
and I changed this line Rigidbody2D rgActor = actor.GetComponent(); to this Rigidbody2D rgActor = coll.GetComponent();
Your answer
Follow this Question
Related Questions
2D Knockback problem 2 Answers
Trying to add force to an object... is this done correctly? 0 Answers
Rigidbody2D Addforce not working on a simulation physics scene 0 Answers
RigidBody2D AddForce Not Adding Acceleration 0 Answers
2D Top-Down Character: How to make my character move using physics? 1 Answer