well i have a problem, i’m a benniger on this stuff of scripting and i need to know how can i make my “cube” move to left and right., i just have this part of the code
pd: i’m making a 2d sidescroller
If you have a Rigidbody, then moving it looks like this:
rigidbody.MovePosition(newPosition);
You can learn this by looking up “Rigidbody” in the manual. If you’re new to scripting, then it may be worth pointing out that the Unity scripting reference manual is your best friend. Get to know it well, and it will take good care of you.
Incidentally, how you move an object varies depending on what other components it has — here’s a bit from one of my scripts, that applies a delta (relative position change) to almost anything:
if (charController != null) {
charController.Move(deltaPos);
} else if (rigidbody != null) {
rigidbody.MovePosition(rigidbody.position + deltaPos);
} else {
transform.position = transform.position + deltaPos;
}
This requires a charController property of type CharacterController, which you’d fill out in the Start method like so:
charController = GetComponent<CharacterController>();
But of course if you never use CharacterController, then you could just remove the parts that deal with that.
Cheers,
- Joe