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 Azial · Mar 15, 2013 at 06:01 PM · randomflipwalkstrange

Sometimes strange behaviour of my 'walk around' script

Hi,

I wrote two scripts:

  • script a) to spawn some prefab-NPC's randomly around an area with script b) attached

  • script b) is for random walk around movement

The NPC's gameobject transform position origin is exactly on its feet sole (sry, I'm from germany, don't know the exact term for this :). When I start the game, the NPC's get spawned properly, but some of them have really strange behaviour over time, especially when they touch the ground (note: in the script b) I calculate the raycast down from the position: feet + 1 local-up for tolerance reasons).

They sometimes start to flip around like crazy, i really don't know why... I guess somethings wrong with the "else"-condition calculation, but this seems just too simple. If you have any hint, please let me know!

Here's script a) to spawn the NPC's:

 //script to spawn things based on size and position of the gameobject attached
 //proper way to use: attach this to an empty gameobject with scale 1, 1, 1 first and attach a box collider which you deactivate
 //the box collider is only for visualisation of the area where we want to spawn things in, the gizmo show it
 //now, you can position, rotate and scale the gameobject how you like, the gizmo of the deactivated box collider show you the spawn-area
 
 var toSpawn : Transform;
 var count : int = 5;
 
 function Awake () {
     var cubeSize : Vector3 = transform.localScale;
     for (var i = 0; i < count; i++) {
         var newSpawn : Transform = Instantiate(toSpawn, transform.position, transform.rotation);
         newSpawn.Translate(Random.Range(-cubeSize.x * 0.5, cubeSize.x * 0.5), Random.Range(-cubeSize.y * 0.5, cubeSize.y * 0.5), Random.Range(-cubeSize.z * 0.5, cubeSize.z * 0.5));
         newSpawn.localScale *= Random.Range(1.0, 4.0); //just in my case; delete this line if you just want to spawn things discribed above
     }
 }


And here's the problematic script b) for movement:

 var explosion : Transform;
 var freedom : float = 5.0;
 private var curSpeed : float = 0.3;
 private var h : float = 1.0;
 private var trans : Transform;
 private var curNormal : Vector3 = Vector3.up;
 private var rndPos : Vector3;
 private var iniPos : Vector3;
 
 function Start() { //important! we use "Start" because it's executed later than "Awake" which is used to spawn this gameobject we want to control
     trans = transform; //link to transform since we use it a lot, so this increase performance
     iniPos = trans.position; //get the position where we spawned this gameobject first
     rndPos = CalcRndPos(freedom);
 }
 
 function Update () {
     var hit : RaycastHit;
     if(Physics.Raycast(trans.position + trans.up * h, -curNormal, hit, h * 1.1)) { //cast ray down the last normal-up we hit
         curNormal = hit.normal;
         var grndTilt : Quaternion = Quaternion.FromToRotation(Vector3.up, curNormal); //rotation from normal-vector
         var directionToRndPos : Vector3 = Vector3(rndPos.x, trans.position.y, rndPos.z) - trans.position; //direction to next random position on same height
         var rotToRndPos : Quaternion = Quaternion.LookRotation(directionToRndPos); //rotation to next random position on same height
         var calcedRot : Quaternion = grndTilt * rotToRndPos; //combine the rotations
         trans.rotation = Quaternion.Slerp (trans.rotation, calcedRot, 4*Time.deltaTime); //set rotation to the transform, but smoothly for natural movement
         trans.Translate(0, h - hit.distance, curSpeed * Time.deltaTime * trans.localScale.y); //walk on ground and forward
         if (directionToRndPos.magnitude <= curSpeed) rndPos = CalcRndPos(freedom); //if reached last random position, calculate new one
     } else {
         trans.Translate(-2 * Time.deltaTime * Vector3.up); //no ground under feet? fall down!
     }
 }
 
 function OnTriggerEnter(other : Collider) { //destroy if hit by something; delete function for testing without collider-component attched to gameobject
     if (other.rigidbody) {
         Instantiate(explosion, trans.position + trans.up * h * 0.5, trans.rotation);
         Destroy(gameObject);
     }
 }
 
 function CalcRndPos(maxDist : float) : Vector3 { //calculate random position around spawn/initial position
     var newRndPos : Vector3 = iniPos + Vector3(Random.Range(-maxDist, maxDist), 0, Random.Range(-maxDist, maxDist));
     return newRndPos;
 }
Comment
Add comment · Show 1
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 Azial · Mar 15, 2013 at 06:07 PM 0
Share

Ah, don't $$anonymous$$d, got it:

if(Physics.Raycast(trans.position + trans.up h, -trans.up, hit, h 1.1)) { …

fixed ;)

0 Replies

· Add your reply
  • Sort: 

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

Make an object, with a random movment, flip 1 Answer

How can i make my enemy walk random in my level? 1 Answer

Choose a random material for particle effect 1 Answer

Creating a light in game C# 1 Answer

Random.value sometimes doesn't instantiate my prefab(pic included) 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