Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by fernandovt · Jul 02, 2014 at 02:39 AM · objectdragresizemousewheel

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);
         }
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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);
         }
 
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fernandovt · Jul 02, 2014 at 05:00 AM 0
Share

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?

avatar image fernandovt · Jul 02, 2014 at 05:25 AM 0
Share

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:/

avatar image
0

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:)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges