Search Unity

Please help me understand ....

Discussion in 'Scripting' started by reset, Jul 15, 2009.

  1. reset

    reset

    Joined:
    May 22, 2009
    Posts:
    393
    I am trying to understand the problem a posted previously - see attached image. I understand one solution but just want to understand why my script below wont solve it.

    The radius of the sphere is 5 ie .5 collider radius * scale(10)

    The model does not end up on the perimeter of the sphere where I assume it would - just off of the center of the sphere :(

    perim is the "circle" around the model. I divide this by 360 degrees (deg) then offset this by the radius of the sphere (radcirc). Then convert this to radians which are applied via Sine and Cosine.


    function Start (){
    dist = Vector3.Distance(other.position, transform.position);
    perim = (2*dist)*Mathf.PI;
    deg = perim/360;
    radcirc = 5*deg;
    }

    function Update () {
    mover();}

    function mover () {
    rad = radcirc* Mathf.Deg2Rad;
    transform.position = Vector3(0,0,0) + Vector3(dist*Mathf.Sin(rad),0, dist*Mathf.Cos(rad));
    }
     

    Attached Files:

  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,603
    your problem, as far as I understand your description, is
    Code (csharp):
    1. transform.position = Vector3(0,0,0) + Vector3(dist*Mathf.Sin(rad),0, dist*Mathf.Cos(rad));
    1. Instead of Vector3(0,0,0) you likely wanted to use transform.position to use the current center of the model

    2. Vector3( ..., 0, ... ); potentially is missing * dist, you currently move the model 1 unit from 0,0,0 into the desired direction but not to the red point nor from the models current position
     
  3. Mike08

    Mike08

    Joined:
    Dec 29, 2005
    Posts:
    124
    Hi reset,

    Your problem is like dreamora says: that you start at the wrong point.

    You also have some mistakes in the mathematics above.

    Here is a version that should work.

    Code (csharp):
    1. function Start (){
    2. dist = Vector3.Distance(other.position, transform.position);
    3. perim = (2*dist)*Mathf.PI;
    4. radcirc = perim/360; // this is allready what you want to get cause you used the real distance and not the distance 1
    5. //radcirc = 5*deg; this line is not necessary.
    6. }
    7.  
    8. function Update () {
    9. mover();}
    10.  
    11. function mover () {
    12. //rad = radcirc* Mathf.Deg2Rad; radcirc is already rad you don't have to convert it
    13. other.position = transform.position + Vector3(dist*Mathf.Sin(radcirc),0, dist*Mathf.Cos(radcirc));
    14. }
    I didn't try it. But this should do now what you expected.

    By
     
  4. reset

    reset

    Joined:
    May 22, 2009
    Posts:
    393
    I actually want the model to start from anywhere - Vector3(0,0,0) is right, it is just a default position for the test.