Quaternion.Lerp not even moving - c#

I wanted to lerp rotation to new Quaternion(0,0,0,0); but it seems like not animating to desired rotation at all... It just stop at where it is...
What I tried were those three on below
RectTransform rectTransformComponent = this.transform.GetComponent<RectTransform>();
rectTransformComponent.localRotation = Quaternion.Lerp(rectTransformComponent.localRotation, new Quaternion(0,0,0,0), 0.1f);
and
this.transform.localRotation = Quaternion.Lerp(this.transform.localRotation, new Quaternion(0,0,0,0), 0.1f);
and
this.transform.rotation = Quaternion.Lerp(this.transform.rotation, new Quaternion(0, 0, 0, 0), 0.1f);
I'm using RectTransform for this object. It was working without lerping. It's in the Update() so it will be looped every frame. I tried to bring up the speed for lerp but no luck...
Does someone have any idea what is going on??
this.transform.localRotation = new Quaternion(0,0,0,0) //This works

An all-zero quaternion (new Quaternion(0,0,0,0)) is an invalid rotation. Quaternion.Lerp doesn't know how to interpolate to an invalid rotation.
To rotate to an Euler rotation of x=0,y=0,z=0, then you should instead use Quaternion.identity (which is equivalent to new Quaternion(0,0,0,1)).

Related

Moving Camera to GameObject: The targetposition is wrong after rotation

I want to place the camera in front of a GameObject onTrigger. It works with my code as long as the GameObject faces one particular direction, but if I rotate it, the target position will be behind the GameObject.
What do I miss? :)
mainCamera.transform.position = currentGameObject.transform.position + new Vector3(lookingDistance, 0, 0);
mainCamera.transform.LookAt(currentGameObject.transform);
The script is not attached to the GOs. I use kind of an interactionManager.
Your rotation code seems all right, but when finding new position you do not take rotation into account.
Try changing
+ new Vector3(lookingDistance, 0, 0);
into
+ currentGameObject.transform.TransformPoint(new Vector3(lookingDistance, 0, 0));

How to calculate angle for transform.RotateAround so the end result has a y==0

So I need to solve the following puzzle:
There is a 3D cube with rotation (34,90,23) (or some other random values). I want to rotate around the local y-axis until transform.position.y == 0. In the editor I can just drag-rotate on the y axis until I see 0 as a value in the transform, but how do I do that with code? (You cannot just set y=0, because all the values change when you rotate around the local y-axis)
I am thinking about using transform.RotateAround(transform.position, transform.up, angle), but I don't know how to correctly calculate angle so that after RotateAround() the transform.rotation.y == 0.
And just to specify, I don't want to code the dragging itself, just the result. The rotation should be instant.
Can anyone write a working code for this?
I guess what you are looking for is an "animation" that will affect the transform.localEulerAngles.y value. To achieve this I'd recommend using a coroutine (you can find Unity tutorial here).
It would look like this:
private IEnumerator RotateAroundY(float rotationTime)
{
float timer = 0.0f;
Vector3 startLocalEulerAngles = transform.localEulerAngles;
Vector3 deltaLocalEulerAngles = new Vector3(0.0f, Mathf.DeltaAngle(startLocalEulerAngles.y, 0.0f), 0.0f);
while (timer < rotationTime)
{
timer += Time.deltaTime;
transform.localEulerAngles = startLocalEulerAngles + deltaLocalEulerAngles * (timer / rotationTime);
yield return new WaitForEndOfFrame();
}
transform.localEulerAngles = startLocalEulerAngles + deltaLocalEulerAngles;
}
And you can call it using StartCoroutine(RotateAroundY(4.0f));.
If you're getting into Unity I highly recommend you to familiarize with coroutines :)
EDIT :
Sorry about not noticing you didn't needed an animation: if you simply want to rotate around the local transform.up vector, you can change the transform.localEulerAngles.y value.
You can do it this way transform.localEulerAngles.y = new Vector3(transform.localEulerAngles.x, 0.0f, transform.localEulerAngles.z);
Hope this helps,

Why the script is not rotating my sphere object?

Well, I am new to unity 3d and C sharp. I was trying a script to rotate my spehere object . But it's not working.
I was following a youtube video. This code worked for him. But in my case it is not working.
I added the transform object.
using UnityEngine;
using System.Collections;
public class cubescript : MonoBehaviour {
public Transform sphereTransform;
// Use this for initialization
void Start () {
sphereTransform.parent = transform;
}
// Update is called once per frame
void Update () {
transform.eulerAngles = new Vector3 (0, 180*Time.deltaTime, 0);
}
}
It's kind of working but stuck at 2.981877-3 Y rotation .. And not rotating around the cube..
The problem is that you are trying to rotate, but eulerAngles only sets to ABSOLUTE angles (if you want to add angles to the current frame angle, you will use Rotate).
So, if you use transform.eulerAngles you will be all the frames setting the angle change to what 180 * Time.deltaTime returns, that will depend on how many FPS you are running, thats why you get constant number.
If you use transform.Rotate it will add the new angle change to the current angle frame. Say that you want to increment by 10 degress, so frame 1 = (0,0,0), frame 2 = (0,10,0), frame 3 = (0,20,0).
In eulerAngles you will get all the time (0,10,0), because it sets ABSOLUTE angle, Rotate adds to the current angle what you want.
Change this
transform.eulerAngles = new Vector3 (0, 180*Time.deltaTime, 0);
To this
transform.Rotate(new Vector3 (0, 180*Time.deltaTime, 0));
This is the official Unity Documentation for eulerAngle and Rotate
As said in another answer, when you set transform.eulerAngles, you are setting an absolute rotation. You can use transform.Rotate() but you can also use Time.time to ensure that you get linear rotation: transform.eulerAngles = new Vector3(0, 180*Time.time, 0);

