Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by harpwood · Apr 18, 2016 at 04:38 PM · physics2daddforce

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.

alt text alt text

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));


inspectors.jpg (297.9 kB)
pic1and2.jpg (263.6 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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();

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

54 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges