Reputation:
I am trying to scale and rotate an object. The rotation is on X-Axis. I am able to implement the scale perfectly, it is the rotation that is causing the problem. How do I rotate the object to a target rotation and then back to initial rotation with this script? When I am executing this script, the rotation sometimes work and sometimes does not execute, however there is no problem with the scaling.
EDIT: The basic thing I found was that I cannot set Quaternion to negative value, that is -90 becomes 270 automatically and due to this I cannot move 270 to 0.
using UnityEngine;
using System.Collections;
public class ScaleAndRotate : MonoBehaviour {
public int startSize = 3;
public int minSize = 1;
public int maxSize = 6;
public float speed = 2.0f;
private Vector3 targetScale;
private Vector3 baseScale;
private int currScale;
//ROT
public Quaternion targetRotation;
public bool startRotation = false;
void Start() {
baseScale = transform.localScale;
transform.localScale = baseScale * startSize;
currScale = startSize;
targetScale = baseScale * startSize;
}
void Update() {
transform.localScale = Vector3.Lerp (transform.localScale, targetScale, speed * Time.deltaTime);
if(startRotation == true)
{
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * speed);
}
else
if(startRotation == false)
{
//Go Back To Initial Rotation
}
if (Input.GetKeyDown (KeyCode.UpArrow))
{
ChangeSize (true);
startRotation = true;
}
if (Input.GetKeyDown (KeyCode.DownArrow))
{
ChangeSize (false);
startRotation = false;
}
}
public void ChangeSize(bool bigger) {
if (bigger)
{
currScale++;
}
else
{
currScale--;
}
currScale = Mathf.Clamp (currScale, minSize, maxSize+1);
targetScale = baseScale * currScale;
}
}
Upvotes: 0
Views: 159
Reputation:
The problem was with the transform.rotation
that was not able to change the rotation from -90/270 to new rotation. I instead had to use transform.localRotation
and it worked perfectly.
using UnityEngine;
using System.Collections;
public class ScaleAndRotate : MonoBehaviour {
public int startSize = 3;
public int minSize = 1;
public int maxSize = 6;
public float speed = 2.0f;
private Vector3 targetScale;
private Vector3 baseScale;
private int currScale;
//ROT
public Quaternion targetRotation;
public Quaternion initialRotation;
public bool startRotation = false;
void Start() {
baseScale = transform.localScale;
transform.localScale = baseScale * startSize;
currScale = startSize;
targetScale = baseScale * startSize;
initialRotation = transform.localRotation;
}
void Update() {
transform.localScale = Vector3.Lerp (transform.localScale, targetScale, speed * Time.deltaTime);
if(startRotation == true)
{
transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation, Time.deltaTime * speed);
}
else
if(startRotation == false)
{
//Go Back To Initial Rotation
transform.localRotation = Quaternion.Slerp(transform.localRotation, initialRotation, Time.deltaTime * speed);
}
if (Input.GetKeyDown (KeyCode.UpArrow))
{
ChangeSize (true);
startRotation = false;
}
if (Input.GetKeyDown (KeyCode.DownArrow))
{
ChangeSize (false);
startRotation = true;
}
}
public void ChangeSize(bool bigger) {
if (bigger)
{
currScale++;
}
else
{
currScale--;
}
currScale = Mathf.Clamp (currScale, minSize, maxSize+1);
targetScale = baseScale * currScale;
}
}
Upvotes: 2
Reputation: 221
I think the problem is that you miss brackets when you use if
statements:
if (Input.GetKeyDown (KeyCode.UpArrow))
ChangeSize (true);
startRotation = true;
if (Input.GetKeyDown (KeyCode.DownArrow))
ChangeSize (false);
startRotation = false;
When you use something like that code above the if
is used only to the first following line of code. Thus, add brackets to that code:
if (Input.GetKeyDown (KeyCode.UpArrow))
{
ChangeSize (true);
startRotation = true;
}
if (Input.GetKeyDown (KeyCode.DownArrow))
{
ChangeSize (false);
startRotation = false;
}
Upvotes: 1