- Home /
Problems with scale and rotation
Hi, guys!
I'm making an array of functions which contains the changes that occur to the array of objects. Everything works, but not the way I want to. hehe))) I would like to change the scale to less amount. But when I use floats (instead of 2), the script gives me errors. The rotation is supposed to be 20 (50) degrees around a transform's local Y axes. But it rotates 180 degrees most of the time.
If anyone can give me a clue how to make it work properly? Thank you!
THE CODE:
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class ChangeBlindness : MonoBehaviour {
public List<GameObject> currentObjects;
void Start () {
currentObjects = GetComponent<GenerateRoom> ().currentObjects;
}
void Update () {
if (Input.GetKeyDown (KeyCode.G)) {
int changeFunction = Random.Range (1, 7);
int randomArrayObj = Random.Range (0, currentObjects.Count);
Changes (changeFunction, currentObjects [randomArrayObj]);
}
}
void ChangePos1 (GameObject gameObject){
gameObject.transform.position = new Vector3 (gameObject.transform.position.x+1F, gameObject.transform.position.y,gameObject.transform.position.z);
}
void Rotate1 (GameObject gameObject){
gameObject.transform.rotation = new Quaternion (gameObject.transform.rotation.x, gameObject.transform.rotation.y+20F, gameObject.transform.rotation.z, Time.deltaTime);
}
void Scale1 (GameObject gameObject){
gameObject.transform.localScale = new Vector3 ( gameObject.transform.localScale.x *2, gameObject.transform.localScale.y *2, gameObject.transform.localScale.z *2);
}
void ChangePos2 (GameObject gameObject){
gameObject.transform.position = new Vector3 (gameObject.transform.position.x+1.5F, gameObject.transform.position.y,gameObject.transform.position.z);
}
void Rotate2 (GameObject gameObject){
gameObject.transform.rotation = new Quaternion (gameObject.transform.rotation.x, gameObject.transform.rotation.y+50F, gameObject.transform.rotation.z, Time.deltaTime);
}
void Scale2 (GameObject gameObject){
gameObject.transform.localScale = new Vector3 ( gameObject.transform.localScale.x /2, gameObject.transform.localScale.y /2, gameObject.transform.localScale.z /2);
}
void Changes (int n, GameObject obj2change) {
Debug.Log("Inside Changes function: " + obj2change.name);
switch(n) {
case 1: //ChangePos();
ChangePos1(obj2change);
Debug.Log("c1");
break;
case 2: //Rotate();
Rotate1(obj2change);
Debug.Log("c2");
break;
case 3: //Scale();
Scale1(obj2change);
Debug.Log("c3");
break;
case 4: //ChangePos();
ChangePos2(obj2change);
Debug.Log("c4");
break;
case 5: //Rotate();
Rotate2(obj2change);
Debug.Log("c5");
break;
case 6: //Scale();
Scale2(obj2change);
Debug.Log("c6");
break;
}
}
}
Answer by Chris_Dlala · May 27, 2014 at 12:43 PM
Hi, the problem here is that you are using Quaternion as if it's x, y, z, and w components are degrees (or Euler angles), they are not. To declare a new Quaternion from Euler angles you can use Quaternion.Euler or you can get/set Euler angles in transform.euler
. However, if you are simply trying to rotate an object you may be better off using the Rotate function in Transform.