# How in move make Y=1 everytime ?

(Sorry for my bad English i’m French)

Hello everybody, for the first time i’m posting here, i need your help :

I have a game who look like Click and Move, RTS, for now, i just have the character. Click Movement work, but when i place reflief on the terrain, the character start to move at Y=1 and go to Y=3 and fly between original position and the mountain. If it’s possible, how can i make my character go to Y=3 without flying ?
Doing Y=1 all time, and when encounter a mountain, up to this mountain ?
Same thing when the character start moving from y=3 to y=1.

Here is my code :

``````public class ClickMove : MonoBehaviour {
Ray ray;
RaycastHit hit;
public float speed;
Vector3 newpos;
void Start()
{
newpos = transform.position;
hit.point = transform.position;
}
void Update()
{
float step = speed * Time.deltaTime;
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetKeyDown(KeyCode.Mouse1))
{
if (Physics.Raycast(ray, out hit, 100))
{
newpos = new Vector3(hit.point.x, hit.point.y, hit.point.z);
newpos.y = newpos.y + 1f;
Debug.Log(hit.point);
}
}
Vector3 direction = newpos - transform.position;
Vector3 movement = direction.normalized * step;
if (movement.magnitude > direction.magnitude)
{
movement = direction;
}

GetComponent<CharacterController>().Move(movement);
// transform.position = Vector3.MoveTowards(transform.position, newpos, step);    //LAST MOVEMENT BEFORE MODIFICATION
}
}
``````

Here is a picture (With paint :D) In green what i want, in red what i have .

Hi,

I’m not totally sure about goes wrong, but I figured I start with a few optimisation recommendation, and you can tell me more about what you want to do.

1st, you don’t want to call GetComponent every frame as it takes performances down.
Instead, you want to make a reference to the component, like :

``````public class ClickMove : MonoBehaviour
{
Ray ray;
RaycastHit hit;
public float speed;
Vector3 newpos;

private CharacterController _charController; // component reference
private void Awake ()
{
_charController = GetComponent<CharacterController>(); // reference assignment
}

void Start()
{
newpos = transform.position;
hit.point = transform.position;
}
void Update()
{
float step = speed * Time.deltaTime;
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetKeyDown(KeyCode.Mouse1))
{
if (Physics.Raycast(ray, out hit, 100))
{
newpos = hit.point; // you can simply pass the point info, it already is a Vector3
newpos.y = newpos.y + 1f;
Debug.Log(hit.point);
}
}
Vector3 direction = newpos - transform.position;
Vector3 movement = direction.normalized * step;
if (movement.magnitude > direction.magnitude)
{
movement = direction;
}

_charController.Move(movement); // use of preassigned component
// transform.position = Vector3.MoveTowards(transform.position, newpos, step);    //LAST MOVEMENT BEFORE MODIFICATION
}
}
``````

Then, what I believe is missing, is a terrain path sampler. Like, from A to B, you’re going over different altitudes, and I don’t see anything like that in your code. Which makes me think I may have misunderstood what you want.
Let me know.
ps : I speak French too