I need help with positioning. I tryed in the script below the X and Z do not move at all their stay at 0. i dont know how to move them. i think the problem is
transorm.position = hit.normal * Vector3.up * 3.0f;
but i need that to stay in the y(3) all the time when moving.
void Update() {
RaycastHit hit;
if (Physics.Raycast(transform.position, -Vector3.up, out hit))
transorm.position = hit.normal * Vector3.up * 3.0f;
transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;
}
I think you want to include the hit.point in your position calculation. A normal is a unit vector, and so is v3.up, so it’s always going to be within 3 units of the world origin this way.
Youc code shouldn’t even compile and it doesn’t make much sense either
hit.normal is a direction vector and Vector3.up is also a direction vector. You can’t simply multiply two vectors together since vectors have different types of multiplications. In Unity you can’t multiply two vectors with the *
operator and in this case it wouldn’t make much sense either ^^
I guess you want something like :
transorm.position = hit.point + hit.normal * 3.0f;
That will move your object to hit.point (which is a position, not a direction) and move it up by 3 units along the normal vector of the object your ray hits.