I'm developing a unity game's ability system in which some of the spells have specific range, I'm not quite sure how to do that but here's what i camed up with.
I will need some sort of sphere which will be invisible and the center of it will be my character.
The radius of the sphere will be equal to the range of the spell selected.
My spells are being cast over the mouse position which means i will be able to check if the sphere is colliding with the mouse.
Overall this idea with the sphere seems good to me because later on i will be able to add color to it so the user can also see the phsycal range of the spell if he want's to. But i see a few problems :
The mouse is moving only in 2 dimension x and y however for a 2D object collission to be detected the method requires another 2D colider, well the sphere is 3D.
private void OnTriggerEnter2D(Collider2D other)
{
}
I'm not sure how to make let's say 500 pixels range to be still relative to my screen and therefore this to be the actual radius of the sphere because my characters dont seem to move huge distance when i'm looking at the x axis, they move just a tiny bit and making a sphere with radius of 500 on the x axis will be complete disaster.
As i said I'm not sure how to make this, I'm new to unity and i'm not sure how to implement my idea, so any help or tips are welcome.
You need to project your mouse in 3D to do the collision check. (You can imagine your mouse shooting a line right in front of it) You can do that using raycast:
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if ( Physics.Raycast (ray,out hit,100.0f))
{
if(hit.collider == yourSphereCollider)
{
// the mouse was pointing at the sphere
}
}
Note that if there is another collider between the sphere and the mouse, it will be detected instead.
You can also use the same technique to determine where on the ground your effect should be displayed
Related
I'm making a space game and I want the game to take place inside of a sphere or cube. I want the walls of the shape to render what's inside on the inside walls so it seems like endless space. Once the illusion of endless is in place I want to teleport the player if they touch the game space shape and teleport them seamlessly to the opposite side of the shape.
Edit: I just had an idea of how to teleport how I want. Maybe using ray casting to cast a ray behind the player and where ever that hits when they touch the edge of the shape to teleport to?
To answer question one I used a render texture. It's very close to the effect I want I just have to play with the perspective like they do with portals in portal 1 and 2.
To answer the my second question I went with raycasting. The player flys around in a sphere and casts a ray behind itself. Once the player hits the collision of the sphere they teleport to the point of the raycast. This works perfectly only for the fact when the player reverses on the sphere collider they escape the sphere flawlessly.
Working in Unity 4.3, 2D mode and C#. I have a player controlled circle with a circle collider and a static edge collider with irregular shapes. The edge collider is in trigger mode.
The circle is supposed to react in a certain way once it has completely passed through the edge collider, but not if it collides with it, and turns back to the same direction it came from. The circle should be able to spin randomly and to cross the edge collider from any direction or angle and at any speed.
I'm trying to use OnTriggerExit2D, but I haven't been able to check if the circle goes through or turns back, because OnTriggerExit2D is launched in both occasions. Here's a picture to clarify:
Thanks in advance, this has been driving me nuts for the past few days.
Maybe you could record the object's position in OnTriggerEnter2D, and then compare it to current position in OnTriggerExit2D. Then use Physics2D.Linecast to determine whether there's a collider between those points - if yes, then you passed through. Note that this might not work reliably if your edge collider shape is complex and the object moves generally along the edge rather than across it.
I've searched the board, as well as the oculus board, and unity board. Couldn't really find something that helped.
I'm working on a vehicle simulation. Before we started using the oculus, it was just a regular first person perspective. You used a racing wheel/pedals to drive and the mouse to control all the buttons and switches etc. We use raycasting from the mouse point on the screen into the world to interact with the various controls in the vehicle.
Now that we're using the oculus, the raycast isn't taking into account the distortion matrix used on the oculus cameras. So you're not actually casting a ray at what you're visually clicking on. Using Debug.DrawRay I found that it was slightly off. Just to be sure, I disabled the lens correction via inspector on the OVRCameraController and sure enough the raycasting was working again.
The ray itself is calculated the usual way one does when firing from the mouse point:
ScreenPointToRay(Input.mousePosition);
Would anyone have any idea how I can adjust my ray so it works with lens correction on?
Cheers,
Gordon
Simply multiply the Distortion-Matrix with the Ray's Vectors (Position and Normal) and you have your new Ray. I would suggest using Homogeneous Coordinates with 4x4 Matrix and Vec4's where Positions has component w = 1.0 and Normals have w = 0.0; This way you can simply multiply and you are done - depending on lookup direction you might have to use the inverse matrix :)
Alright, what I ended up doing was creating a 3D cursor, bypassing the distortion matrix entirely.
I placed a gameobject at the same place as the "head" (between left eye and right eye cameras). It has a script on it that rotates up/down/left/right based on mouse movement. I then temporarily put a spot light with a narrow cone and high intensity on it so it looked like a laser pointer. I figured if the light is hitting things, so should a raycast of the same origin. Which ended up working.
However this didn't really solve the issue of using a cursor. I tried a number of things that ultimately didn't work (didn't line up with with where the light/raycast hit).
Finally I realized I was overlooking something very simple. I lowered the near clipping plane of the cameras and placed a plane as close as I could to the camera while still being visible. I then rotated it on local y by 180 so it would be invisible to the cameras and not block ray casts.
I then added some code so that when a raycast hit something, it would fire a second raycast from the hit point back to the origin. On the way it would have to hit the plane, which was essentially at the near clipping plane. I would then move my 3D cursor to that hit point.
Now it works as intended. Where the cursor is, is where the original raycast hit. The cursor now matched the position of the laser dot. So then I removed the light component. Done.
Hope this helps someone else someday.
I'm trying to make a respawn system for a game in unity that starts the character back on the last platform they were on.
As it is currently, it keeps track of which platform they were last grounded on with onCollisionEnter and detects if onCollisionExit touches an out of bounds area.
I need to find the position of a face on the mesh with the y axis (assuming the best way is to do a raycast on the global y axis from the center of the platform) and add the height of the character/2 to determine where to respawn the character.
I'm very new to unity and c#, so I've never done a raycast before and I'm not sure if it's possible to raycast from inside an object to find it's mesh in a given direction, or if there is a better/more efficient way.
Thanks in advance :)
"if it's possible to raycast from inside an object to find it's mesh in a given direction"
You can place a empty gameobject at the center of your mesh (make it child of your mesh ) then pass the position of this empty gameobject to raycast origin.
I usually make re-spawn system with triggers. If you explain little bit more what actually you want to do. I'll try to guide you in that particular direction.
I am working on a game, and I currently have objects continuously orbiting around a sphere at a fixed distance. I need to allow the object to bounce off of each other. Does anyone know how I can go about doing this?
I have the collision detection working, and each object has a bounding sphere. I am able to get the point of collision, I just need to take the current rotation vector from each object and get the resulting "bounce" angle (vector to rotate around) and have each object continue orbiting around it's new vector.
Let me know if that doesn't make sense or if you need anything else! I should mention that this is done using Unity3D (I am not using rigidbodies, or the built in physics engine for performance reasons)
Edit:
Here is what I've tried:
public void OnTriggerEnter(Collider collider)
{
// Determine resultant rotation axis
Vector3 collisionNormal = collider.ClosestPointOnBounds(thisTransform.position);
rotationAxis = Vector3.Reflect(rotationAxis, collisionNormal);
}
Here is a link to the Vector3.Reflect() method in the Unity3D docs: Vector3.Reflect()
At this point the objects don't start moving in a new direction they collide and then don't bounce off. They just appear to stop when the collision occurs.