I need to know how to make a rectangle one click and drag on the screen and make it a rigid body without gravity. Please help me with this
3 Answers
3you could Instantiate a default prefab where your mouse is and make the Rigidbody.useGravity false
I m creating the game for android n iPhone. All touch scren models. How to get the mouse's position? Also I want draw the rectangle depending on the drag's length and position.
– GaneshcsePrototype Script
using UnityEngine;
using System.Collections;
public class DragCreate : MonoBehaviour {
public Transform cubeObject;
public Rigidbody bead;
public float absoluteZPosition = 0f;
public float absoluteZScale = 0.5f;
private Transform activeObject;
private Vector3 startPosition;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 position = Camera.main.ScreenToWorldPoint( Input.mousePosition );
position.z = absoluteZPosition;
// Left-Click
if( Input.GetMouseButtonDown(0) ) {
activeObject = Instantiate( cubeObject, position, Quaternion.identity ) as Transform;
startPosition = position;
Form ( activeObject, startPosition, position );
}
//Right-Click
if( Input.GetMouseButtonDown(1) ) {
Instantiate( bead, position, Quaternion.identity );
}
if( Input.GetMouseButton(0) ) {
Form ( activeObject, startPosition, position );
}
}
private void Form( Transform shape, Vector3 start, Vector3 end ) {
Vector3 scale = end - start;
scale.z = absoluteZScale;
Vector3 pos = start + scale/2;
pos.z = absoluteZPosition;
shape.position = pos;
shape.localScale = scale;
}
}
Note
A rectangle of scale (0,0, absoluteZScale) will be create if the user do a quick click. You will need to do some extra for that (for example, Destroy rectangles that are too small)
This script only works with Orthographic camera.
The cubeObject is a standard cube, nothing is added at all, I just convert it to a prefab. The bead setup is as follow: [8751-screenshot.5.png|8751] Screenshot of the script in action: [8752-screenshot.6.png|8752]
Are you, by any chance, new to Unity? The UnassignedReferenceException is one of the first exceptions you learn to solve in Unity. Read the first line of the error message very carefully and figure it out.
You should assign the cube to the gameObject this script is attached to. Like so: [8720-screenshot.7.png|8720]
– Chronos-Lif you want it without gravity just uncheck the gravity box in the rigidbody options
## Part 1 ## What I understood from your question: - Create a rectangle from a click and drag motion - The rectangle size and position is calculated from 2 point: start-drag-point and end-drag-point (Similar to a mouse-drag highlight) Is this what you want? Correct me if I am wrong. ## Part 2 ## Depends on the solution for part 1, you can either do this as a prefab (creating rigidbody component in editor) or during runtime ([Rigidbody.useGravity)][1]. [1]: http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-useGravity.html
– Chronos-Li came to know tat onMouseDrag won't work in iPhone then wat function can i use? if the player click and drag at some points in the screen a rectangle shud be formed in those points. how to do it ? Can u explain me or provide me the code to do the part 1?
– GaneshcseI have no experience in touch input. I am quite confident that I can whip-out a calculation function that work with MouseInput, but you will have to figure out how to convert from MouseInput to TouchInput yourself. If you are okay with it, I will post the calculation function here as answer. For touch screen, please refer to these documentation: - [Input.touches][1] - [Touch.phase][2] [1]: http://docs.unity3d.com/Documentation/ScriptReference/Input-touches.html [2]: http://docs.unity3d.com/Documentation/ScriptReference/Touch-phase.html
– Chronos-LY aokay pls provide me the calculation ! Also the tough part is to draw a rectangle in tat positions and make it as a rigid body so that tiny balls falling on the rectagle created may get deviated .
– GaneshcseYou just need to set the cube as a normal collider. While you add the rigidbody to the little balls, the simulation will work out fine.
– Chronos-L