Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by HydraulicKangarooJay · Mar 27, 2020 at 02:30 AM · scaleruntimerigged

Scaling Rigged Character at Runtime Through Script?

Me and my friends have been working on a game, and we're all relatively new to unity. I have a script that scales the player up and down after pressing certain keys, which worked fine until we rigged the character. Now, whenever I scale the character, this happens. alt text But, if I scale the player before running the script by manually changing the scale of the player, it works fine.

alt text This is the code for the scaling, the playermovement variables and stuff are just the rest of the controls for the game, all of the scaling happens in this script.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using TMPro;
 
 public class ScalePlayer : MonoBehaviour
 {
     private Vector3 currScale = new Vector3(1f, 1f, 1f);
     private Vector3 targetScale = new Vector3(1f, 1f, 1f);
 
     private Vector3 LifeIndOrigPos;
     private Vector3 LifeIndOrigScale;
 
     public readonly float playerHeight = 9.496286f;
 
     private readonly float scaleAmount = 2f;
     private float prevScale;
     private readonly float[] bounds = { 0.25f, 4f };
     private readonly float gravCache = -60f;
 
     public LifeIndicator lifeInd;
 
     public TextMeshPro sizeInd;
     public Interaction interactor;
 
     private void OnDestroy()
     {
         Physics.gravity = Utils.WithOnlyY(gravCache);
     }
 
     private void OnDrawGizmos()
     {
         Gizmos.color = Color.red;
         Gizmos.DrawSphere(transform.position, 0.1f);
     }
     
 
     private void UpdateCurrScale()
     {
         Physics.gravity = Utils.WithOnlyY(gravCache * currScale.x);
         currScale = targetScale;
         sizeInd.text = currScale.x.ToString() + "x";
         interactor.SetScale(currScale.x);
     }
 
     private void UpdateLifeInd(Vector3 origScale)
     {
         lifeInd.transform.localScale = origScale * GetCurrScale();
     }
 
     public void SetScale(int scale)
     {
         Vector3 scaleVector = new Vector3(scale, scale, scale);
         transform.localScale = scaleVector;
         targetScale = scaleVector;
         UpdateCurrScale();
         UpdateLifeInd(LifeIndOrigScale);
     }
 
     public void ScaleUp(int scaleCount = 1)
     {
         UpdateCurrScale();
         if (GetCurrScale() < bounds[1])
         {
             Vector3 targetScaleTemp = new Vector3();
             while (scaleCount > 0)
             {
                 targetScaleTemp = currScale * scaleAmount;
                 scaleCount--;
             }
 
             Ray ray = new Ray(transform.position, Vector3.up);
             if (!Physics.Raycast(ray, playerHeight * targetScaleTemp.x, 1 << 12))
             {
                 targetScale = targetScaleTemp;
             }
             else
             {
                 lifeInd.UnAbleFlash();
             }
 
             UpdateCurrScale();
             UpdateLifeInd(LifeIndOrigScale);
         }
         else
         {
             lifeInd.UnAbleFlash();
         }
     }
 
     public void ScaleDown(int scaleCount = 1)
     {
         UpdateCurrScale();
         if (GetCurrScale() > bounds[0])
         {
 
             while (scaleCount > 0)
             {
                 targetScale = currScale / scaleAmount;
                 scaleCount--;
             }
             UpdateCurrScale();
             UpdateLifeInd(LifeIndOrigScale);
         }
         else
         {
             lifeInd.UnAbleFlash();
         }
     }
 
     public float GetCurrScale()
     {
         UpdateCurrScale();
         return currScale.x;
     }
 
     public bool HasScaledInFrame()
     {
         if (Input.GetKey(",") || Input.GetKey(".")) return true;
         else return false;
     }
 
     private void LerpScale(float speed)
     {
         transform.localScale = Vector3.Lerp(transform.localScale, targetScale, Time.deltaTime * speed);
     }
     
     // Start is called before the first frame update
     void Start()
     {
         LifeIndOrigScale = lifeInd.transform.localScale;    //Set cache variables
         LifeIndOrigPos = lifeInd.transform.position;
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKeyDown(","))  //If downSize key pressed, downSize
         {
             ScaleDown();
         }
         else if (Input.GetKeyDown(".")) //If upSize keyPressed, upSize
         {
             ScaleUp();
         }
 
         LerpScale(10f);             //Lerp the scale at a speed of 10x
         if (prevScale != targetScale.x)
             prevScale = targetScale.x;  //update prevScale
     }
 }
 

Does anyone know why this would happen? Thanks

what.png (12.3 kB)
whoops.png (107.6 kB)
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

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

200 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 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 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 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 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 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 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 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 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 avatar image avatar image

Related Questions

Why my prefab's Scale is changed to 0 at runtime? 1 Answer

LocalScale is not taking effect at runtime 1 Answer

Sprites and Shaders : problem with lighting 2 Answers

Mirroring a gameObject 0 Answers

Manipulating Objects in callback 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