Advertisement
shadowplaycoding

P001_PositionMaths

Sep 12th, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.04 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public static class PositionMaths
  6. {
  7.     // This method creates a random polar coordinate then converts and returns it as a Cartesian coordinate
  8.     public static Vector3 RandomPolarPosition(float minRad, float maxRad){
  9.  
  10.         float distance = Random.Range(minRad, maxRad);
  11.         float angle = Random.Range(0, (2 * Mathf.PI));
  12.  
  13.         Vector3 cartPosition = PolarToCart(distance, angle);
  14.  
  15.         return cartPosition;
  16.  
  17.     }
  18.  
  19.     // This method converts a distance and angle (polar coordinates)
  20.     public static Vector3 PolarToCart(float distance, float angle){
  21.  
  22.         float x = distance * Mathf.Cos(angle);
  23.         float y = 0.0f;
  24.         float z = distance * Mathf.Sin(angle);
  25.  
  26.         Vector3 cartPosition = new Vector3(x, y, z);
  27.  
  28.         return cartPosition;
  29.  
  30.     }
  31. }
  32.  
  33. /*
  34. Copyright Shadowplay Coding 2019 - see www.shadowplaycoding.com for licensing details
  35. Removing this comment forfits any rights given to the user under licensing.
  36. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement