Advertisement
Danisuper

AI Lerp (2D)

Aug 29th, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.67 KB | None | 0 0
  1.     //I modified the code in the Update() in the AISimpleLerp code
  2.     //that makes the object move to make it work correctly with rigidbodies
  3.  
  4. void FixedUpdate()
  5.     {
  6.         if (canMove)
  7.         {
  8.             Vector3 direction;
  9.             Vector3 nextPos = CalculateNextPosition(out direction);
  10.             //the direction where it has to go
  11.             Vector3 move_dir = (nextPos - tr.position);
  12.  
  13.             // Rotate unless we are really close to the target
  14.             if (enableRotation && direction != Vector3.zero)
  15.             {
  16.                 if (rotationIn2D)
  17.                 {
  18.                     /*float angle = Mathf.Atan2(direction.x, -direction.y) * Mathf.Rad2Deg + 180;
  19.                     Vector3 euler = tr.eulerAngles;
  20.                     euler.z = Mathf.LerpAngle(euler.z, angle, Time.deltaTime * rotationSpeed);*/
  21.                     //the angle the rigidbody (rb) has to rotate
  22.                     //float angle = Mathf.Atan2(nextPos.x, nextPos.y) * Mathf.Rad2Deg;
  23.                     // rb.MoveRotation(angle*Time.fixedDeltaTime);
  24.                     //it uses transform because I couldn't get it working with the rigidbody API
  25.                     transform.rotation = UtilityFunctions.FaceTo(nextPos, tr.position);
  26.  
  27.                 }
  28.                 else
  29.                 {
  30.                     Quaternion rot = tr.rotation;
  31.                     Quaternion desiredRot = Quaternion.LookRotation(direction);
  32.                     //tr.rotation = Quaternion.Slerp(rot, desiredRot, Time.deltaTime * rotationSpeed);
  33.                 }
  34.             }
  35.             rb.MovePosition(tr.position + move_dir.normalized * speed * Time.fixedDeltaTime);
  36.         }
  37.  
  38.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement