Search Unity

Physics and repeating some events/forces

Discussion in 'Scripting' started by Ardeni, Aug 3, 2013.

  1. Ardeni

    Ardeni

    Joined:
    Jul 28, 2013
    Posts:
    31
    Hello guys and girls.

    I have the problem with repeating some events based on physics in my game.

    To better describe situation I prepare this art:
    $problem.jpg

    Description:

    After start object I'm using some forces (blue) to change its direction (after get the minimum distance to force - force range(grey)),
    it is always repetitive after scene start - Its ok :)

    Problem is when I'm trying to "rollback" position of object to the start in the same scene, each subsequent paths (red) are different.

    Any sugestions/questions?
     
  2. Cameron_SM

    Cameron_SM

    Joined:
    Jun 1, 2009
    Posts:
    915
    I have no idea what this is about, perhaps a web player demo would help?
     
  3. Ardeni

    Ardeni

    Joined:
    Jul 28, 2013
    Posts:
    31
  4. mousefist

    mousefist

    Joined:
    Jun 23, 2013
    Posts:
    21
    Alright, you want the rocket to go the same path every time after resetting, but the problem is that the path is slightly different each time.

    Would help if you could post some your code for this. =)

    Quick question: in your motion code (e.g. anything where Vector3 objects are calculated, for forces or translations), do you use Time.fixedDeltaTime or just Time.deltaTime? If you're using Time.deltaTime anywhere, try using Time.fixedDeltaTime everywhere, since deltaTime is dependent on framerate (which you'd expect to change once the game has run for a few seconds). You're apparently already using FixedUpdate() for all the motion related stuff, which is good.
     
    Last edited: Aug 3, 2013
  5. Ardeni

    Ardeni

    Joined:
    Jul 28, 2013
    Posts:
    31
    Yest, I'm using FiedUpdate without any Time.deltaTime

    There is one of my scripts, sorry about this rubbish. I know there's many unnecessary lines but I'm trying to find solution :/.
    Code (csharp):
    1. #pragma strict
    2. function FixedUpdate ()
    3. {
    4.    
    5.     RocketMesh.transform.position = Vector3.Lerp(transform.position, RocketCapsule.transform.position,1);
    6.     if (rigidbody.velocity != Vector3.zero)
    7.     {  
    8.         //RocketMesh.transform.rotation = Quaternion.LookRotation(rigidbody.velocity); 
    9.         var targetPoint = rigidbody.transform.position;
    10.         var targetRotation = Quaternion.LookRotation(rigidbody.velocity);
    11.         RocketMesh.transform.rotation = Quaternion.Slerp(RocketMesh.transform.rotation, targetRotation, 1);
    12.     }  
    13.     if (started == false)
    14.     {
    15.         if (Input.GetKey("s"))
    16.             {
    17.                 RocketStart();
    18.             }
    19.     }
    20.     if (started)
    21.     {
    22.         InvokeRepeating("ScanForTarget", 0, scanFrequency);
    23.         rigidbody.velocity = RocketMesh.transform.forward*25;
    24.     }  
    25.  
    26.     var rocketVel = rigidbody.velocity;
    27.     RocketSpeed = rocketVel.magnitude; 
    28.     //Debug.Log("speed:" + RocketSpeed  );
    29.     Debug.Log("gravityOn "+ rigidbody.velocity);
    30. }
    31.      
    32.  
    33.  
    34. function RocketStart()
    35. {  
    36.    
    37.     rigidbody.velocity = Vector3.zero;
    38.     Debug.Log("RocketStart");
    39.     rocketStartRotation = this.transform.localRotation;
    40.     rocketStartPosition = this.transform.position; 
    41.     started = true;
    42. }
    43.  
    44. function ScanForTarget()
    45. {
    46.         target = GetNearestTaggedObject(); 
    47.         GetDistanceNearestPlanet();
    48.        
    49.         var rotation = Quaternion.LookRotation(target.position - transform.position);
    50.         transform.rotation = Quaternion.Lerp(transform.rotation, rotation, 1);
    51. }
    52.  
    53. function GetNearestTaggedObject() : Transform {
    54.    
    55.         var nearestDistanceSqr = Mathf.Infinity;
    56.         var taggedGameObjects = GameObject.FindGameObjectsWithTag(searchTag);
    57.         var nearestObj : Transform = null;
    58.      
    59.         for (var obj : GameObject in taggedGameObjects)
    60.         {
    61.             var objectPos = obj.transform.position;
    62.             var distanceSqr = (objectPos - transform.position).sqrMagnitude;
    63.      
    64.             if (distanceSqr < nearestDistanceSqr) {
    65.                 nearestObj = obj.transform;
    66.                 nearestDistanceSqr = distanceSqr;
    67.             }
    68.         }    
    69.      return nearestObj;  
    70.  
    71. }
    72.  
    73. function GetDistanceNearestPlanet()
    74. {
    75.     if (started)
    76.         {
    77.         NearestPlanetDistance = Vector3.Distance(transform.position, target.transform.position);
    78.         Debug.Log(NearestPlanetDistance.ToString() + target + target.transform.localScale.x*15);
    79.         if (NearestPlanetDistance < target.transform.localScale.x*15)
    80.             {                  
    81.                 StartGravity();        
    82.             }
    83.         }
    84. }
    85.  
    86. function StartGravity()
    87. {
    88.     targetDirection = (RocketCapsule.transform.forward);
    89.     //var gravityForce = 0.000002;
    90.     gravityForce = 1/NearestPlanetDistance/37000;
    91.     rigidbody.AddForce((targetDirection*gravityForce));
    92.     //Debug.Log("gravityOn "+ rigidbody.velocity + "on target:"+ target.transform.position+ "speed:" + RocketSpeed +"gravityForce:"+ gravityForce );
    93.    
    94.  
    95. }
    96. function OnCollisionEnter(collision : Collision)
    97. {
    98. if (collision.collider.name == "SphereFinish")
    99.     {
    100.         //Debug.Log("target to spherefinish");
    101.         Finish();      
    102.     }
    103. else{
    104.    
    105.     Debug.Log("kolizja");
    106.     started = false;   
    107.     RestartLevel.SetActive(true);
    108.     }  
    109. }
    110. function Finish()
    111. {
    112.     Debug.Log("Finish");
    113.     rigidbody.velocity = Vector3.zero;
    114.     NextLevel.SetActive(true);
    115.     RocketMesh.SetActive(true);
    116.     started = false;
    117.    
    118. }
    119. function RestartStartPosition()
    120. {
    121.     Debug.Log("Restart");
    122.     GameObject.Find("Screen_Collision").collider.enabled=false;
    123.     Button_Start_Rocket.SetActive(true);
    124.     RocketMesh.SetActive(true);
    125.     started = false;
    126.     rigidbody.velocity = Vector3.zero;
    127.     this.transform.position = rocketStartPosition;
    128.     this.transform.localRotation = rocketStartRotation;
    129.     RocketMesh.transform.position = rocketStartPosition;
    130.     RocketMesh.transform.localRotation = rocketStartRotation;  
    131.    
    132.    
    133.     RocketMesh.transform.rotation = Quaternion.LookRotation(RocketPlayerStartDirection.normalized);
    134.     RocketPlayerStartDirection = Vector3(0.5,0.5,0);
    135.    
    136.    
    137. }
     
    Last edited: Aug 8, 2013