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

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.

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.

How to define the direction perpendicular to a GameObject's motion (Unity)?

I'm trying to create a game where a ball is launched off from a circle, much like a cannon, but it can be launched into any direction, 360ยบ because the circle which the ball is attached to can rotate. So, I was thinking of using Rigidbody2D.AddForce() but I'm not sure how to define the direction I want the force to be applied. I want it to be the direction perpendicular to the ball/player's movement but I don't know how to define that. Thanks for any help! :)
If you get the direction that the ball is travelling (as say degrees) you can add a certain angle to that and use the angle to determine the force that needs to be applied.
I'm assuming that the ball/player has no ridged body, if it does then you can just grab the direction directly. Also since your using circle to describe the spawned shape i'm going to assume your working in 2D.
Vector3 lastPosition;
void Update()
{
//launch the ball before updating the last position
lastPosition = transform.position;
}
Vector3 launchDirection()
{
Vector3 ballDirection = transform.position - lastPosition;
float angle = Mathf.Atan2(ballDirection.x, ballDirection.y) * Mathf.Rad2Deg;
//the direction should be perpendicular to direction of movement
float angle += 90;
return new Vector2(Mathf.Cos(angle), Mathf.Sin(angle));
}
I haven't ran the code in unity but it looks like it should work. Basically to get the direction you store the last position and compare it to the current direction to work out the delta (if your using a ridged body to move around then grab the direction from that instead) then use atan2 to get the angle. To make the launch direction perpendicular to the ball I added 90 degrees (you might want -90 depending on what side you need the circle to launch from). Now that we have the angle that you want the ball to shoot out from, use that with Sin and Cos to get the XY of the angle.
There you have it that will get you the direction perpendicular to movement, you your using 3 dimensions it's basically the same maths just with a few numbers thrown in to pad the return value.
Oh if your object always faces the direction of movement transform.right also works :P

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.

vector math in unity to avoid obstacles

I am trying to make my character to avoid obstacles while moving.
the target is defined on click and character moves to it on every frame by updating transform.position (so no physics here).
every obstacle has collider and character has trigger. on TriggerStay event of the character I have managed to get some info about the contact point:
Vector3 point = other.ClosestPointOnBounds(transform.position);
Vector3 dist = point - transform.position;
and the best thing I came to is to:
Vector3 perp = Vector3.Cross(transform.forward, dist.normalized);
float dir = Vector3.Dot(perp, transform.up);
transform.position -= transform.right * dir;
but it does not work as I was expected. actually it does not work at all.
how to correct character position, So it appears to be smoothly deflected of obstacle, while moving to the target?
EDIT: here is the screenshot
white rays show the direction of contacts, so they are multiple.
what i want is that the character can avoid obstacles while moving to his target.
for now i came to new idea:
float correction = Vector3.Dot(transform.right, dist.normalized) / 10;
transform.position += transform.right * correction;
it somewhat works as i want, but character is constantly jumping from one place to another. don't know how to fix this.
also tried correcting direction of movement by substracting the vector of contact from vector of movement, but character is also moves jerky and rotates stupidly.
You can try something like:
Vector3 perp = Vector3.Cross(transform.forward, other.transform.up);
transform.position -= perp.normalized * other.bounds.size.x / 2f;
You seem to not use Vector3.Cross proper way. Read here about it: Unity3D docs link

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 ?