• 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 kadamx · Nov 17, 2014 at 10:41 AM · rotatepingpong

Rotate enemy with Mathf.PingPong

Hi Everyone!

I'm creating a 2D platformer with round enemies in it. I want those enemies to move back and forth on x-axis. Mathf.PingPong works for that perectly, but i also want to rotate them on z-axis so it looks like they are rolling on the ground back and forth, but i'm doing something wrong. The enemy is parented to an empty gameobject so i can adjust it's local position.

Here's the code:

   function Update () 
 {
   transform.localPosition.x = Mathf.PingPong(Time.time, Dist);
   transform.localRotation.z = Mathf.PingPong(Time.time, Rot);
   }

However the rotation stops when it reaches 180degree. Tried with Rotation instead localRotation but it didn't work. Can someone give me a tip how to make it right? Thank You!

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 Scribe · Nov 17, 2014 at 10:44 AM 1
Share

transform.localRotation is a Quaternion, you need to convert between euler rotations and quaternions before you can set it like this, search the documentation for eulerAngles, Quaternion.Euler, Quaternion.AngleAxis etc

avatar image kadamx · Nov 17, 2014 at 12:12 PM 0
Share

Of course. Thank You it's working now.

1 Reply

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

Answer by robertbu · Nov 17, 2014 at 03:13 PM

If you are careful, you can use eulerAngles to do the rotation you are looking for. Rather than a separate PingPong() for rotation, you will have better results if you use the same value. That way the rotation direction will match the move direction:

 pragma strict
  
 var dist = 5.0;
 var moveSpeed = 1.0;
 var rot = 90.0;
  
 private var v = Vector3.zero;
   
 function Update () 
 {
     var x = Mathf.PingPong(moveSpeed * Time.time, dist);
     //var x = (Mathf.Sin(moveSpeed * Time.time) + 1.0) / 2.0 * dist;
     transform.localPosition.x = x;
     v.z = rot * -x;
     transform.eulerAngles = v;
 }


The commented out line is an alternate way of doing the 'ping pong' of the value that produces a results that feels more realistic. Also look at the following answer for more information about assigning values to the components of transform.rotation:

http://answers.unity3d.com/questions/799824/oafatwhy-does-transformrotationx-45-not-work.html

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 kadamx · Nov 18, 2014 at 05:30 PM 0
Share

Thank You. $$anonymous$$uch better this way.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

27 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

Related Questions

Camera rotation around player while following. 6 Answers

Limit Rotations with Min and Max on Mobile 1 Answer

Camera 'FixedUpdate' and Rotate function problem 0 Answers

Face to direction, in a Mario Galaxy style game? 1 Answer

Rotation Help 2 Answers


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