Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private var mouseX: float;
- private var mouseY: float;
- private var squareRoot: float;
- private var tankSpeed: float;
- var UnitVector: Vector3;
- var TranslationVector: Vector3;
- function Awake() {
- tankSpeed = 2.0;
- }
- function Update() {
- mouseX = Input.mousePosition.x;
- mouseY = Input.mousePosition.y;
- worldPos = camera.main.ScreenToWorldPoint(Vector3(mouseX, mouseY, 10));
- var xPosition = (tankObject.position.x - worldPos.x) * (tankObject.position.x - worldPos.x);
- var yPosition = (tankObject.position.y - worldPos.y) * (tankObject.position.y - worldPos.y);
- squareRoot = Mathf.Sqrt((xPosition + yPosition));
- MaxDistance = tankSpeed * Time.deltaTime;
- if (squareRoot > MaxDistance) {
- DistanceToGo = MaxDistance;
- } else {
- DistanceToGo = squareRoot;
- } // Unit vector is of length 1, but with the correct orientation
- UnitVector.x = (tankObject.position.x - worldPos.x)/squareRoot;
- UnitVector.y = (tankObject.position.y - worldPos.y)/squareRoot;
- // Get the vector at the desired length, which give the x/y movement required
- TranslationVector.x = UnitVector.x * DistanceToGo;
- TranslationVector.y = UnitVector.y * DistanceToGo;
- // Update the tank position with the calculated translation
- tankObject.position.x = tankObject.position.x + TranslationVector.x;
- tankObject.position.y = tankObject.position.y + TranslationVector.y;
- }
Advertisement
RAW Paste Data
Copied
Advertisement