Unity rotate object around local axis

I am trying to rotate an object around it's local axis (In this case a humanoid index finger) with my custom editor.
public float amount;
void OnGUI() {
amount = EditorGUILayout.Slider("Rotate Amount", amount, 0, 100);
index1.transform.localEulerAngles = new Vector3(0, 0, amount);
}
The issue I am having is when I move the slider, the finger rotates down and forward so the tips of the fingers point outwards, when in theory they should point towards the elbow.
I think I am using the wrong type of transform here, so what should I do to use the right transform?
Try to use:
index1.transform.Rotate(new Vector3(0, 0, amount), Space.World);
or
index1.transform.Rotate(new Vector3(0, 0, amount), Space.Self);
Hope it solves your problem :)
There may be 2 parts to this:
Getting the current rotation of the object on the local z-axis, here's one way to do that. https://stackoverflow.com/a/47841408/228738
Using the current rotation value to find the amount of rotation to apply to the object.
code for step #2
var currentZEuler = zRotation(this.transform.rotation).eulerAngles.z;
var deltaZEuler = amount - deltaZEuler;
index1.transform.Rotate(0, 0, deltaZEuler, Space.Self);

How to make my sprite object go offscreen to the left and have it appear on the right?

In Unity, I'm trying to make my sprite go offscreen to the left and have it appear offscreen on the right or vice versa. I have my sprite move left or right (depending on user input) constantly for my gameplay by the way.
I take into account that the sprite needs to be fully offscreen before having it appear on the right side.
Here's my current code:
void Start ()
{
minXValueWorld = Camera.main.ScreenToWorldPoint(new Vector3(0,0,0)).x;
maxXValueWorld = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, 0, 0)).x;
playerSize = this.GetComponent<Renderer>().bounds.size;
}
void Move()
{
if (inputLeft)
{
this.transform.position -= new Vector3(speed * Time.deltaTime, 0, 0);
}
else
{
this.transform.position += new Vector3(speed * Time.deltaTime, 0, 0);
}
}
void OffscreenCheck()
{
Vector3 screenPos = Camera.main.WorldToScreenPoint(this.transform.position);
Vector3 maxWorldXWithPlayerSize = Camera.main.WorldToScreenPoint(new Vector3(maxXValueWorld,0,0) + playerSize/2);
Vector3 minWorldWithPlayerSize = Camera.main.WorldToScreenPoint(new Vector3(minXValueWorld,0,0) - playerSize/2);
if (screenPos.x < minWorldWithPlayerSize.x)
{
this.transform.position = new Vector3(maxWorldXWithPlayerSize.x, this.transform.position.y, this.transform.position.z);
}
if (screenPos.x > maxWorldXWithPlayerSize.x)
{
this.transform.position = new Vector3(minWorldWithPlayerSize.x, this.transform.position.y, this.transform.position.z);
}
}
Then the Move and OffscreenCheck are called in the Update function in order.
The problem with this code is that once my sprite goes fully offscreen on the left, it appears on the right offscreen, but it does not move left anymore.
Instead, it just teleports to the left or right offscreen positions. I do not see it move left or right across the screen anymore because of this.
I'm pretty sure that my code logic is just off. Does anyone know how to fix this issue?
Thanks
I think the following should work:
void OffscreenCheck()
{
Vector3 screenPos = Camera.main.WorldToScreenPoint(this.transform.position);
Vector3 maxWorldWithPlayerSize = new Vector3(maxXValueWorld + playerSize/2,0,0);
Vector3 minWorldWithPlayerSize = new Vector3(minXValueWorld - playerSize/2,0,0);
Vector3 maxScreenWithPlayerSize = Camera.main.WorldToScreenPoint(maxWorldWithPlayerSize);
Vector3 minScreenWithPlayerSize = Camera.main.WorldToScreenPoint(minWorldWithPlayerSize);
if (screenPos.x < minScreenWithPlayerSize.x)
{
this.transform.position = new Vector3(
maxWorldWithPlayerSize.x,
this.transform.position.y,
this.transform.position.z);
}
if (screenPos.x > maxScreenWithPlayerSize.x)
{
this.transform.position = new Vector3(
minWorldWithPlayerSize.x,
this.transform.position.y,
this.transform.position.z);
}
}
The above is a good illustration of why to be careful about how you name your variables. When I first looked at your code, I failed to connect the fact that while the variables had the word "World" in them, they were actually in screen coordinates. So I didn't notice the faulty assignment of the position, in which you used screen coordinates to assign an X coordinate in world coordinates.
In the above, I have separated the world and screen coordinate vectors into individual local variables. The screen coordinates are used for the off-screen comparison itself, while the world coordinates are used for actually moving the sprite to where you want.
Again, lacking a complete code example I'm not able to actually test the above and verify that it solves your problem. But I think it will.

Categories