Unity: aligning object from multiple points to ground - unity3d

I need to allow players to drag objects around the surface of my lowpoly ground so that the object sticks to the ground from certain points (in this case all the 6 legs). I tried Raycast to set position and rotation of the object with:
transform.position = hit.point;
transform.rotation = Quaternion.FromToRotation (Vector3.up, hit.normal);
This works quite nice but it always leaves some of the legs in the air because I set position and rotation of the whole transform not each leg individually. I could Raycast all of the legs individually but after that how can I position and rotate legs so that other parts of the model stays with the legs and the model doesn't "break".
Also I tried to give each leg a rigidbody and box collider but that just breaks the model by shooting the legs somewhere.
All ideas are welcome.

Related

Unity Question: Transforming world coordinates to my Radar's Coordinates for 3D Radar

I'm learning Unity3D and having some trouble on my 3D radar. My radar is a child of my Player game object which is rotating around as I fly. The radar itself is rotated 45 degrees so it faces the user. There is a Cube that is supposed to be the radar blip of the enemy plane. The Cube is a child of Radar so should inherit its rotation. There is a script on the Cube to update itself every update(). Here is the hierarchy:
Enemy Plane
Player
-- Camera
-- Radar
------ Cube (radar representation of Enemy Plane)
The problem is that while the Cube itself is rotated, with the Radar, its motion is not. As I get closer to the enemy plane, the Cube just gets closer to the camera (which is good) but I would expect its motion to follow the 45 degree rotation of the parent Radar object?
public class RadarGlyph : MonoBehaviour
{
GameObject radarSource;
GameObject trackedObject;
Vector3 radarScaler;
void Start()
{
this.radarSource = GameObject.Find("Radar");
this.trackedObject = GameObject.Find("Enemy Fighter");
this.radarScaler = new Vector3(0.001f, 0.001f, 0.001f);
}
void Update()
{
Vector3 vDelta = this.trackedObject.transform.position - this.radarSource.transform.position;
vDelta.Scale(this.radarScaler);
this.transform.localPosition = this.transform.InverseTransformDirection(vDelta);
}
}
For a complete solution, you have to get the position of the target wrt the ship first and then recreate it within the context of the blip and the radar.
As a quick fix, you can try changing your last line like this:
this.transform.localPosition = this.parent.localRotation * this.transform.InverseTransformDirection(vDelta);
or (apparently not good as you mentioned)
this.transform.localPosition = Quaternion.Inverse(this.parent.localRotation) * this.transform.InverseTransformDirection(vDelta);
one of these is bound to work. (The first one did)
Edit: here's a third alternative
this.transform.localPosition = this.transform.parent.parent.InverseTransformDirection(vDelta);
This one gets the position in Player's space and applies it in radar's space.
The first and third are trying to do the same thing. Since you were transforming the direction into the blip's coordinate frame, any rotations that its parents have are canceled out. Instead, the correct thing to do is to get the position relative to the Player first. Then apply it to the blip in the radar. The third line of code I have here is attempting to do that.

Weird 2D physics in unity3d, making "Ballz" kind of game

As you see in the screenshot. When the ball hits between two colliders, it should do what red arrows shows. Instead it does what blue arrow shows. All the boxes in the game are perfectly next to each other.(Used an algorithm to make it perfect.).
I am using unity's own physics system. I only have this piece of code for the ball :
void Start()
{
rb = transform.GetComponent<Rigidbody2D>();
rb.velocity = new Vector2(10.0f, -5.0f) * 41f * Time.deltaTime;
}
What "Ball" game object has;
Sphere Collider2D
Bounce physic in Collider2D {friction set to: 0, bounciness set to: 1}
Those are enough to achieve exactly the same physics of Ballz game.
When the ball hits an edge of a collider, one of these situation happens. This is okay, exactly what i want. But I do not want the same thing happen when the ball hits between two colliders.
I hope i'm clear enough.

How do I make an object instantly drop to the ground (skipping the fall animation) in Unity?

Now, I have an object wherein I only know the x and z location. If I place it at a very high y coordinate, I would get an animation of it falling down towards the ground. I want to skip this and instantly make it be on the ground. How do I do that?
Place the object at an arbitrary Y-position well above the ground, then cast a ray down to the ground and move the object to the hit position.
Pseudo-code:
transform.position.y = 200f;
if (Physics.Raycast(transform.position, -Vector3.up, out hit)) {
transform.position = hit.transform.position;
}
You could use Raycast or Boxcast to determine the distance from the bottom of your object to the next collider below it. Then just subtract this distance from the objects transform.position.y.

Collision Detection of objects in certain area of camera in Unity3d

I am creating a 3rd person action game where the player is a helicopter and he can shoot other objects while moving. The problem is I am trying to find the enemy objects who are inside a circle in the center of the camera and I need to track them and shoot them.
Raycast wouldn't help as i need a thicker raycast and so I tried spherecast and capsulecast.
I have a GUI element which gives the player idea on where he can shoot.When using Spherecast or Capsulecast, It is working when the enemy is near but when the enemy is far behind I guess the spherecast becomes small while traveling along z and doesn't hit the object most times.
if (Physics.SphereCast (startPoint, 1f, transform.forward, out hit)) {
if (hit.collider.CompareTag ("Shootable") ){
Debug.Log(hit.collider.name);
Destroy(hit.collider.gameObject);
}
}
I have seen raycast from camera and so i was wondering if there is something to do like circlecast from the camera which would be appropriate for this. If not how can I proceed?
Any help is really appreciated.
If you want to detect whether enemies lie within a conical area in front of your camera, using a SphereCast or RayCast will not be able to meet your needs.
Instead, you might consider checking the angle between an enemy's relative position and your camera's forward vector, to see if it is below a particular value, and hence within the cone.
For a 60-degree field of view, and assuming you store your enemy Transform components in an array/List, your code might look like:
foreach (Transform enemy in enemies){
if (Vector3.Angle(transform.forward, enemy.position - transform.position) < 30){
Destroy(enemy.gameObject);
}
}
Hope this helps! Let me know if you have any questions. (Answer adapted from this Unity question.)

unity3d - Instantiate and fire bullet relative to player rotation in 3D

This is a top-down view but in 3D coordinates, I would like to instantiate and fire a bullet from the player's gun. This script is on a spawner object at the end of the barrel. Also tried putting the script on the player itself but also didn't work.
GameObject projectile = Instantiate (bullet, transform.position, transform.rotation) as GameObject;
projectile.GetComponent().AddForce(transform.forward * speed);
The problem is the bullets doesn't behave as intended, instead they don't appear relative to the player rotation they just go in a very different direction. Shouldn't "Transform.Forward" mean forward in the Z position regarding the object's transform ?