- Home /
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 : http://www.youtube.com/watch?v=aiOzPCplYY0)). 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 :)
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
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 :/
Answer by robertbu · May 02, 2013 at 07:19 PM
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
}
@robertbu Thanks for the code but unfortunately I have tried what you suggested and have found 2 major issues:
To make my gameobjects 2D I have removed the z axis and set the rotation of everything rendered to 270 degrees on the x axis if there is a better way to make things 2D then please say so but with your code, "transform.rotation = Quaternion.FromToRotation(Vector3.right, angle);" This sets the right angle required but I also need to set the x axis to equal 270 degrees so that it doesnt face down which I am unsure how to do.
Currently in order that the platforms collide with the other objects when I instantiate them I set the z axis distance to 84.5 units away from the camera which is the required distance however I cannot achieve this if I just simply do "v3End-v3Start"
Therefore currently although this has been helpful, especially with the Quaternion.FromToRotation function this has not helped me reach my intended script.
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.
I tested the above code with a vertical 1 x 1 plane using the CreatePlane script. It works fine. In order to set the 'Z' position to 84.5, you have to set the z position of both v3Start and v3End to 84.5. Given that the plane mesh is facing out and your objects are co$$anonymous$$g down, you might have to use a box collider on your plane mesh to get the collisions to work right. Note the 'v3End - v3Start' calculates a vector between the two relative to v3Start and is not the position. The actual position calculation is half way between v3Start and v3End, so if both Z coordinates are the same, then the Z position will match both v3Start and v3End.
Your answer
Follow this Question
Related Questions
Having trouble instantiating a gameobject with rotation 0 Answers
Scale issue returns when object is instantiated 1 Answer
Instantiating prefab of object like it was in the scene 0 Answers
Scaled sprite distorts on rotation in different resolutions 0 Answers
Instantiated object not rotating around the center 0 Answers