-1

I have a cube placed at 0, 1 ,0 and once the program is started after testing I found that rigidbody's gravity is causing my cube it continuously moving around coordinates when it actually isnt moving in the program.

I could be clicking absolutely nothing and it will say its moving +-5 units. Its on a completely flat surface. I have beyond confused. http://prntscr.com/j75fed

I can confirm that it starts at 0, 0, 0

using System.Collections;
using UnityEngine;

public class TumblingCubes : MonoBehaviour
{

    public float tumblingDuration = 0.2f;

    void Update()
    {
        var dir = Vector3.zero;

        if (Input.GetKey(KeyCode.UpArrow))
            dir = Vector3.forward;

        if (Input.GetKey(KeyCode.DownArrow))
            dir = Vector3.back;

        if (Input.GetKey(KeyCode.LeftArrow))
            dir = Vector3.left;

        if (Input.GetKey(KeyCode.RightArrow))
            dir = Vector3.right;

        if (dir != Vector3.zero && !isTumbling)
        {
            StartCoroutine(Tumble(dir));
        }
        var vec = transform.eulerAngles;

        transform.eulerAngles = vec;

    }

    bool isTumbling = false;
    IEnumerator Tumble(Vector3 direction)
    {
        var vec = transform.eulerAngles;

        transform.eulerAngles = vec;
        isTumbling = true;

        var rotAxis = Vector3.Cross(Vector3.up, direction);
        var pivot = (transform.position + Vector3.down * 0.5f) + direction * 0.5f;

        var startRotation = transform.rotation;
        var endRotation = Quaternion.AngleAxis(90, rotAxis) * startRotation;

        var startPosition = transform.position;
        var endPosition = transform.position + direction;

        var rotSpeed = 90 / tumblingDuration;
        var t = 0.0f;

        while (t < tumblingDuration)
        {
            t += Time.deltaTime;
            transform.RotateAround(pivot, rotAxis, rotSpeed * Time.deltaTime);
            yield return null;
        }

        transform.rotation = endRotation;
        transform.position = endPosition;



        isTumbling = false;



    }


}

This is the only script attachted to the cube, other than rigidbody

After disabling the script this is thee outcome,??? weird coordinates http://prntscr.com/j75htl

Héctor M.
  • 2,302
  • 4
  • 17
  • 35
jonesmax
  • 85
  • 1
  • 1
  • 6
  • 2
    How is this related to c#? Hint: it's not. You will get better answers at a Unity forum, this is for programming Q&A – Camilo Terevinto Apr 19 '18 at 01:02
  • I can only assume it has something to do in your scripts, can you add any relevant scripting to your question? Anything that moves the cube? – NyW Apr 19 '18 at 01:06
  • @NyW , my bad updating now – jonesmax Apr 19 '18 at 01:07
  • There is nothing wrong with your script, I just made an example to test it and it works fine. The only difference was I had gravity off for the cube. Is there anything else that might affect the cube? – NyW Apr 19 '18 at 01:39
  • @nyw yes u are correct, when i had gravity off, it does work. but I need it on as the cube needs working gravity to be able to fall through holes. what do u get when u have gravity on? – jonesmax Apr 19 '18 at 01:43
  • After putting gravity on, it still seems to work fine. However, I saw from your picture that you have a physic material attached to something. If the friction on the material is too low, it could just be sliding around. – NyW Apr 19 '18 at 01:51

2 Answers2

0

I think the final answer is because of your physic material. Since the friction is too low, the cube is just slipping over the area. You can fix this two ways, if you take the material off whatever it is attached to, or if you freeze the rotation of the cube in the inspector under Rigidbody > Constraints > Freeze Rotation. The second option will not stop your code from working.

Hope this helps!

NyW
  • 107
  • 8
  • thanks ffor all your help, its reallly odd becuse its stil not working. after freezing it , this is what I got http://prntscr.com/j75zyx – jonesmax Apr 19 '18 at 02:17
  • Those values in your screenshot are really small - is this still a problem? For most uses, changing by +- 0.000001 should be ok, as it isn't visible. (I can't see the full value, but it looks the y value is -1.151... * 10^-7 ?) – NyW Apr 19 '18 at 02:29
  • im pretty sure it has to be perfect, but im more focused on the position rather rotation. it puzzles me how its continually moving to units that are across the board ?_? – jonesmax Apr 19 '18 at 02:34
  • And you definitely need the physic material? Or you need the friction low for some reason? – NyW Apr 19 '18 at 02:42
  • update, so i got it to allllmost work. all that is not working is the .000001 that is stopping it from working perfectly. i dont understand how its rotating when its froozen?? – jonesmax Apr 19 '18 at 02:43
  • i spoke to soon – jonesmax Apr 19 '18 at 03:11
0

I had the same with a child with collider intersecting with a parent with his own collider; All Constraints frozen but still minute moves like -0.002... My ugly "solution": transform.localRotation = Quaternion.identity; in Update()!

SC440
  • 1