Instantiating Platforms between two Vectors

Hi,

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;
Debug.Log(timer);
}
}

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

If i'm honest it's not surprising to me that your question didn't get an answer yet. - Your code isn't formatted. - You never really asked a question. You just explained what you already did. - This question looks like a "make a script for me"-question. You should: - Read this page : http://answers.unity3d.com/page/newuser.html - Please watch : http://video.unity3d.com/video/7720450/tutorials-using-unity-answers

–

Please reformat your question. First use the code button to make your code be displayed as one. Then you remove the parts which are in there 2 times and also the parts which aren't totally necessary to the question. Then you may get help. At least i will come back if you do so and will see what i can do. Just don't post another question about the same topic. Rather edit this one and put the text from the new one (which i didn't approved but also am not able to reject because unity answers is being weird lately with the moderation).

–

Apologies for the scrappy post @ExTheSea I have edited the code and question so that hopefully it is more obvious what I am trying to achieve, I would love any more suggestions.

–

Didn't robertbu's post help you? I had a look at it and it seemed like it's the perfect solution for your problem.

–

@ExTheSea please see the comment I just posted under his script Ive found some issues with it :/

–

1 Answer

1

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 
}

I'm away from my computer. I'll take a second look Sunday evening. The CreatePlane script above will allow you to create a plane with a vertical orientation. You might want to consider using a thin cube with a box collider rather than a plane for your platforms. It might do a better job of collision detection.

–