1
\$\begingroup\$

Unity3d. I am trying to move an object with Camera around a fixed terrain (Mountain), while staying an set distance from its edge. I have the camera set to lookAt the mountain, and I want to circle the mountain. I was thinking about sine wave for circular motion but its not an exact circle as I need to move in or out to maintain the fixed distance from the mountains edges.

It looks like I might need transform.RotateAround(..), but what about the adjusting to maintain a fixed distance from the edge of the uneven surface while maintaining the circular motion.

    transform.RotateAround (Vector3.zero, Vector3.up, 20 * Time.deltaTime);

Update: Here is my current script for circling around mountain as first person object:

function Update () { var mountain : GameObject; mountain = GameObject.Find("QuantumCold_B"); transform.RotateAround(mountain.transform.position, Vector3.up,20 * Time.deltaTime);

}

Unfortunately there are two problems:

1) The first person object appears to be falling or maybe its the main camera, but in any even there is a game screen looks like its falling below the mountain. 2) it is the mountain that seems to be rotating. Do I need to switch the main camera to be the first person camera. Why does the mountain appear to be rotating?

\$\endgroup\$
  • \$\begingroup\$ You should post some code to work with and not to expect getting all done for you. Show what you have and how you have tried to do it. That would make answering easier. \$\endgroup\$ – Katu Jul 22 '13 at 18:31
  • \$\begingroup\$ I don't think you need to show much code. I do think you need to define what you mean by the "edge" of the mountain. It's trivial to set up orbiting around a single fixed point, but it sounds like what you want is to orbit around a contour such that you are always a fixed distance from that contour, so it's important to indicate what that contour is. \$\endgroup\$ – Josh Jul 22 '13 at 18:34
  • \$\begingroup\$ Its a mesh called from Quantum Cold. \$\endgroup\$ – gitright Jul 22 '13 at 22:30
  • \$\begingroup\$ I don't really like using RotateAround() because that only works right when the target is still. I mean, in this case the target is still, but then you'll get used to doing it this way. You can also orbit the camera by storing an offset position, multiplying that offset by the rotation to get a rotated offset, and then add the rotated offset to the target position. The code (with _rotY incrementing every frame) Quaternion rotation = Quaternion.Euler(0, _rotY, 0); transform.position = target.position - (rotation * _offset); transform.LookAt(target); \$\endgroup\$ – jhocking Jul 18 '14 at 11:36
  • \$\begingroup\$ also, you shouldn't use GameObject.Find() every frame. Instead do it once in the Start() function and then store the result in a variable. \$\endgroup\$ – jhocking Jul 18 '14 at 11:49
3
\$\begingroup\$

After rotating the camera position, slide the camera position toward or away from the mountain by casting a ray from the camera position to the center of the mountain. Choose a ray intersect test that returns a distance value. Test that ray against all triangles (or optimize with some sort of hierarchical system) of the mountain mesh and whichever triangle you hit whose Dot(triangleNormal, ray.direction) <= zero and has the least absolute value, use that test result to set the cam position like this:

cameraPosition += -ray.Direction * (distanceIwantMyCameraToBe - intersectionTestResult);
\$\endgroup\$
  • \$\begingroup\$ Ok. I'm going try this. \$\endgroup\$ – gitright Jul 22 '13 at 20:31
  • \$\begingroup\$ please view my update above. I am still trying to get the fpo to rotate around the mountain at all. Also how do I make the first person object the main camera? \$\endgroup\$ – gitright Jul 23 '13 at 0:54
  • \$\begingroup\$ I've actually never used unity so I don't think I'm much help with this one. Sounds like someone Would have to understand what your code is trying to do and what it is actually doing to help. \$\endgroup\$ – Steve H Jul 23 '13 at 1:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.