- Home /
Moving objects using raycasting?
OK, so earlier I posted a question about trigger colliders which were recommended to me for my game. Basically, i want to be able to move a background object using the position of a game object in the foreground. Previously I had a cube in the foreground with a long box trigger attached to it to interact with objects in the background, the idea being that when objects activated its triggerStay, I could then trigger code to allow that object to me moved as a child of the foreground object.
However, I've had some issues with this (such as objects not being found despite having a rigidbody and collider) and was wondering whether it might be possible to do it with a raycast instead. Here's what I want to know.
The box collider caused problems because of the camera perspective - perspective viewpoints caused trouble because the boc collider would extend to a central point in the distance, meaning the placement of my foreground object didnt directly correspond to what it could pick up. I was wondering if a Raycast would go directly forward in terms of screen position instead of worldposition so I could avoid this. (If this is possible with a trigger i'd like to know too!)
How would I make my Raycaster's origin move with the gameobject i want to cast from? Is it as simple as including the raycaster as a script on that object or on a child of that object?
P.S. I know a mouse is a better way to do what I'm describing, but this is for a degree project game that relies on a physical method of control so that's not an option for me right now.
Thanks, if I've made this a bit confusing please tell me and I'll try to elaborate it a bit clearer. I am a newbie when it comes to Unity, so these might be half understood ideas that dont make sense to you experienced users
Answer by Alec-Slayden · Feb 28, 2011 at 11:34 PM
you can get the screen point of an object using WorldToScreenPoint
and raycasts can go from any 2 points that you'd like, or make a ray from the screenpoint of that object position with a certain length. A ray as such should go directly away from the screen, rather than being distorted by perspective.
EDIT: If you wanted perspective to be gone as a whole you could use a camera with orthographic projection
Thank you, this is very helpful I'll try it now.
As for the orthographic camera thing, I tried it before but it doesn't work for what I need it for - the lack of the disappearing point in the distance was very helpful but unfortunately I need to have a sense of perspective that is completey lost with the orthographic camera - I will be moving objects from the distance to the foreground, so theyed need to change sizes to reflect this. Whilst my manipulation of the environment and the movement of the game environment are locked to 2D, my game environment needs to be fully 3D.
Sorry to double comment, but if I'm right this should only find one object at each point right? It'll only find the foremost object in the scene at that screenpoint? If so, this is perfect and exactly what I needed!
Your answer
Follow this Question
Related Questions
Prevent shooting when gun is inside wall 1 Answer
Help with collision detection with specific colliders 1 Answer
active script from distance 3 Answers
Child collider triggering parent collider effects. 1 Answer
Trigger can't see the enemy 2 Answers