D. Making a 3D Cube with Entity
Estimated time: 7 minutes | Previous: C. Declaring Variables | Next: E. Final Code and Running It
๐ฏ Learning Goals
By the end of this lesson, you'll be able to:
- โ
Entity()can create 3D objects with - โ
model,color,scalecan use arguments - โ can store an Entity in a variable
Core Concepts
What Is Entity?
Entityrepresents every object in the game world in Ursina.
๐ Analogy: LEGO blocks
An Entity is a LEGO blockis the same!
- Comes in various shapes (cube, sphere, plane)
- Can be painted a color
- Can be resized
- Can be combined to build complex things
An Entity is the basic building block of the game world!
Entity Creation Flowchart
flowchart TD
A["Entity()"] --> B{"Set arguments"}
B --> C["model='cube'<br/>Sets the shape"]
B --> D["color=color.red<br/>Sets the color"]
B --> E["scale=2<br/>Sets the size"]
C --> F["3D object created!"]
D --> F
E --> F
style A fill:#e3f2fd,stroke:#1976d2
style F fill:#e8f5e9,stroke:#388e3c
Creating an Entity
Argument descriptions:
model='cube': cube shapecolor=color.red: redscale=2: 2x size
Key Arguments
| Argument | Role | Example value |
|---|---|---|
model |
Shape | 'cube', 'sphere', 'plane' |
color |
Color | color.red, color.blue, color.green |
scale |
Size | 1, 2, (2,1,3) |
position |
Position | (0,0,0), (1,2,3) |
Available shapes
| model value | Shape | Description |
|---|---|---|
'cube' |
Cube | The most basic 3D shape |
'sphere' |
Sphere | A round ball shape |
'plane' |
Plane | A flat floor panel |
'quad' |
Square | A 2D rectangle |
โ ๏ธ Watch the argument format!
Arguments are name=value written in this format!
- โ
model='cube'(use an equals sign) - โ
model:'cube'(no colons allowed) - โ
model-'cube'(no hyphens allowed)
Example Code
from ursina import *
app = Ursina()
# Store the settings in variables
cube_color = color.red
cube_size = 2
# Make a cube with Entity
my_cube = Entity(
model='cube',
color=cube_color,
scale=cube_size
)
app.run()
Result
A red cube appears in the center of the screen!
- Size: 2x the default
- Color: red
- Position: center of the screen (0, 0, 0)
๐ฏ Quiz
Show Answer
Answer: A) model
model: sets the shape (cube, sphere, etc.)color: sets the color (red, blue, etc.)scale: sets the size (1, 2, etc.)
๐ง Debug Clinic
Problem: TypeError: Entity() got an unexpected keyword argument
Cause: a typo in the argument name
Fix: use the exact argument name
Problem: I can't see the cube!
Cause: a camera position or cube size issue
Fix: check the scale value or add EditorCamera
Try Coding It Yourself
Try making different Entities!
Creating a Blue Sphere
Write the code to make a blue sphere.
Example Answer
from ursina import *
app = Ursina()
my_sphere = Entity(
model='sphere',
color=color.blue
)
app.run()
Result: A blue sphere appears in the center of the screen!
๐งฉ Key Takeaways
What you learned in this lesson:
- Entity: every object in the game world (LEGO blocks)
- model: sets the shape (
'cube','sphere'etc.) - color: sets the color (
color.redetc.) - scale: sets the size (a number)
- Argument format:
name=value(use an equals sign!)
Review Checklist
- [ ] Can you explain what an Entity is?
- [ ] Do you know what the model, color, and scale arguments do?
- [ ] Do you know how to store an Entity in a variable?
Next step: E. Final Code and Running It โ