- Home /
Scale Cube script Help? (In-game via mouse)
Hello
This are actually 2 problems that I have. I already work with a click and drag script for a cube in my scene, and a fixed camera (making it look like a 2d cube)
First I want to be able to change the size of this cube with the mouse wheel(scroll up=bigger, scroll down=smaller) With a max/min scale
And the second one is a bit trickier (so if you cannot help me it's ok), but i want to change the height of this cube also with click and dragging. As the click and drag script that i already have is for freely moving the cube, I already have an arrow model (a simple triangle over a cube) that its located on top of this cube. If its possible(maybe by a public variable?), I want to change the height of the cube by dragging this arrow away or closer to the cube, also with a max/min height for it (the cube)
Any ideas?, I'm not good with scripts sorry. I found this one on unity answers that could be close to what I need (this script attached to an object grow/shrink it's size with right/left click over them(raycasting))
Here is the script I found:
using UnityEngine;
using System.Collections;
public class Scale : MonoBehaviour {
public float maxScale = 10.0f;
public float minScale = 2.0f;
public float shrinkSpeed = 1.0f;
private float targetScale;
private Vector3 v3Scale;
void Start() {
v3Scale = transform.localScale;
}
void Update()
{
RaycastHit hit;
Ray ray;
if (Input.GetMouseButtonDown (0)) {
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit) && hit.transform == transform) {
targetScale = minScale;
v3Scale = new Vector3(targetScale, targetScale, targetScale);
}
}
if (Input.GetMouseButtonDown (1)) {
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit) && hit.transform == transform) {
targetScale = maxScale;
v3Scale = new Vector3(targetScale, targetScale, targetScale);
}
}
transform.localScale = Vector3.Lerp(transform.localScale, v3Scale, Time.deltaTime*shrinkSpeed);
}
}
Answer by gstock · Jul 02, 2014 at 04:06 AM
You can change the scale of the cube with a script like this one:
using UnityEngine;
using System.Collections;
public class ScaleCube : MonoBehaviour {
Vector3 minScale = new Vector3(0.1f,0.1f,0.1f);
Vector3 maxScale = new Vector3(3,3,3);
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float zoomValue = Input.GetAxis("Mouse ScrollWheel");
if (zoomValue != 0) {
transform.localScale += Vector3.one * zoomValue;
transform.localScale = Vector3.Max(transform.localScale, minScale);
transform.localScale = Vector3.Min(transform.localScale, maxScale);
}
}
}
Hey thanks! works well but the thing is it works everywhere. It must work only when the mouse is over the object. I tried to make a mix between the script I pasted and yours to implement raycast but i can't find the way to do it right. Do you know how to do it?
And how do I implement public variables to your script?, so I can change them easily. Like this ones from the script I posted:
public float maxScale = 10.0f;
public float $$anonymous$$Scale = 2.0f;
public float shrinkSpeed = 1.0f;
Sorry if this are too basic questions:/
Answer by fernandovt · Jul 03, 2014 at 01:26 AM
The answer to this problem for me was:
using UnityEngine;
using System.Collections;
public class ScaleCube : MonoBehaviour {
Vector3 minScale = new Vector3(0.1f,0.1f,0.1f);
Vector3 maxScale = new Vector3(3,3,3);
// Update is called once per frame
void Update () {
float zoomValue = Input.GetAxis("Mouse ScrollWheel");
if (zoomValue != 0 && CursorOnMe()) {
transform.localScale += Vector3.one * zoomValue;
transform.localScale = Vector3.Max(transform.localScale, minScale);
transform.localScale = Vector3.Min(transform.localScale, maxScale);
}
}
bool CursorOnMe() {
Ray cursorRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit[] cursorHits = Physics.RaycastAll(cursorRay);
for (int n = 0; n < cursorHits.Length; n++) {
if (cursorHits[n].collider == collider) {
//HIT!
return true;
}
}
return false;
}
}
Thanks for the help:)
Your answer
Follow this Question
Related Questions
Dragging an object smoothly 2 Answers
Bullet mouse control 2 Answers
Find object that enters trigger 1 Answer
getting the object that the running script is attached to 1 Answer
Disable script on other object 2 Answers