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. But, if I scale the player before running the script by manually changing the scale of the player, it works fine.
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
Your answer
Follow this Question
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