# Keeping transform.position.Y Locked at a certain number

I have an drone that is programmed to wander randomly along the X and Z dimensions with Y staying locked at 56. This works fine but when i add in object avoidance there comes instances in which the drone trys to avoid objects by going over them.
Here is his avoidance code:

`````` //Cast ray from transform if you hit one..
if (Physics.Raycast(transform.position, transform.forward, out objectHit, 10f))
{
//If the transform you hit isnt you
if (objectHit.transform != transform)
{
//How much you want to repel by the opposite direction
wanderSpot += objectHit.normal * 10;
transform.Translate(Vector3.back* 10f * Time.deltaTime);

}
}
``````

How do I make it so he only wanders to the left or the right around the object. Also i want to keep his Y at 56 at all times no matter what. I already clicked the lock Y axis in the rigid body but it doesnt work.
Also i know that the only way to change postion.y value is to make a new Vector 3 and set the position to it … for Ex:

transform.positon = new Vector 3 (transform.x,56,transform.y)

OR

Vector3 tmp = transform.position;
tmp.y = 56;
transform.position = tmp

but this makes him stay in the same spot. I need to lock only the Y and make him move around objects with out trying to go over them. I hope i explained this well… thank you in advance.

Ok… so there are several things fishy here. The first and obvious thing is, you are not
using `wanderSpot` at all when setting things back to the transform. You only use your “back” vector, hence you will just come to a halt.

You probably ment this:

``````transform.Translate(wanderSpot * Time.deltaTime);
``````

Anyhow… the next problem could be, that if you only clamp the y to 56 after moving and if your avoidance algorithm wants to ONLY avoid in the y - axis, then you just nulled out your whole avoidance.

You use the normal of the impact point as avoidance vector. This is a quick-and-dirty approximation if you only have convex objects to avoid. However, this will never work if you perfectly approach the object straight on, because then, its normal on impact point is exactly opposite of your movement: you just slow down.

The best (and hardest) solution is to use a “proper” avoidance algorithm that takes your moving object size into account, the circumference of your hit object and calculate the best avoidance vector blabla… complicated math… blabla…

But if you want to keep it simple, I suggest you do a check whether the X/Z distance of your avoidance vector is “very very small sideways” and if so, just shift it a bit clock-wise. This way, you will tend to go around an object on the right side (do it counter-clock-wise if you are australien or british. You know… the guys driving on the “wrong” side of the road? :-P. Just kidding, you get the idea ;))

Some code to get you the idea: (written from memory. Not tested)

``````//Cast ray from transform if you hit one..
if (Physics.Raycast(transform.position, transform.forward, out objectHit, 10f))
{
//If the transform you hit isnt you
if (objectHit.transform != transform)
{
//How much you want to repel by the opposite direction
var avoidance = objectHit.normal * 10;
avoidance.y = 0; // zero out the Y-axis. We don't want to move up or down.
if (Vector3.AngleBetween(avoidance, transform.forward) > 179)
avoidance = Vector3.RotateTowards(avoidance, transform.right, 0.1f, 0);
transform.Translate(avoidance * Time.deltaTime);
}
}
``````

I don’t know much but maybe if you checked if the drone Y is greater or equals to 56 then return his Y position to 56

``````if (transform.position.y >= 56)
{
transform.position = new Vector3(transform.position.x, 56, transform.position.z);
}
``````

it worked with me when I need to make an object stays in one position in a specific axes