Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by dustinjbailey · Oct 15, 2020 at 05:21 PM · waypointroll a balltower-defensenew user

How to roll a ball through waypoints (3D Tower Defense)

Hi, I'm a new Unity user and I can't find the answer to my specific questions anywhere. I'm creating a tower defense game where the "enemy" is a ball that rolls along a track.

I've followed this great Brakey's tutorial and everything works fine, I just don't know how to alter it to have the ball roll along the waypoint path instead of float mid-air.

Here is his code for the Enemy to move from Waypoint to Waypoint for reference:

 using UnityEngine;
 
 public class Enemy : MonoBehaviour
 
 {
    
     public float speed = 1.0f;
     private int wavepointIndex = 0;
     private Transform target;
 
     void Start ()
     {
         target = Waypoints.points[0]; 
     }
 
     void Update ()
     {
         Vector3 dir = target.position - transform.position;
         transform.Translate(dir.normalized * speed * Time.deltaTime, Space.World);

         if (Vector3.Distance(transform.position, target.position) <= 0.05f)
         {
             GetNextWaypoint();
         }
     }
 
     void GetNextWaypoint () 
     {
         if (wavepointIndex >= Waypoints.points.Length - 1)
         {
             Destroy(gameObject);
             return;
         }
 
         wavepointIndex++;
         target = Waypoints.points[wavepointIndex];
     }
 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ady_M · Oct 16, 2020 at 04:46 AM

This example does not implement the waypoint system, but it should be obvious what you need to do.

I've taken into account the ball's radius and speed to make it look more natural while rolling, but the code does not attempt to calculate the radius for you. Measure your objects manually and input an appropriate value.

It ignores the Y position of the target/destination to prevent vertical movement when objects have varying sizes. You can easily disable this if you want.

 public class Ball : MonoBehaviour
 {
     public float speed = 1;
     public float radius = 0.5f;

     public Transform target;

     void Update()
     {
         Vector3 destinationPositionModified = target.position;

         // Uses the ball's Y position instead of destination Y
         // to prevent vertical movement
         destinationPositionModified.y = this.transform.position.y;

         // Used for distance calculation and rotation
         Vector3 vectorToDestination = destinationPositionModified - transform.position;

         float distance = vectorToDestination.magnitude;
         float distanceToTravelOnThisFrame = Mathf.Min (distance, speed * Time.deltaTime);

         if (distanceToTravelOnThisFrame > 0)
         {
             // Move
             transform.position = Vector3.MoveTowards (transform.position, destinationPositionModified, speed * Time.deltaTime);
             
             if (radius > 0)
             {
                 // Takes into account the ball's radius
                 float circumference = 2 * Mathf.PI * radius;
                 float angularChange = distanceToTravelOnThisFrame / circumference * 360;

                 // Rolls towards the destination
                 Vector3 rotationAxis = Quaternion.LookRotation (vectorToDestination) * Vector3.right;
                 transform.Rotate (rotationAxis, angularChange, Space.World);
             }
         }
         else
         {
             // Destination reached
             // Call GetNextWaypoint() here
         }
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

137 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Turret Firing problem [C#] 2 Answers

Rotate to an amount, wait, rotate to an amount 1 Answer

How to get character animations to transition? Why isn't this working? 0 Answers

Unity loading everything up see through 0 Answers

Roll-a-Ball enemy ball AI 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges