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