Hello,
There is no problem when the floor object is not animated. but only when animated. The location of the cube is corrupted. What do you think is the reason for this? any clue ?
(I can upload the test project if needed. thanks in advance)
is the cube moving using animations or is it through code?
floor moves up down using animation.
cube moves just using script. (no rigidbody attached).
codes attached.
just used transform.rotatearound method.
9593794–1359712–IEnum_002.cs (3.52 KB)
It looks like your using transform.position when moving the cube, have you tried using transform.localPosition? when using the regular position your setting it to the position in world space but it looks like your ground is a parent of the object meaning if you just used localPosition it would apply the new position in local space as a child of the ground
The main two problems you have are:
So make sure you (re-)calculate the actual pivot every loop iteration as the world space position you want to rotate around.
Another solution for this kind of mechanics is to use an empty gameobject as a temporary parent for your cube. So you move it to the edge you want to rotate around, parent the cube to it, rotate the empty GO and when finished, unparent the cube again.
thanks, I got the 3 ideas youall mentioned. most closest solutions. but I couldn’t achieve yet today.
attempted many code change combinations. nothing worked : )
maybe IEnumerate threads not correctly syncronized with FPS ticks.
I have a temporal solution is disabling animation while iterator is running. and that indeed worked.
you can comment this lines to see problem
transform.parent.parent.GetComponent().enabled
next week … I will preserve and update the “offset” via code in iterators instead of parenting null Object.
uploaded test project to gdrive here.
9594055–1359793–IEnum_002.cs (4.2 KB)
@tarzmedia
Currently, you have a separate method for each of the four directions. Essentially, these four methods perform the same logic, differing only in their input data. It’s likely that you even used copy-paste for each method and just changed some Vector3 values within each.
You can try to streamline the code by removing duplication, for example, like this:
using System.Collections;
using UnityEngine;
public class RollingCube : MonoBehaviour
{
public float Speed = 1f;
private bool _rolling;
void Update()
{
if (Input.GetKey(KeyCode.RightArrow))
RollTo(Vector3.right);
if (Input.GetKey(KeyCode.LeftArrow))
RollTo(Vector3.left);
if (Input.GetKey(KeyCode.UpArrow))
RollTo(Vector3.forward);
if (Input.GetKey(KeyCode.DownArrow))
RollTo(Vector3.back);
}
private void RollTo(Vector3 direction)
{
if (_rolling)
return;
StartCoroutine(RollInWorld(direction));
}
private IEnumerator RollInWorld(Vector3 direction)
{
_rolling = true;
float kalanAci = 90;
Vector3 rotcenter = transform.position + Vector3.down / 2 + direction / 2;
Vector3 dingil = Quaternion.AngleAxis(90f, Vector3.up) * direction;
while (kalanAci > 0)
{
float buDonus = Mathf.Min(Time.deltaTime * Speed, kalanAci);
kalanAci -= buDonus;
kalanAci = Mathf.Max(0, kalanAci);
transform.RotateAround(rotcenter, dingil, buDonus);
yield return null;
}
_rolling = false;
}
}
thanks so much for suggestion. I will take into account that in next level.
but my focus is not “having a clear code”.
btw, thats not the problem solution. exact problem still exists with that code.
if you download gdrive project and replace C# files, you will see the problem.