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 RaulG · Jan 20, 2015 at 01:27 AM · rotationmovementsimplify

Simplifying a script

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class FlyingEnemy : MonoBehaviour 
 {
     private float amplitudeZ = 5.0f;
     private float amplitudeY = 3.0f;
     private float omegaZ = 2.0f;
     private float omegaY = 2.0f; 
     public EnemySpawn eSpawn;
     
     public int place;
 
 
     private float timeI;
     
     private float mTime0;
     private float mTime1;
     private float mTime2;
     private float mTime3;
     private float mTime4;
 
     //keeps current position of objects
     public List<Vector3> posList = new List<Vector3>();
 
     void setVars()
     {
         posList.Add (eSpawn.eSnake [0].transform.position);
         posList.Add (eSpawn.eSnake [1].transform.position);
         posList.Add (eSpawn.eSnake [2].transform.position);
         posList.Add (eSpawn.eSnake [3].transform.position);
         posList.Add (eSpawn.eSnake [4].transform.position);
     }
     
     public void Snaker()
     {
 
 
         float y0;
         float y1;
         float y2;
         float y3;
         float y4;
 
         Vector3 v0 = posList [0];
         Vector3 v1 = posList [1];
         Vector3 v2 = posList [2];
         Vector3 v3 = posList [3];
         Vector3 v4 = posList [4];
 
         Vector3 leader0 = eSpawn.eSnake [0].transform.position;
         Vector3 leader1 = eSpawn.eSnake [1].transform.position;
         Vector3 leader2 = eSpawn.eSnake [2].transform.position;
         Vector3 leader3 = eSpawn.eSnake [3].transform.position;
 
 
         Vector3 toLeader0 = leader0 - transform.position;
         Vector3 toLeader1 = leader1 - transform.position;
         Vector3 toLeader2 = leader2 - transform.position;
         Vector3 toLeader3 = leader3 - transform.position;
 //
 //        Quaternion wantDir0 = Quaternion.LookRotation(toLeader0);
 //        Quaternion wantDir1 = Quaternion.LookRotation(toLeader1);
 //        Quaternion wantDir2 = Quaternion.LookRotation(toLeader2);
 //        Quaternion wantDir3 = Quaternion.LookRotation(toLeader3);
 
         timeI += Time.deltaTime;
 
         mTime0 += Time.deltaTime;
         if (mTime0 > 0.1f)mTime1 += Time.deltaTime;
         if (mTime1 > 0.1f)mTime2 += Time.deltaTime;
         if (mTime2 > 0.1f)mTime3 += Time.deltaTime;
         if (mTime3 > 0.1f)mTime4 += Time.deltaTime;
 
         
         float z = (omegaZ * amplitudeZ);
 
               y0 = Mathf.Abs (amplitudeY * Mathf.Sin (omegaY * mTime0));
               y1 = Mathf.Abs (amplitudeY * Mathf.Sin (omegaY * mTime1));
               y2 = Mathf.Abs (amplitudeY * Mathf.Sin (omegaY * mTime2));
               y3 = Mathf.Abs (amplitudeY * Mathf.Sin (omegaY * mTime3));
               y4 = Mathf.Abs (amplitudeY * Mathf.Sin (omegaY * mTime4));
 
 
         eSpawn.eSnake [0].transform.position = new Vector3 (0.0f, v0.y + y0, v0.z -z * timeI);
 
         eSpawn.eSnake [1].transform.position = new Vector3 (0.0f, v1.y + y1, v1.z -z * timeI);
 //        eSpawn.eSnake [1].transform.rotation = Quaternion.RotateTowards(transform.rotation, wantDir0, 360*Time.deltaTime);
 
         eSpawn.eSnake [2].transform.position = new Vector3 (0.0f, v2.y + y2, v2.z -z * timeI);
 //        eSpawn.eSnake [2].transform.rotation = Quaternion.RotateTowards(transform.rotation, wantDir1, 360*Time.deltaTime);
 
         eSpawn.eSnake [3].transform.position = new Vector3 (0.0f, v3.y + y3, v3.z -z * timeI);
 //        eSpawn.eSnake [3].transform.rotation = Quaternion.RotateTowards(transform.rotation, wantDir2, 360*Time.deltaTime);
 
         eSpawn.eSnake [4].transform.position = new Vector3 (0.0f, v4.y + y4, v4.z -z * timeI);
 //        eSpawn.eSnake [4].transform.rotation = Quaternion.RotateTowards(transform.rotation, wantDir3, 360*Time.deltaTime);
 
     }
     
 
     // Update is called once per frame
     void Update () 
     {
         Invoke ("setVars", 0.5f);
         Invoke ("Snaker", 1.0f);
     }
 
 
 }
 


That problem with this is the rotation part. Which is why it's commented out. If it isn't it causes huge amounts of lag.

So I was wondering if I could get some help altering this script so that I can get the rotation in there without causing my computer to explode.

Comment
Add comment · Show 2
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 getyour411 · Jan 20, 2015 at 01:49 AM 0
Share

I doubt you want new invokes made as often as Update is called!

avatar image RaulG · Jan 20, 2015 at 01:54 AM 0
Share

Doesn't Invoke only get called once?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Reimus · Jan 20, 2015 at 02:00 AM

instead of Invoke in Update() do InvokeRepeating(functionname, interval, interval) in a start function.

public void InvokeRepeating(string methodName, float time, float repeatRate);

 void Start () {
     InvokeRepeating("setVars", 0.5f, 0.5f);
     InvokeRepeating("Snaker", 1.0f, 1.0f);
 }
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 RaulG · Jan 20, 2015 at 02:07 AM 0
Share

That gives this really weird stop motion thing.

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

26 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

Related Questions

Rotating Character 1 Answer

Rotate an object such that it is theta degrees relative to another object 1 Answer

Top down space game Rotation help. 0 Answers

Making RigidBody face direction for movement 1 Answer

Random insect movement 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