Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Alienjesus · Mar 14, 2011 at 12:38 AM · raycastingperspective

Raycasting forward in screen perspective

Ok, I tried explaining this before, but it seemed hard to explain and not many people understood what I was trying to do. I'm hoping this image will help, but honestly, I'm not optimistic.

alt text

In the above image you can see my problem. In my screen shot on the right hand side are two cubes - the front most one is controllable by the player and is raycasting forward. If the ray hits an object it can be picked up and moved around. However, in order for the ray to hit the crate behind it (the one slightly to its left), it needs to be held in the position shown because of the way the camera perspective works.

Obviously this isn't very user freiendly (they have to try and work out how far to either side to move the object to pick it up) so I want the ray being cast from the object to move forward from the main cameras perspective rather than in worldspace - thus allowing you to pick up an object that appears directly behind you when you play the game.

Please tell me if you need anything clarified and I'll do my best, but even trying to explain this much has given me trouble.

Anyway, here is my code so far (in java, attached to the controllable raycasting cube:

var hasObject: boolean = false; var otherThing: Transform;

function Update () {

var fwd = Camera.main.transform.forward; var hit : RaycastHit; //only finds objects in the 'moveable' layer var LayerMask = 1 << 9;

if (Input.GetButtonDown ("Fire2")) { if (!hasObject) { //if (otherThing){ if (Physics.Raycast(transform.position, fwd, hit, 50, LayerMask)) //if (Physics.Raycast(ray, hit, 50, LayerMask)) {
otherThing = hit.transform; otherThing.rigidbody.isKinematic = true; otherThing.parent = transform; otherThing.Find("Glowblock").light.enabled = true; hasObject = true; print("Picked up "+otherThing.gameObject.name+"\n"); }else { print("Nothing to pickup!\n"); } } else { otherThing.rigidbody.isKinematic = false; transform.DetachChildren(); otherThing.Find("Glowblock").light.enabled = false; hasObject = false; print("Dropped "+otherThing.gameObject.name+"\n")
} } }

Comment
Add comment · Show 2
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 loopyllama · Mar 14, 2011 at 03:50 AM 0
Share

can you clarify? post more screenshots. the second diagram doesn't look correct to me. Perhaps you can explain it's shape...

avatar image Alienjesus · Mar 14, 2011 at 09:59 AM 0
Share

I'll post more screenshots later. The second diagram is meant to show how the camera perspective looks when you play the game, as it narrows to a point in the distance.

1 Reply

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

Answer by Owen-Reynolds · Mar 14, 2011 at 02:49 AM

So, to clarify, you can pick something up when box#1 looks like it is in front of box2.

If this is the case, you can think of box#1 as a cursor being moved over an item to select. It's just more difficult to move than a real cursor.

The standard solution is to shoot a ray from the camera through the box#1. For a cursor, you have to convert it from screen pos to world pos using ScreenPointToRay and raycast from the camera through it. In your case, everything is in world pos, so you can skip that step. Try:

Raycast(transform.position, transform.position-camera.position, ... )
          ^^ from            ^^ direction
Comment
Add comment · Show 4 · 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 Alienjesus · Mar 14, 2011 at 10:03 AM 0
Share

Yes, I want to pick it up when it looks directly in front of it, just like a cursor. At the moment, the camera perspective distorts that and means I need to move the object slightly left or right of the object i want to pick up. I think that's what you're saying here, so I'll try your method and report back later.

Shouldn't the first box shouldn't be picked up by the raycast anyway because it's not in the moveable layer. I'll make sure I do that anyway.

avatar image Alienjesus · Mar 14, 2011 at 02:33 PM 0
Share

Gives me an error: Assets/Scripts/$$anonymous$$ovement script.js(57,46): BCE0019: 'position' is not a member of 'UnityEngine.Camera'.

avatar image Alienjesus · Mar 14, 2011 at 02:40 PM 0
Share

never $$anonymous$$d, i was being stupid. I just need camera.transform.position.

Works great, thank you!

avatar image Owen-Reynolds · Mar 15, 2011 at 03:19 AM 0
Share

About the first box, I just realized you could go from it. Editting my response.

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

1 Person is following this question.

avatar image

Related Questions

Multiple Camera.main.ScreenPointToRay 1 Answer

Instantiating prefabs, when there is nothing 2 Answers

How to make a custom wheel collider ? 1 Answer

Hover effect : raycasting problems with terrain height and speed 1 Answer

What effects/controls a ScreenPointToRay() ray? 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