I am using the ‘Ruby’s 2D Adventure’ tutorial to get a basic introduction into Unity, and I’ve hit a pretty big stumbling block. I am as far as ‘World Interactions - Damage Zones & Enemies’ part 4, and am stuck on the script. The script ‘solution’ given does not work for me. It is attached to the prefab, and I get not errors.
.
The enemy is a robot who is supposed to move up and down. Here is the script. I am using Unity version 2018.3, which is the suggested version for this course.
EnemyControler.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyController : MonoBehaviour
{
public float speed = 3.0f;
public bool vertical;
public float changeTime = 3.0f;
Rigidbody2D rigidbody2D;
float timer;
int direction = 1;
// Start is called before the first frame update
void Start()
{
rigidbody2D = GetComponent<Rigidbody2D>();
timer = changeTime;
}
// Update is called once per frame
void Update()
{
timer -= Time.deltaTime;
if (timer < 0)
{
direction = -direction;
timer = changeTime;
}
Vector2 position = rigidbody2D.position;
if (vertical)
{
position.y = position.y + Time.deltaTime * speed * direction;
}
else
{
position.x = position.x + Time.deltaTime * speed * direction;
}
rigidbody2D.MovePosition(position);
}
}
Looks fine to me as a standalone script. I would guess the instance in your game isn’t the same as your prefab. Or you have the script disabled. Or one of a million things in unity that can go wrong. The script itself looks fine tho.
What does that mean “Your trying to move a rigidbody in update”, and “You should be using FixedUpdate for any physics”?
I am doing this tutorial and am having this same problem. I fix or alternative would be very helpful or an explanation about “Update”, and “FixedUpate”.
Is it that your enemy is just moving horizontally? The script won’t automatically change the enemy from going side-to-side to going up-and-down. As bool vertical is public it creates a check box in the Unity editor. Ticking that should make the enemy move vertically
if enemy not moving, maybe you add script on gameobject directly not prefabs so script not updating. try delete enemy gameobject, then add again from prefabs folder
Yeah, despite the script saying “3.0f” the Inspector window still shows “0” so you have to set a number there. I’m using 2019.2.0.
One problem I noticed is that the robot guy will move when Ruby touches him. She can basically move him around the map. Granted, I’m still only on that particular chapter. Haven’t gone further yet.
mine doesnt turn arround, starts going right, or up if i check the “vertical” in the inspector, but only goes in one direction. anyone know how to fix this?
So I had a couple issues and here is how I fixed them:
My bot didn’t have Rigid Body 2D with gravity set to 0. Somehow this got lost when I was playing around with my prefabs. It didn’t throw an error in the code, so it wasn’t until I checked what modules I had on my prefab I realized anything was wrong.
In the example code, they have public class EnemyController2 : MonoBehaviour. This needs to match the script file name, so I updated this to public class EnemyController : MonoBehaviour
Set the speed of the bot either in the code to start at not zero, or update it when you start the game in the inspector window