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 Strom_CL · Nov 17, 2012 at 01:35 AM · movementpositionobjectrandomedge

Make an object move towards random spot on another objects edge?

I'm working with a script that performs a waypoint setup, object moves from A to B to C to D to A... etc.

Right now the object, in this case a pool cue ball, moves towards the bumpers middle position and on to the next bumpers middle. Is there an easy way to randomize the position that it moves towards instead of using the center of the object?

You can see below the green lines are the gizmos I have setup to show the path from waypoint to waypoint, and the red is where i'd like it to randomly strike, just a few examples of some random points.

alt text

This is the code I'm using for the Follow script that the ball has set on it.

 using UnityEngine;
 using System.Collections;
 
 public class Follow : MonoBehaviour {
 
     public Transform activeWaypoint;
     public Vector3 move;
     public float speed = 1.0f;
 
     void OnTriggerEnter (Collider triggerWaypoint) {
     
         if (activeWaypoint.collider == triggerWaypoint) {
             activeWaypoint = activeWaypoint.GetComponent<Waypoint>().next;
             move.x = (activeWaypoint.transform.position.x - transform.position.x) / speed;
             move.z = (activeWaypoint.transform.position.z - transform.position.z) / speed;
         }
     }
 
     void Start () {
         activeWaypoint = Waypoint.start;
         move.x = (activeWaypoint.transform.position.x - transform.position.x) / speed;
         move.z = (activeWaypoint.transform.position.z - transform.position.z) / speed;
 
     }
     
     // Update is called once per frame
     void Update () {
         transform.Translate(move * Time.deltaTime);
     }
 }

I'm just not familiar enough with the rendering or object bounds to get a random point along an objects edge. Any help would be appreciated! :D

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
Best Answer

Answer by Strom_CL · Nov 17, 2012 at 07:14 AM

Figured it out finally, I just used:

 move.x = (activeWaypoint.transform.position.x + 
           Random.Range(-activeWaypoint.renderer.bounds.extents.x + 1,
           activeWaypoint.renderer.bounds.extents.x - 1)  
           - transform.position.x)

This way it uses the activeWaypoint position then adds a random negative value from the extents (half the length) to a positive extents length. I did a +1 and -1 to keep the ball away from the edge, was getting bounces right on the opening. Works like a charm! :D

Might be an easier way but this works, still open to any ideas!

Comment
Add comment · Show 1 · 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
avatar image Fattie · Nov 17, 2012 at 08:46 AM 0
Share

that's just how you do it, but don't forget it only works if the object is horizontal or vertical.

for a more general solution just DO THIS:

under the object add two $$anonymous$$AR$$anonymous$$ER OBJECTS. I mean simply two empty game objects. Name them "$$anonymous$$arkerLeftEnd" and "$$anonymous$$arkerRightEnd"

In video game dev, it is incredibly common to have "marker objects" everywhere in the scene. I mean on say a vehicle, you could easily have dozens. (things like "attract bugs to here" "keyhole near hear" "top of roofline" etc etc .. it goes on and on)

So then write a general script that returns a random point between the two such markers.

This is an incredibly useful general solution. Note that you can very easily move the markers by eye .. for instance you may actually not want the "very ends" of the object -- whatever. hope it helps.

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

10 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

Related Questions

2D get touch input 1 Answer

Object not moving,Object not moving in any direction 1 Answer

Position an array of objects randomly 1 Answer

How to get an Objects position per axis? 1 Answer

Save object position (random) 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