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

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 ?

Related

Freezing Movement to the side after rotating GameObject

I am trying to freeze the position of a GameObject towards the z-Axis but when turning the GameObject the z-Axis should turn with it (or atleast the freeze Position)
So basically I want my object to only be able to move forward, backwards and up and down, no matter what direction it is facing.
A RigidBody freeze Position is in relation to the world axis and not the rotated axis of the object.
Appreciate the help, thank you
Top Down View of Object, before rotation and after
You can get the GameObject's "z-axis" with transform.forward. If this isn't the axis you want, try transform.right or transform.up.
It's unclear what you're asking for. Here are two cases that might answer your question:
To confine velocity to the "z-axis":
//reference to the Rigidbody to restrict
private Rigidbody rig;
void Start(){
//find the Rigidbody attached to this GameObject
rig = GetComponent<Rigidbody>();
}
//Updates every physics frame
void FixedUpdate(){
//set the velocity to the component of the velocity that is parallel to the forward direction
rig.velocity = transform.forward * Vector3.Dot(transform.forward, rig.velocity);
}
If you don't want any movement in the "z-axis", replace the rig.velocity = ... line with rig.velocity -= ...
Put this code in a MonoBehaviour (script) attached to your GameObject.
Note that if the GameObject is physically hit, it might spin, causing the "z-axis" to rapidly change. To prevent this, you could use the Rigidbody freeze rotation.

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.

Unity: aligning object from multiple points to ground

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.

Unity|Moving gameobjects away from other gameobjects which are moving towards them

I have 2 AI game objects which are both capsules. Considering the first capsule AI is named X and the second capsule AI is named Y, I try to make Y move away from X (escape from him) while X is chasing Y (following him). I have no idea how to do that, I would appreciate a direction.
I tried to do what's written here, but they both move through walls even though they have capsule collider, I tried to do this:
http://forum.unity3d.com/threads/getting-objects-to-move-away-from-my-users-gameobject.142468/
but they only move in one direction and through walls.
Vector3 position = transform.position;
Vector3 targetPosition = target.transform.position;
Vector3 direction = position - targetPosition;
transform.position += direction * 2.0f * Time.deltaTime;`
You are moving them with transform.Translate. Moving the transform means "Put the object at the given position regardless of environment". If you want them to interact, you need to either use the CharacterController component and its methods or a rigidbody component and move it with force.

How to move object based on its angle?

I am using Unity3d with mono develop(c#).
My object is Arrow.It gets Random Angel at starting.When i click on arrow ,it should be move like gun shooting some Point.How to achieve this?can any one help?
The forward Vector3 on the Transform belonging to your GameObject describes which direction is forward for the object.
An example given on the linked page is rigidbody.velocity = transform.forward * 10;. So if you were to put that line of code in your FixedUpdate function then your GameObject would move forward at a velocity of 10 * framerate.