Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 FIGM3NT · Oct 30, 2015 at 09:03 PM · performanceraycasthit2dfollow path

Inconsistant speed of object following raycast

So im creating a simple game and one component of the game is a greendot following the outside of the level. I got this working using a raycast in the middle which rotates and gives the position of collision the the gameobject.

alt text

The problem is that the speed is inconsistant at the moment since the distance between two collisions can be further distance if i have a slope. I also have the feeling that there should be a easier way to get the same result. What are your thoughts?

public class FollowPath : MonoBehaviour { Vector3 collisionPos; public GameObject greenDot;

     void Update ()
     {
         RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.up);
         transform.Rotate(0.0f, 0.0f, 3);
 
         if (hit.collider != null)
         {
             collisionPos = hit.point;
         }
 
         greenDot.transform.position = collisionPos;
     }
 }
 


show.png (20.0 kB)
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
1
Best Answer

Answer by OncaLupe · Oct 31, 2015 at 01:48 AM

My thought was to have a path the green dot follows. You can place empty game objects at each of the angles the walls make, store them in an array in the dot, and have it move from one to the next. This should give you constant movement no matter the direction, as well as allow more complex room shapes than a raycast would work with.

 using UnityEngine;
 using System.Collections;
 
 public class GreenDotMover : MonoBehaviour
 {
     public Transform[] waypointPaths;
     int currTarget;
     Vector3 targetPosition;
     public float moveSpeed = 1f;
     float arrivalDistance = 0.1f;
 
     void Start()
     {
         //Move to the first waypoint, then set target to the second
         transform.position = waypointPaths[0].position;
         currTarget = 1;
         targetPosition = waypointPaths[1].position;
     }
     
     void Update()
     {
         //Move toward the next waypoint. When we reach it, get next waypoint
         transform.position = Vector2.MoveTowards(transform.position, targetPosition, moveSpeed * Time.deltaTime);
         if(Vector2.Distance(transform.position, targetPosition) < arrivalDistance)
         {
             ++currTarget;
             if(currTarget >= waypointPaths.Length)
                 currTarget = 0;
             targetPosition = waypointPaths[currTarget].position;
         }
     }
 }
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Why are gravity rigidbodies so performance costly? 1 Answer

complexity of hierarchy affects performance? 1 Answer

Low CPU and GPU utilization with Vsync disabled, in blank scene GPU Usage "other" spikes to ~20ms 1 Answer

Reading Profiler Results 1 Answer

1024 x 1024 or 2048 x 2048 for main character, what is the best performance/looks 2 Answers


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