Quantcast
Channel: Latest Questions on Unity Answers
Viewing all articles
Browse latest Browse all 171066

Script changes behavior based on order in which the lines are written

$
0
0
In this piece of code, the first of the two transform.position assignments works badly (the transform stutters or fails to go up inclines in the terrain), while the second works flawlessly, no matter which of the two is first and which is second. Meaning: if you switch positions of the two lines of code, they change behavior. The rest is pasted for context. I find this to be very strange, I suspect it's something to do with using the same resource twice, but it might be something else. I haven't studied anything like this yet. RaycastHit hit; Vector3 height = transform.position + (transform.forward * direction.z); Physics.Raycast(height,-Vector3.up,out hit); terrainDistance = hit.distance; RaycastHit hitSide; Vector3 heightSide = transform.position + (transform.right * direction.x); Physics.Raycast(heightSide,-Vector3.up,out hitSide); terrainDistanceSide = hitSide.distance; if(terrainDistance < 20.0f || terrainDistanceSide < 20.0f) { //the first one of these two lines seem to act worse, depending on the order in which they're written transform.position = new Vector3(height.x, height.y + (20.0f - terrainDistance), height.z); transform.position = new Vector3(heightSide.x, heightSide.y + (20.0f - terrainDistanceSide), heightSide.z); }

Viewing all articles
Browse latest Browse all 171066

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>