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
1
Question by stbandk · Feb 11, 2019 at 12:42 AM · vector3angleportal

Laser redirected through portal

Hi, I make Portal game for AR and I can't solve this problem. I want redirected laser through portal to second portal (Image 1)

alt text

 public void ReflectLaser(RaycastHit hit, Transform laserStartPosition)
 {
     Vector3 laserDirect = laserStartPosition.position - hit.point;
     Vector3 portalDirect = transform.position - hit.point;
     
     float angle = Vector3.Angle(laserDirect, Vector3.forward);
     
     GameObject point = new GameObject();
     point.transform.position = otherPortal.GetComponentInChildren<Portal>().laserStartPoint.position - portalDirect;
     otherPortal.GetComponentInChildren<Portal>().SetLaser(point.transform, angle);
 }

 public void SetLaser(Transform laserTransform, float angle)
 {
     laserTransform.rotation = Quaternion.AngleAxis(angle, Vector3.up);

     laser = Instantiate(GameManager.Instance.other.LaserRed);
     laser.SetActive(true);
     laser.GetComponent<Laser>().InitLaser(1, laserTransform);
 }

this code works fine when both portals are side by side on one wall, as you can see on second image

Problem is when second portal are placed on surface or somewhere else. Redirected laser has still same direction. Check pic. 3

Please what is wrong with this ?

alt text

problem3.png (56.1 kB)
problem6.jpg (315.4 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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Feb 11, 2019 at 02:18 AM

Just forget about messing with angles. All you need to do is transform your worldspace direction into local space of your entrance portal. Once you have the direction and hit pos in local coordinates, just transform them back to worldspace using your exit portal. So all you need is

 var lPos = entranceTransform.InverseTransformPoint(hit.point);
 var lDir = entranceTransform.InverseTransformDirection(hit.point - laserStartPosition.position);
 lPos.x = -lPos.x;
 lPos.z = -lPos.z;
 lDir.x = -lDir.x;
 lDir.z = -lDir.z;
 var outPos = exitTransform.TransformPoint(lPos);
 var outDir = exitTransform.TransformDirection(lDir);

Note that the 4 lines in the middle just flip the local position / direction. This assumes that the surface normal of the portals is the z axis of the transforms. It basically does a 180° rotation around the y axis. Note that we assume that the hit position is at the portals surface. Keep in mind that if the hit point is actually "before" the entrance portal, it would be "behind" the exit portal. Though this is the correct relative position anyways.

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
avatar image
1

Answer by Chimer0s · Feb 11, 2019 at 12:51 AM

From what I can see, you're going off the world forward vector when calculating the angle to feed into your set laser function. As a result, you'll always have the lasers going in the exact same direction, because they're both set relative to the global Z-axis. You'll want to set your angle according to the start point's forward vector(or the direct laser's). Switching float angle = Vector3.Angle(laserDirect, Vector3.forward); to float angle = Vector3.Angle(laserDirect, transform.forward); may be all you need. You'll likely also have to set your rotation in the SetLaser function to work in local space, though.

Comment
Add comment · Show 1 · 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
avatar image stbandk · Feb 13, 2019 at 09:17 PM 0
Share

Thanks for fast reply but this didn't solve me problem.

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

125 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 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 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

making a camera that moves depending on mousepos.y 0 Answers

Uses of Vector3.Dot 0 Answers

from Quaternion to RotateAround 1 Answer

Problems with Vector3.angle and angle to target 1 Answer

Unity 8-Directional Locked Movement (Super Mario 3D World Style) 3 Answers


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