# Making a vector3 z coord have a zero on reflect?

Right so before you laugh at my silly question lets get the laughs out of your system with a joke… why did the chicken fall through the world?.. cause he wasn’t rigid body enough HAHAHA… alright in all seriousness i found this little code and want to implement it so that x,y are always reflected but not the z. so when it hits an angle of an edge i will notice through debugged that it gains a z and will fly off in the distance.

``````using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ball_Behaviour : MonoBehaviour
{
[SerializeField]
private Vector3 initialVelocity;

[SerializeField]
private float minVelocity = 10f;

private Vector3 lastFrameVelocity;

private Rigidbody rb;

void OnEnable()
{
rb = GetComponent<Rigidbody> ();
rb.velocity = initialVelocity;
}

// Update is called once per frame
void Update ()
{
lastFrameVelocity = rb.velocity;
}

void OnCollisionEnter(Collision col)
{
Bounce (col.contacts [0].normal);
}

private void Bounce(Vector3 collisionNormal)
{
var speed = lastFrameVelocity.magnitude;
var direction = Vector3.Reflect (lastFrameVelocity.normalized, collisionNormal);

Debug.Log ("Out Direction:" + direction);
rb.velocity = direction * Mathf.Max (speed, minVelocity);
}
}
``````

so basically what i want is a way that if the object hits a corner and wants to go in z i can set it to 0 or make it so that the z isn’t possible to start with… i hope this makes sense for what i want.

MajorSmurf

Ahh okay I think I know what you mean now (for future Google people, see the comments too, I asked for clarification)

Without looking too much into it, I’d probably force it along the xy-plane by changing it to

`Mathf.Max (speed, minVelocity) * Vector3.Scale(new Vector3(1, 1, 0), direction).normalized;`

`direction * Mathf.Max (speed, minVelocity);` on line 40 (at the very end)
For example, `Vector3.Scale(new Vector3(1, 1, 0), new Vector3(10, 7, 4))` would give you back an expression equal to `new Vector3(10, 7, 0)`