- Home /
Scaling a rotated object in global Y
In my game I am having a bouncing ball over platforms / sidescroller, and during it's bounces it rotates as it's having a rigidbody, with bounce material.
Now I want to build with a raycasthit, that if the ball bounces onto something underneath it, that it is going to squish, or be scaled alittle done in 1 axes. If the ball's local Y is similar to the global Y, this is no problem and is working perfectly, though when the ball rotates around it's axes I want the ball still to be scaled in global Y coordinates, no matter how it is being rotated. Does anyone have any idea how to fix this, or a workaround?
using UnityEngine;
using System.Collections;
public class raycasthit : MonoBehaviour {
RaycastHit hit = new RaycastHit();
Vector3 dir;
public float distance = 3;
public float scaleTo = 0.90f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
dir = new Vector3(0,-1,0);
Debug.DrawRay(transform.position,dir*distance,Color.green);
if(Physics.Raycast(transform.position,dir,out hit,distance)){
transform.localScale = new Vector3(scaleTo, scaleTo, scaleTo);
}
else{
transform.localScale = new Vector3(1f, 1f, 1f);
}
}
}
Thanks in advance!
Answer by robertbu · May 08, 2014 at 10:28 PM
One hackish solution is to make the ball a child of an empty game object. The empty game object must have rotation of (0,0,0). Scale the empty game object's 'y' to scale the ball on the global 'y'.
Your answer
![](https://koobas.hobune.stream/wayback/20220613144602im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Global movement 0 Answers
Character Customization demo - Problem with scaling and rotation 2 Answers
Making Quaternion.RotateTowards finish at exact time Vector3.MoveTowards finishes in a coroutine? 1 Answer
Parallel animation play using iTween 1 Answer
Scaling parent, collider of child goes wrong when rotated 2 Answers