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 lollie009 · Sep 26, 2013 at 05:07 PM · c#mouseclickresizerescale

Scaling objects in C#

I would like to be able to make an object smaller by clicking on it with the left mouse button and bigger with right mouse button. The problems with this code are: 1. it doesn't allow to scale down unless its been scaled up 2. if there are multiple objs, they all get affected once they've been clicked on. I've tried a few different ways to do it and I'm guessing it's something to do with the resize bool. Your help is much appreciated

using UnityEngine; using System.Collections;

public class Scale : MonoBehaviour { public GameObject obj;

 private float targetScale;
 public float maxScale = 10.0f;
 public float minScale = 2.0f;
 
 public float shrinkSpeed = 1.0f;
 
 private bool resizing = false;

void OnMouseDown() { resizing = true; }

 void Update()
 {
     if (resizing)
     {
          if (Input.GetMouseButtonDown(1)) 
         {
             targetScale = maxScale;
             //Resize();
             
         }
          if (Input.GetMouseButtonDown(0))
         {
             targetScale = minScale;
             //Resize();
             
         }
         
         obj.transform.localScale = Vector3.Lerp(obj.transform.localScale, new Vector3(targetScale, targetScale, targetScale), Time.deltaTime*shrinkSpeed);
         
         Debug.Log(obj.transform.localScale);
         
         if (obj.transform.localScale.x == targetScale)
         {
         resizing = false;
             Debug.Log(resizing);
         }
 }
 }

}

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
2
Best Answer

Answer by robertbu · Sep 26, 2013 at 05:40 PM

OnMouseDown() only captures a left button click, so you right button click was only working if 'resizing' had already been set to true by a left button click. To solve this problem, you'll have to move to a raycast model. Below is a bit of code implementing a 3rd party raycast model. That is, the script only has to exist in one place, not on every object. But the one drawback is that if you click on a second object before the first one has completed growing/shrinking, the growing/shrinking of the first object stops. I'm not sure of your purpose or need, so consider this example code:

 using UnityEngine;
 using System.Collections;
 
 public class Scale : MonoBehaviour {
     
 private Transform tr;
 
     public float maxScale = 10.0f;
     public float minScale = 2.0f;
     public float shrinkSpeed = 1.0f;    
     
     private float targetScale;
     private Vector3 v3Scale;
         
     
     void Update()
     {
         RaycastHit hit;
         Ray ray;
             
         if (Input.GetMouseButtonDown (0)) {
             ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(ray, out hit)) {
                 tr = hit.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)) {
                 tr = hit.transform;
                 targetScale = maxScale;
                 v3Scale = new Vector3(targetScale, targetScale, targetScale);
             }
         }
      
         if (tr != null)
             tr.localScale = Vector3.Lerp(tr.localScale, v3Scale, Time.deltaTime*shrinkSpeed);
     }
 }
Comment
Add comment · Show 5 · 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 lollie009 · Sep 26, 2013 at 06:15 PM 0
Share

Hi Robert, Thanks for your quick response. In the inspector what would I set the tr as?

Basically, it's a first person puzzle game where you have a gun that resizes objects. For example make a boulder smaller to reveal an entrance to the cave. Grow a plant of wood to cross the river etc.

avatar image robertbu · Sep 26, 2013 at 06:31 PM 0
Share

tr should have been private and it gets set by the code.

avatar image lollie009 · Sep 26, 2013 at 06:33 PM 0
Share

so do I set tr as the object itself?

It seems to be shrinking as soon as I press play and it shrinks all the way down

avatar image robertbu · Sep 26, 2013 at 06:36 PM 1
Share

Given your expanded description, you might want a script that you attach to each game object so that you can set the maxScale and $$anonymous$$Scale independently for each object. Here is a modified version of the script above that needs to be attached to each object that you want to grow or shrink:

 using UnityEngine;
 using System.Collections;
 
 public class Scale : $$anonymous$$onoBehaviour {
     
     public float maxScale = 10.0f;
     public float $$anonymous$$Scale = 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.Get$$anonymous$$ouseButtonDown (0)) {
             ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(ray, out hit) && hit.transform == transform) {
                 targetScale = $$anonymous$$Scale;
                 v3Scale = new Vector3(targetScale, targetScale, targetScale);
             }
                 
         }
             
         if (Input.Get$$anonymous$$ouseButtonDown (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);
     }
 }
avatar image lollie009 · Sep 26, 2013 at 06:54 PM 0
Share

This works beautifully.. Thank you thank you thank you =)

avatar image
1

Answer by Entairex1 · Sep 26, 2013 at 05:50 PM

 private Vector3 targetScale;
 public float maxScale = 10.0f;
 public float minScale = 2.0f;
 public float shrinkSpeed = 1.0f;
 void Update(){
     if (Input.GetMouseButtonDown(1))
         targetScale.Set(maxScale,maxScale,maxScale);
     if (Input.GetMouseButtonDown(0))
         targetScale.Set(minScale,minScale,minScale);
     if (transform.localScale != targetScale)
         transform.localScale = Vector3.Lerp(transform.localScale, targetScale, Time.deltaTime*shrinkSpeed);
 }

Hope this helps =)

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 lollie009 · Sep 26, 2013 at 06:26 PM 0
Share

Thanks for this. This sort of works except at the beginning, the object starts shrinking down to nothing. If I then hit the rmb it scales to maxScale and then lmb scales down to $$anonymous$$Scale. Except it resizes even when I'm not clicking on the object

avatar image Entairex1 · Sep 27, 2013 at 05:03 PM 0
Share

Ahhh I thought you wanted it to always be either $$anonymous$$- or maxscale

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

16 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

OnMouseDown gets both Collider and UI Button 1 Answer

Texture not showing 1 Answer

Click On object that has a larger collider 1 Answer


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