Search Unity

Addforce is only applied when my object is lying down (0 or 180 degree rotation).

Discussion in 'Physics' started by Schytheron, Aug 29, 2015.

  1. Schytheron

    Schytheron

    Joined:
    Jun 6, 2015
    Posts:
    16
    So Im making a Portal-like 2D game and Im trying to get the portals to shoot the character out (with Rigidbody addforce) when he gets teleported. And this works...partially. You see, force is only applied to the character if the portal is rotated horizontally but not if its standing up vertically! WHAT?! I dont understand why this happens.
    Heres my code: (script is attached to both portals and force multiplier is exaggerated just to show of my issue in the video down below)
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PortalScript : MonoBehaviour {
    6.     public GameObject otherportal;
    7.     public GameObject player;
    8.     private Collider2D otherportalCollider;
    9.     public Rigidbody2D otherRigidBody;
    10.  
    11.     void Awake(){
    12.         otherportalCollider = otherportal.GetComponent<BoxCollider2D>();
    13.  
    14.     }
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.    
    19.     }
    20.    
    21.     // Update is called once per frame
    22.     void Update () {
    23.  
    24.     }
    25.  
    26.     void FixedUpdate(){
    27.  
    28.     }
    29.  
    30.     void OnTriggerEnter2D(Collider2D other) {
    31.         if(other.tag == "Player"){
    32.             otherRigidBody = other.GetComponent<Rigidbody2D>();
    33.             other.GetComponent<BoxCollider2D>().enabled = false;
    34.             other.GetComponent<CircleCollider2D>().enabled = false;
    35.             other.transform.position = new Vector2(otherportal.transform.position.x, otherportal.transform.position.y);
    36.             otherRigidBody.AddForce(otherportal.transform.up * 50, ForceMode2D.Impulse);
    37.             }
    38.            
    39.  
    40.  
    41.         }
    42.  
    43.     void OnTriggerExit2D(Collider2D other){
    44.         if(other.tag == "Player"){
    45.             other.GetComponent<BoxCollider2D>().enabled = true;
    46.             other.GetComponent<CircleCollider2D>().enabled = true;
    47.         }
    48.        
    49.     }
    50. }
    51.  
    Heres also a video I made to show the problem:
     
  2. Schytheron

    Schytheron

    Joined:
    Jun 6, 2015
    Posts:
    16