Advertisement
Guest User

Untitled

a guest
Jan 17th, 2012
107
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. private var mouseX: float;
  2. private var mouseY: float;
  3.  
  4. private var squareRoot: float;
  5. private var tankSpeed: float;
  6. var UnitVector: Vector3;
  7. var TranslationVector: Vector3;
  8.  
  9. function Awake() {
  10.     tankSpeed = 2.0;
  11. }
  12.  
  13. function Update() {
  14.     mouseX = Input.mousePosition.x;
  15.     mouseY = Input.mousePosition.y;
  16.  
  17.     worldPos = camera.main.ScreenToWorldPoint(Vector3(mouseX, mouseY, 10));
  18.    
  19.     var xPosition = (tankObject.position.x - worldPos.x) * (tankObject.position.x - worldPos.x);
  20.     var yPosition = (tankObject.position.y - worldPos.y) * (tankObject.position.y - worldPos.y);
  21.    
  22.     squareRoot = Mathf.Sqrt((xPosition + yPosition));
  23.  
  24.     MaxDistance = tankSpeed * Time.deltaTime;
  25.     if (squareRoot > MaxDistance) {
  26.         DistanceToGo = MaxDistance;
  27.     } else {
  28.         DistanceToGo = squareRoot;
  29.     } // Unit vector is of length 1, but with the correct orientation
  30.     UnitVector.x = (tankObject.position.x - worldPos.x)/squareRoot;
  31.     UnitVector.y = (tankObject.position.y - worldPos.y)/squareRoot;
  32.    
  33.     // Get the vector at the desired length, which give the x/y movement required
  34.     TranslationVector.x = UnitVector.x * DistanceToGo;
  35.     TranslationVector.y = UnitVector.y * DistanceToGo;
  36.     // Update the tank position with the calculated translation
  37.     tankObject.position.x = tankObject.position.x + TranslationVector.x;
  38.     tankObject.position.y = tankObject.position.y + TranslationVector.y;
  39. }
Advertisement
RAW Paste Data Copied
Advertisement