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 Nercoe · Oct 27, 2012 at 11:27 AM · collisionmovementrandom

Random Movement collision bug

Hey guys I tried getting a fix for this earlier but I can't get it working. What I want is the object to move at random speeds. This is fine and working and changes direction at random intervals. Basically what I want is for the object to collide with something and change its direction (IMPORTANT: I have a fence around my plane to stop people exiting the map, when the object collides with this, I want the object to in effect "bounce" off it). At the moment it does this but sometimes it bugs and goes flying through the fence (I guess it changes it volicity that way twice in a row and glitches. Here is the code:

 var chicken : Transform; 
 var vel : Vector3;
 var Direction : float = 3;
 var curTime : float = 0;
 function Start()
 {
   SetVel();
 }
 function SetVel()
 {
   if (Random.value > .5) {
     vel.x = 10 * 10 * Random.value;
   }
   else {
     vel.x = -10 * 10 * Random.value;
   }
   if (Random.value > .5) {
     vel.z = 10 * 10 * Random.value;
   }
   else {
     vel.z = -10 * 10 * Random.value;
   }
 }
 
 function Update()
     {
      if (curTime < Direction) {
      curTime += 1 * Time.deltaTime;
   }
 
   else {
         SetVel();
            if (Random.value > .5) {
            Direction += Random.value;
 }
 
   else {
       Direction -= Random.value;
   }
         if (Direction < 1) {
         Direction = 1 + Random.value;
     }
     curTime = 0;
   }
 }
 
 
 function FixedUpdate()
     {
   chicken.rigidbody.velocity = vel;
 }
 
 
 function OnTriggerEnter(c:Collider)
 {
 SetVel();
   }

Anyone know a good way of going about this? Thanks in advance! :)

Jim

Comment
Add comment · Show 4
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 · Oct 27, 2012 at 11:51 AM 0
Share

are you aware the physics engine will do this for you totally automatically?

you just add a rigidbody (one click) and put colliders (say, a sphere collider) on your objects.

check unityGems.com for tutes

avatar image Nercoe · Oct 27, 2012 at 12:29 PM 0
Share

It still manages to slip through

avatar image Nercoe · Oct 27, 2012 at 12:43 PM 0
Share

the fences consist of around 20 game objects and are children of one object (one of the fences). If I add a rigid body to the parent it should do this for all of the fences right? At the moment it works, but on occasions it slips through the fence.

avatar image Nercoe · Oct 27, 2012 at 03:08 PM 0
Share

Does anyone know why it bounces back sometimes and not others?

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by aldonaletto · Oct 27, 2012 at 08:40 PM

If the fences form a square aligned to the axes x and z, the easiest solution is to give different names to them, like "top", "bottom", "left" and "right", and in OnTriggerEnter revert only the z or x velocity, like this:

 function OnTriggerEnter(fence: Collider){
   switch (fence.name){
     case "left":
     case "right":
       vel.x = -vel.x;
       break;
     case "top":
     case "bottom":
       vel.z = -vel.z;
       break;
   }
 }

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
avatar image
0

Answer by Epixam · Oct 27, 2012 at 05:18 PM

Your collision logic doesn't account for a bounce. When you enter the trigger you are just randomizing the direction and that direction could be any direction, including one that goes through the object (and since you are using OnTriggerEnter and not OnTriggerStay it will only be called one time when the object first enters). Have you considered turning IsTrigger OFF and instead using OnCollision? If you do that then you wouldn't need to worry about the change in direction from bounce, it would be accounted for automagically, and all you would need to worry about are the random changes in direction that you desire. Also its worth noting that

   if (Random.value > .5) {
     vel.x = 10 * 10 * Random.value;
   }
   else {
     vel.x = -10 * 10 * Random.value;
   }

Could be simplified to a single line, and will still give you the functionality you desire.

   vel.x = multiplier * (Random.value-.5);
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 Nercoe · Oct 27, 2012 at 05:32 PM 0
Share

Nice suggestion, I changed the code so when OnCollision, set a new velocity for x and z but all it does is move back and forwards now. Looks like it's progressing though, any tips? Thanks for the reply.

Additionally it just sticks to some objects. This is the only problem I have with it :) What would fix that? Basically it will bounce off things but sometimes it well get stuck in a corner and not move, and I do need them as a trigger because they get shot. Is there a way to bypass that?

avatar image
0

Answer by xtremepman · Oct 27, 2012 at 05:07 PM

Use a character controller instead!

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 Nercoe · Oct 27, 2012 at 05:24 PM 0
Share

How would that work?

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

12 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

Related Questions

Handling Animation and movement speed 0 Answers

Need player to walk in random direction 2 Answers

How to stop a character from moving out of player's control? 0 Answers

Boat collides with track(plane) below it while moving forward 0 Answers

Respawn random objects 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