Spinning doesn't start until Box drops

I’ve just written the spin code (and made it framerate independent with DeltaTime). Unfortunately, it doesn’t commence spinning until the previously made Dropper cube appears and drops. It is linked, since I can change the drop-time variable and the spinner will always start when the drop-time variable goes off.

Blockquote
using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Dropper : MonoBehaviour

{

[SerializeField] float dropTime = 5.0f;



Rigidbody rigid;

MeshRenderer meshy;

// Start is called before the first frame update

void Start()

{

    rigid = GetComponent<Rigidbody>();

    rigid.useGravity = false;

    meshy = GetComponent<MeshRenderer>();

    meshy.enabled = false;

}

// Update is called once per frame

void Update()

{

    if(dropTime > Time.time)

    {

        rigid.useGravity = true;

        meshy.enabled = true;

    }

}

}

Hi,

Welcome back to our community. :slight_smile:

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture? And have you already tried to add Debug.Logs to your code to see what is going on during runtime?

To which game object did you assign the spinner. Actually, the Dropper is not supposed to affect the other component.

1 Like

Thank you! I finally figured it out. If you look in the code I pasted above, I had “if(dropTime > Time.time)”, whereas what works is “if (Time.time > dropTime)”. By having the assignment backward, I was basically freezing the clock for everything time dependent, like the drop and the spinning, rather than having the time regulate them.
This is so fun, but really challenging! Thanks again!

Good job! :slight_smile:

Awwww, thanks! This course is about the first time I feel like I’m really understanding scripting. I’m very comfortable with the graphics side of things (I teach it at the University level) but scripting has always seemed too math-y and scary. After all, it’s basically using an invisible interface to solve a logic puzzle.

I feel almost the same about coding but I regard it as a way to express a logic/solution. In my opinion, the logic itself does not have any specific form. You could visualise it in a flowchart or describe it with words (“First do this, then do that”). The problem is: The computer cannot understand our flowchart or English, so we will have to translate it into a language which it is able to interpret. C# is such a language, and its purpose is not creating awesome poetry but giving precise instructions, hence the limited vocabulary and simple syntax. This really does not have much to do with maths.

You’re right, of course. I just meant that the appearance of code gives me Algebra II flashbacks!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms