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 /
  • Help Room /
avatar image
0
Question by Snakox · Aug 02, 2016 at 03:05 PM · scripting problemmovement scriptfly

fly movement

Hello,

This is my first time on unity and I have some probleme in this script ( the fly can't fly in all the area) #pragma strict

 var displacement:Vector3 = Vector3(5,2,5);
 var desplVel:float = 1;
 
 
 private var pos0:Vector3;
 
 private var iniRndX:float;
 private var iniRndY:float;
 private var iniRndZ:float;
 
 private var audioOn:boolean=false;
 
 private var audioSourceMax:int = 4;
 private var audioSourceCurr:int = 0;
 
 function Start () {
     iniRndX = Random.Range(Random.Range(0,200), 500);
     iniRndY = Random.Range(Random.Range(0,200), 500);
     iniRndZ = Random.Range(Random.Range(0,200), 500);
     
     desplVel *= Random.Range(0.5, 2);
     
     pos0 = transform.localPosition;
     transform.localEulerAngles.y = Random.Range(1,360);
 }
 
 function Update() {
     
     //// Delete this Lines if you haven't an Audio Source attached to the GameObject
     if (!audioOn){
         if (Random.value < 0.01) {
             if(CheckAudioSourcesMax()) {
                 GetComponent.<AudioSource>().Play();
                 audioOn= true;
             } else {
                 GetComponent.<AudioSource>().enabled = false;
                 audioOn = true;
             }
         }
     }    
     ////
 }
 
 function FixedUpdate () {
     UpdateDespl();
 }
 
 function UpdateDespl() {
 
     var x:float = (Mathf.PerlinNoise(Time.time*desplVel +iniRndX, iniRndX) -0.46525) *displacement.x;
     var y:float = (Mathf.PerlinNoise(iniRndY, iniRndY+ Time.time*desplVel) -0.46525) *displacement.y;
     var z:float = (Mathf.PerlinNoise(iniRndZ, iniRndZ+ Time.time*desplVel) -0.46525) *displacement.z;
     
     
     var despl:Vector3 = Vector3(x,y,z);
     var rot:Quaternion = Quaternion.LookRotation(despl, Vector3.up);
     rot.eulerAngles.x = 270;
     rot.eulerAngles.z = 0;
     rot.eulerAngles.y += 90;
     
     GetComponent.<Rigidbody>().AddForce(despl, ForceMode.Force);
     transform.rotation = Quaternion.Lerp(transform.rotation, rot, 0.09);
      
     if ((transform.localPosition-pos0).sqrMagnitude>displacement.sqrMagnitude) {
         GetComponent.<Rigidbody>().AddForce((pos0 -transform.localPosition)*2, ForceMode.Force);
     }    
 }
 
 function CheckAudioSourcesMax():boolean {
     var maxNum:int = 0;
     var flyScripts:FlyBehav[] = FindObjectsOfType(FlyBehav);
     for(var flyScript:FlyBehav in flyScripts) {
         maxNum = Mathf.Max( flyScript.IncreaseAudioSourcesCurr(), maxNum);
     }
     if(maxNum<=audioSourceMax)    return true;
     else return false;
 }
 
 function IncreaseAudioSourcesCurr():int {
     audioSourceCurr ++;
     return audioSourceCurr;
 }


and I would like to know how to add a limit border to stay on the screen( maxX, minX, maxY, MinY) and the fly can fly in all the area like in this code

 #pragma strict
  var maxX = 6.17;
  var minX = -6.06;
  var maxY = 3.08;
  var minY = -5.63;
  
  private var tChange: float = 0; // force new direction in the first Update
  private var randomX: float;
  private var randomY: float;
  var moveSpeed = 10;
 
 function Start () {
 
 }
 
 function Update () {
      // change to random direction at random intervals
      if (Time.time >= tChange){
          randomX = Random.Range(-2.0,2.0); // with float parameters, a random float
          randomY = Random.Range(-2.0,2.0); //  between -2.0 and 2.0 is returned
          // set a random interval between 0.5 and 1.5
          tChange = Time.time + Random.Range(0.5,1.5);
      }
 
 
      transform.Translate(Vector3(randomX,randomY,0) * moveSpeed * Time.deltaTime);
      // if object reached any border, revert the appropriate direction
      if (transform.position.x >= maxX || transform.position.x <= minX) {
         randomX = -randomX;
      }
      if (transform.position.y >= maxY || transform.position.y <= minY) {
         randomY = -randomY;
      }
      // make sure the position is inside the borders
      transform.position.x = Mathf.Clamp(transform.position.x, minX, maxX);
      transform.position.y = Mathf.Clamp(transform.position.y, minY, maxY);
 
 }


Can anyone help me please ?

Thank's

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

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

76 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

Related Questions

My 2d movement script isn't working 0 Answers

Noob Here... Can you please give Script for moving a object into random Direction. 0 Answers

Fix Player Movement Script,Fix Player Movement 0 Answers

why does my player move when anywhere on screen is touched? 1 Answer

Jittery 3D Turning 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