- Home /
Transform Help
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;
}
Answer by AlwaysSunny · Nov 07, 2014 at 02:30 PM
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.
Answer by Bunny83 · Nov 07, 2014 at 06:45 PM
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.
Your answer
Follow this Question
Related Questions
how can i target a nother cube from a script 1 Answer
Prevent changing position 1 Answer
Need some help with to grap .x with a Raycasthit (c#) 1 Answer
Please help. I can't stop this light from moving!!! 1 Answer
Rotating GameObject problem 4 Answers