Unity C# raycasting from center of an object to find mesh intersection - c#

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.

Related

Create ability radius Unity C#

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

Unity 2D - Freeze axis and AddForceAtPosition

I'm learning Unity 2D and I want to do something like this:
Basically the character runs into the box/edge and it falls over. BUT! I want to lock down the X axis somehow. So in reality it would kinda look like if it was just rotated simple by 90 degrees (with some kind of acceleration).
I've tried to do it with rigidbody2d and edgecollider and AddForceAtPosition, but I failed miserably.
What I really wanted to do is lock down the "wall" and apply the force at the very top of the rigidbody so it would just fall over to the right, but it simply didn't work out.
Any help would be appreciated!
Remember that same logic applies to physics in games as it does to physics in real life. Just make a hinge and put the anchor and connected anchor at the same location at the bottom and set a limit for hinge.
Wall:
Hinge:
I set Lower Angle to something near 90, otherwise wall becomes uneven when it drops.
Remember to put a Rigidbody2D and a Box Collider for wall.
Firstly, I would lock the rotational axes that you don't want to move on your rigidbody, then, if your object origin is located on the ground / bottom of the object, you can add rotational torque to the object to achieve the affect you want :) http://docs.unity3d.com/ScriptReference/Rigidbody.AddTorque.html

Unity 2D line collider

I try to do the following: I have a point A at -4x-4y and a point B at 4x 0y. I want to make a colliding line from position A to position B.
I tried to do it with a linerenderer but I can't get the line to collide with my other 2d objects.
My other tought was to calculate the center of the points and the rotation and do it with a box collider but that seems to be really complicated and hacky.
Is there a simple way to achieve this?
Thanks in advance
You can use PolygonCollider2D, it's automatically create collider for sprites, and if you are not satisfied results, you can edit it by clicking Edit Collider in inspector, or trought Unity's API.
I think you must have a Rigidbody2D attached with your other 2D object. Then this will work 100%. You can use any collider it doesn't matter.

How to determine if an object is between two other objects in XNA

I have a screen filled with circles. One circle is the player, one is the enemy, and the others are obstacles. I want the enemy to be able to calculate if there is an obstacle in the path of a straight line from the player to the enemy, so it can adjust accordingly (it is taking place in space, so it is a straight line for jumping from asteroid to asteroid).
Right now, I just turn a random direction when the AI is stuck on an asteroid.
A simple solution could be find the Vector2 between player and enemy (using Vector2.Subtract), and then divide it into smaller vectors or reduce it to a delta that you choose. This can be done normalizing the distance vector and multipling it by a fixed number.
Then simply add that delta to the enemy position and check if the new position collides with an objects (maybe using Rectangle.Contains method), and do this until you reach the player.

Orbit Collision Angle

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.

Categories

Resources