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 /
avatar image
0
Question by Schytheron · Aug 29, 2015 at 04:31 PM · rigidbodyforcevectoranglesportal

How to make a Vector with the same angle as a certain object?

Hi Im making a Portal style game in Unity but in 2D. And I got stuck while making the portal system (Im a Unity noob). I want my character to be shoot out of the portal that he exited in the direction that the portal is facing (with Rigidbody2D.AddForce).

I also want the AddForce vector "power" to be dependent of the velocity that the character has before entering the first portal. I have tried everything I could have come up with and I cant seem to figure it out (without making a ton of IF statements... sigh). Any help Please?! :)

Im kind of bad at explaining so I made 2 pictures in Paint to demonstrate (excuse my shitty Paint skills ;) ):

http://i.imgur.com/5QjGGVH.png
http://i.imgur.com/V6ftd2o.png

Comment
Add comment · Show 5
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
avatar image _Gkxd · Aug 29, 2015 at 04:35 PM 0
Share

It would be probably be one of the following vectors: transform.forward, transform.right, transform.up, where transform is the transform of the portal object.

avatar image Schytheron · Aug 29, 2015 at 04:49 PM 0
Share

@_Gkxd Wait, why would I need transform here? transform.forward, transform.right and transform.up Point to the global axises right? transform.up for example points to the global Y-axis and not what is up from the portals point of view, right? Or am I wroing, i have no idea

avatar image fafase · Aug 29, 2015 at 05:04 PM 0
Share

transform.forward is the forward in the object coordinates. Vector3.forward is the world z axis and is constant.

avatar image Schytheron · Aug 30, 2015 at 05:54 PM 0
Share

Hmm, I tried a solution using IF statements but on OnTriggerEnter It detects only the first if statment ( if(otherportal.transform.rotation.z == 0) ) and not the second one ( if(otherportal.transform.rotation.z == 90) ). I dont understand why.

Heres my code: using UnityEngine; using System.Collections;

 public class PortalScript : $$anonymous$$onoBehaviour {
     public GameObject otherportal;
     public GameObject player;
     private Collider2D otherportalCollider;
     public Rigidbody2D otherRigidBody;
 
     void Awake(){
         otherportalCollider = otherportal.GetComponent<BoxCollider2D>();
 
     }
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
 
     }
 
     void FixedUpdate(){
 
     }
 
     void OnTriggerEnter2D(Collider2D other) {
         if(other.tag == "Player"){
             otherRigidBody = other.GetComponent<Rigidbody2D>();
             other.GetComponent<BoxCollider2D>().enabled = false;
             other.GetComponent<CircleCollider2D>().enabled = false; 
 
             if(otherportal.transform.rotation.z == 0){
                 other.transform.position = new Vector2(otherportal.transform.position.x + 1,otherportal.transform.position.y + 1);
                 otherRigidBody.AddForce(otherportal.transform.up * otherRigidBody.velocity.magnitude, Force$$anonymous$$ode2D.Impulse);
             }
             if(otherportal.transform.rotation.z == 90){
                 other.transform.position = new Vector2(otherportal.transform.position.x,otherportal.transform.position.y);
                 otherRigidBody.AddForce(otherportal.transform.right * otherRigidBody.velocity.magnitude, Force$$anonymous$$ode2D.Impulse);
             }
 
 
         }
     }
 
     void OnTriggerExit2D(Collider2D other){
         if(other.tag == "Player"){
             other.GetComponent<BoxCollider2D>().enabled = true;
             other.GetComponent<CircleCollider2D>().enabled = true;
         }
 
     }
 
 
 }
 
avatar image Scribe · Aug 31, 2015 at 03:04 PM 0
Share

rotation is of type Quaternion, the z component moves between 0 and 1 and not in a way you can visualise, you should see my answer on how to do it with Quaternions.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Scribe · Aug 29, 2015 at 05:06 PM

You could 'undo' the effect of the entered portals rotation, by multiplying by the inverse of its rotation. The at the other end, redo the rotation by multiplying by the new portals rotation:

The following is pseudo code

 Vector3 velocity;
 Quaternion rotation;
 Vector3 position;

 void OnEnterPortal(Collider2D collider){
     if(collider is not a portal) return; //make sure we are hitting a portal

     GameObject thisPortal = collider.gameObject;
     GameObject otherPortal; //get your other portal somehow

     //setup mapping rotations
     Quaternion undoRot = Quaternion.Inverse(thisPortal.transform.rotation);
     Quaternion redoRot = otherPortal.transform.rotation;

     velocity = undoRot * ourCharacter.velocity;
     rotation = ourCharacter.rotation * undoRot;
     position = undoRot * (ourCharacter.position - thisPortal.transform.position);

     //Set new variables
     ourCharacter.position = otherPortal.transform.position + (redoRot * position);
     ourCharacter.rotation = rotation * redoRot;
     ourCharacter.velocity = redoRot * velocity;
 }

Hope that helps!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Trying to apply a force on a ball at certain angles but the ball moves back and forth or otherwise moves erratically 1 Answer

How can i make a ball jump in a curve from one place to another and vice versa. Like juggling a ball ! 0 Answers

rigidbody problem 1 Answer

How to fix/clamp rigidbody dir vector, despite Force dir 1 Answer

Direction of rotation and ApplyForce 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