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 Ochreous · Jun 12, 2015 at 08:11 PM · c#transformpositionlocalefficiency

C# Creating a More Efficient Script

This has been driving me nuts. So I want to create a script that makes a gameobject rotate. I was able to achieve this but I still think this could be done better. I initially tried creating a vector2, assigning values to it and making the axes I don't want to move 0. But with Mathf.PingPong it gave me an error saying the transform.position attempt was not valid. Do you guys have any suggestions for improvements of my script?

 using UnityEngine;
 using System.Collections;
 
 public class Ping_Pong_Sway : MonoBehaviour {
     public float sway;
     public string swayAlongVector;
     void Update() {
         if(swayAlongVector == "x"){
         transform.localPosition = new Vector3(Mathf.PingPong(Time.time * 5, sway), transform.localPosition.y, transform.localPosition.z);
         }
         else if(swayAlongVector == "y"){
         transform.localPosition = new Vector3(transform.localPosition.x, Mathf.PingPong(Time.time * 5, sway), transform.localPosition.z);
         }
         else if(swayAlongVector == "x+y"){
         transform.localPosition = new Vector3(transform.localPosition.x, Mathf.PingPong(Time.time * 5, sway), transform.localPosition.z);
         }
     }
 }

Comment
Add comment · Show 3
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 FortisVenaliter · Jun 12, 2015 at 08:14 PM 0
Share

I have no idea what you're asking. Could you include some screenshots of what you're expecting vs. what you're seeing?

avatar image FortisVenaliter · Jun 12, 2015 at 08:15 PM 0
Share

Also, if it gives you an error message, post it here. That helps us diagnose the problem a lot easier.

avatar image Ochreous · Jun 13, 2015 at 02:38 AM 0
Share

I'm looking for a better way of creating my script. It calls transform.localPosition 3 times and uses what I think is an extra variable in swayAlongVector. I haven't found an alternative to this however.

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Tick_Talos · Jun 13, 2015 at 03:17 PM

If your just trying to make it rotate in place (not around anything) you can just do something like this

 tf.Rotate(-Vector2.up * rotateSpeed * Time.deltaTime);

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 PlanetVaster · Jun 13, 2015 at 03:18 PM

You could use a switch statement

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 DiegoSLTS · Jun 13, 2015 at 03:54 AM

You just want to refactor your code?

First, I think you have a bug, you do the same for "y" and for "x+y".

Second, I guess you could avoid most of that repetition writing it like this:

  using UnityEngine;
  using System.Collections;
  
  public class Ping_Pong_Sway : MonoBehaviour {
      public float sway;
      public string swayAlongVector;
      void Update() {
          //this check is not neccesary if swayAlongVector will alywas have one of this values
          if (swayAlongVector == "x" || swayAlongVector == "y" || swayAlongVector == "x+y") {
              float aux = Mathf.PingPong(Time.time * 5, sway); //write this only once
 
              Vector3 pos = Vector3.zero;
              pos.z = transform.localPosition.z;
          
              if(swayAlongVector == "x" || swayAlongVector == "x+y"){
                  pos.x = aux;
              }
              if(swayAlongVector == "y" || swayAlongVector == "x+y"){
                  pos.y = aux; 
              }
 
              transform.localPosition = pos;
          }
      }
  }

That code should work like your code.

Another improvement would be to use an enum instead of strings for the swayAlongVector variable. And would be even better if the enum is a "flags" enum: http://www.dotnetperls.com/enum-flags

EDIT: Anyway, this code is not more "efficient", that's something you have to measure, but with something so simple it's probably an insignificant difference. Your original code doesn't create useless variables nor call the same function more than once, on each update only one of the conditions will be true, so only one line of code will get called. You should aim for cleannes, and maintainability instead of efficience in this case.

For example, it's more maintainable if you write that PingPong line only once in case you have to change something (change in one place instead of 3). It's easier to understand what the code does if you change the localPosition once at the end and use the swayAlongVector to only change the x and y values you'll use. But it's all subjective, someone might think other options are better for other reasons.

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 Bunny83 · Jun 13, 2015 at 02:11 PM 0
Share

Just like to add that your refactored code does not the same as the original. If "swayAlongVector" is either "x" or "y" you set the y or x position to 0 as you initialized your pos with Vector3.zero. You should have initialized pos with localPosition.

It's true that calculating the pingpong value only in one place is more maintainable since each case will need the value anyways.

However swayAlongVector is just a selector (which would be better as enum, as you said) which is considered constant for an instance. In such a case i wouldn't mix the different cases as it makes it more entangled.

Also even you merged the pingpong calculation and the assignment into one place, you duplicated the comparing of the string values. If you want to add for example 3 "rotate" cases as well and in that run you rename the parameters from "x" ro "mx" for "move x" and add "rx" for "rotate x" your code is less maintainable.

Finally your script executes 4(best case) up to 7(worst case) string compare operations while the original code executes 1(best case) up to 3(worst case).

To me your solution is less readable and actually less efficient ^^

I would suggest using an enum like this:

 public enum SwayOperation { X, Y, XY };
 public float sway;
 public float speed = 5f;
 public SwayOperation swayType;
 
 void Update()
 {
     float val = $$anonymous$$athf.PingPong(Time.time * speed, sway); 
     Vector3 pos = transform.localPosition;
     switch (swayType)
     {
     case SwayOperation.X:
         pos.x = val;
         break;
     case SwayOperation.Y:
         pos.y = val;
         break;
     case SwayOperation.XY:
         pos.x = pos.y = val;
         break;
     }
     transform.localPosition = pos;
 }

In the hypothetical case of adding more "operations" i would even move the assignment of localPosition into each case. It doesn't matter how long the code is as long as you can easily follow what it does. Also maintainability can't be generalized. You can consider something like "what might get changed more likely?", but you never know what might be changed in the future. If you know it already then you usually implement it right away ^^.

avatar image
0

Answer by Yuanfeng · Jun 13, 2015 at 03:20 PM

Make Sure that you didn't set the sway value to be 0. Mathf.PingPong(float t, float length); the parameter require to be bigger than 0.

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

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

22 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

Related Questions

Multiple Cars not working 1 Answer

C# round transform.position floats to .5 2 Answers

Freeze transform.rotation.x 1 Answer

Why are floats not accessing my inputted values? 1 Answer

Obtaining the relative size of a Models body part 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