Instantiating Platforms between two Vectors


For a while now Ive been working on a 2D game which requires the player, to create multiple miniature platforms when they drag the mouse while holding it down (to explain this easier here is a youtube video I found after the initial idea doing the same thing except in 3D (just look at the platform creation : Draw Physics Rope Game - YouTube)). I have already searched for previously asked solutions for this but none seem to really appear so hopefully someone can help me with this!

The main question I want answered is how I would go about writing a script to:

  1. when the player first clicks down set the startPosition to the mouse coordinates
  2. Start the timer
  3. When the timer reaches 1 second (this is just for testing)set the second coordinate to the endPosition
  4. calculate the the distance between the 2 points
  5. calculate the necessary scale up size (not currently working)
  6. Instantiate in world coordinates the platform prefab (just a basic plane rotated to 270 degrees on the x axis to be rendered in 2D)
  7. set the scale to the discovered distance
  8. set the starting point to the current end point to then create another platform
  9. restart the timer to repeat :slight_smile:

My current relevant code:

var plain : GameObject;
private var cameraDistance : float = 84.5;
private var platformStart : Vector2;
private var platformEnd : Vector2;
private var timer : float = 0.0;
private var startTimer : boolean = false;

function Start () {

function Update () {

if (Input.GetButtonDown("Fire1") {
platformStart = Input.mousePosition;
startTimer = true; 

if (Input.GetButtonUp("Fire1")){
startTimer = false;
timer = 0;

if (timer >= 1) {
platformEnd = Input.mousePosition;
platformGenerate(platformStart, platformEnd);
timer = 0.0;
startTimer = false;

if (startTimer) {
timer += Time.deltaTime;

function platformGenerate (start : Vector2, end : Vector2) {
var offsetX = end.x - start.x;
var offsetY = end.y - start.y;
var distance = offsetX + offsetY;
var scale = distance / 2;
var position : Vector3 = Vector3(start.x + (offsetX / 2), start.y + (offsetY / 2), cameraDistance);
var placement = Camera.main.ScreenToWorldPoint(position); 

Instantiate(plain, placement, plain.transform.rotation);
plain.transform.localScale.x = scale;
startTimer = true;
platformStart = platformEnd;

Please note all of this code does function but the main issues i’m having are:

  1. I cannot seem to calculate the angle between the 2 points to then set the z axis rotation of the “plain” to. Please also note that because I am creating multiple prefabs I need a script inside each one to set the scale and rotation to rather than the actual prefab itself.

  2. I need to set the local scale of the prefab plane to equal the distance between the 2 points to fill the space, what you I need to do to achieve this as right now the current code just creates ultra long versions of the current plane prefab…

Any help would be hugely appreciated! Thanks GrumpyLlama

Here is a bit of code that will position, rotate and scale an object between two vectors. I tested it with a block. Objects that have other orientations (like a cylinder) will require something other than ‘Vector3.right’ in the rotation calculation. Scale assumes that if the scale is set to (1,1,1), the size of the object will be one unity on a side. The default plane is 10 units on a side. You can use the CreatePlane script to create a plane 1 unit length/width.

#pragma strict

var v3A = Vector3(-1,1,0);
var v3B = Vector3(1,-1,0); 

function Start () {
	PositionSize(v3A, v3B);

function PositionSize(v3Start : Vector3, v3End : Vector3) {
	var v3Pos = (v3End-v3Start)/2.0f + v3Start;  // Position
	transform.position = v3Pos;
	var v3 = v3End-v3Start;    // Rotation
	transform.rotation = Quaternion.FromToRotation(Vector3.right, v3);
	transform.localScale = new Vector3(v3.magnitude, 0.1, v3.magnitude);  // Scale