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 ergiomarxD · Mar 01, 2014 at 06:24 PM · scale

Scale in and out

I've use this code but it only does the scale out function :3 can somebody help me to fix this so it could contain the scale in function :3

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 · Show 4
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 robertbu · Mar 01, 2014 at 06:30 PM 0
Share

Your code works perfectly for me scaling both in and out. Note that your starting scale for the object should be somewhere between your $$anonymous$$Scale and maxScale. What is happening when you run this code?

avatar image mclankyman · Mar 01, 2014 at 06:39 PM 0
Share

Can't work out why that wouldn't work.

Could you not change the float values to vector 3 values as you are perfor$$anonymous$$g universal scaling.

So then just put targetScale = $$anonymous$$Scale without having to create a new vector each time.

Not sure if this will help. Oh and put an else if statement just to avoid messing up the target value.

avatar image ergiomarxD · Mar 01, 2014 at 06:48 PM 0
Share

When I touch the object it only keeps on getting bigger and bigger.

avatar image robertbu · Mar 01, 2014 at 06:51 PM 0
Share

It worked perfectly for me. Test your code in a controlled environment.

  • Start a new scene.

  • Add a cube.

  • Scale your cube to (5.0, 5.0, 5.0).

  • Add your script (don't do any Inspector changes to the values).

  • Run the app and test

This is what I did, and everything was fine. Note if you are looking at values in the Inspector, Lerp() used this way can take a very long time to reach the specified size, though it will get close to the size very quickly.

0 Replies

· Add your reply
  • Sort: 

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

21 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

Related Questions

A node in a childnode? 1 Answer

Cant change the depth on my map without it bugging out how do I fix this? 0 Answers

Scaling and performance? 0 Answers

How to scale an object between two values over distance 1 Answer

Android/IOS First Person Shooter 0 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