Using SerializeField lecture - What happened

So when I went through this particular lecture, He said to make the object run away from the camera and back again.
Of course that made me make something in the script that would do this (although I did think maybe he just means changing the value via inspecter)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Mover : MonoBehaviour
{
    [SerializeField] float xValue = 0.0f;
    [SerializeField] float yValue = 0.01f;
    [SerializeField] float zValue = 0.0f;
    [SerializeField] int steps = 0;
    [SerializeField] int maxSteps = 60;
    bool runAway = false;
    // Start is called before the first frame update
    void Start()
    {
        transform.Translate(1,0,0);
    }

    // Update is called once per frame
    void Update()
    {
        if (steps == maxSteps)
            runAway = false;
        else if(steps == 0)
            runAway = true;

        if(runAway)
        {
            transform.Translate(xValue, yValue, zValue);
            steps++;
        }
        else
        {
            transform.Translate(-xValue, -yValue, -zValue);
            steps--;
        }
    }
}

Of course after unpausing I realised he was going to do what I though but any way…
The humour is there xD
Also I know the code is aweful but it was just a quickie at the time

2 Likes

I also misunderstood the challenge, and went for a cyclic movement with the sin function


    void Update()
    {
        transform.Translate(xValue * Time.deltaTime, (float) Math.Sin(Time.time)*yValue, zValue*Time.deltaTime);
    }

Ah, I misunderstood as well but I didn’t go crazy with the code like you two did lol. I have a serious lack of knowledge when it comes to coding so all I did was in the void Start area I added transform.Translate(0,0,10);
It made it pop out pretty far and in unity i put a negative value to make it come back. Looking at both of yours it seems simple but it worked xD.

This may depend on the Unity version. For me it’s “Mathf.Sin()” instead.
(Project created with Unity 2020.3.1f1)

Yeah, i guess that Math lib deals with doubles when Mathf gives float. That’s probably why i had to cast.

Privacy & Terms