Understanding Vector Angles in Roblox

Feb 14, 2025

Lecture Notes on Vector Angles in Roblox

Introduction to Vectors in Roblox

  • Vectors are essential three-number constructs in Roblox.
  • Key uses include:
    • Assigning values to hinge constraints for smooth animations.
    • Determining FOV (Field of Vision) range for enemy NPCs.

Importance of Finding Angles Between Vectors

  • Historically difficult due to complex math.
  • Roblox now provides a vector3 angle method simplifying this process.
  • Understanding the underlying math is beneficial.

Understanding Angles Between Vectors

2D Example

  • Angle between vectors measured at the origin.
  • Always finds the shortest (most acute) angle.
  • Non-signed angle by default and up to 180 degrees.

3D Example

  • Involves an imaginary plane where vectors sit.
  • Plane is not aligned with any axis.
  • Angle is the closest to the vectors by the plane.
  • Axis perpendicular to the plane can be specified to determine angle sign.

Practical Implementation in Roblox

Setting Up in Roblox

  • Create parts in Roblox: ref and target, both anchored.
  • Use a script to find angles using vector3 methods.

Script Implementation

  • Calculate direction as unit vector from ref to target.
  • Example script setup:
    local dir = (target.position - ref.position).unit
    local angle = math.deg(vector3.xAxis:Angle(dir))
    
  • Converts radians to degrees using math.deg.

Handling Angle Sign and Axis

  • Roblox returns angles in radians (0 to 2Ï€).
  • Convert to degrees for intuitive understanding.
  • Use additional parameters to specify axis and sign.

Application with Hinges and Servos

  • Use a hinge constraint for animated parts.
  • Demonstrate alignment and pointing using vectors.

Practical Setup

  • Flatten the direction vector to eliminate unwanted components.
  • Example:
    local flatDir = dir * Vector3.new(1, 0, 1)
    
  • Define axis for determining angle sign, e.g., Vector3.yAxis for xz plane.

Advanced Applications

  • Create complex systems like AI turrets using the same principles.
  • Example turret tracks targets and responds dynamically.

Conclusion

  • Working with vectors and angles is intuitive with practice.
  • Experiment to understand behavior and create sophisticated systems.
  • Final Note: Engage with the community for tutorials and further learning.