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 arturoza · Sep 15, 2014 at 02:08 AM · quaternioneulerangleseuler

Quaternion from US to C#

Hi, I have this script in US, I have been trying to translate it to C# but I don't know how to use quaternions, eulers and that stuff I hope you can help me here is the code in JS and below the code in C#, thanks in advance.

 #pragma strict
 var qTo : Quaternion;
 var speed = 1.25;
 var rotateSpeed = 3.0;
 var timer = 0.0;
  
 function Start() 
     {
          qTo = Quaternion.Euler(Vector3(0.0,0.0, Random.Range(-30, 30)));
     }
  
 function Update() {
  
     timer += Time.deltaTime;
       
         if(timer > 2) 
             {
                  qTo = Quaternion.Euler(Vector3(0.0,0.0,Random.Range(-30, 30)));  
                  timer = 0.0;
             }
         transform.rotation = Quaternion.Slerp(transform.rotation, qTo, Time.deltaTime * rotateSpeed);
         transform.Translate(Vector3.forward * speed * Time.deltaTime);
 }



and the code in C#, I only got one error in the line 24 where transform.position is:

     using UnityEngine;
     using System.Collections;
     
     public class POLICEscript : MonoBehaviour {
         private Quaternion qTo;
         float speed = 1.5f;
         float rotateSpeed = 3.0f;
         public float timer = 0.0f;
             
         void Start()
             {
                 qTo = Quaternion.Euler (new Vector3 (0.0f, 0.0f, Random.Range (-30f, 30f)));
             }
         void Update()
             {
                 timer += Time.deltaTime;
     
                 if(timer > 2)
                     {
                         qTo = Quaternion.Euler (new Vector3 (0.0f, 0.0f, Random.Range (-30f, 30f)));
                         timer = 0.0f;
     
                         //transform.position = Quaternion.Slerp(transform.rotation, qTo, Time.deltaTime * rotateSpeed);
                         transform.position = Quaternion.Slerp(transform.rotation, qTo, Time.deltaTime * rotateSpeed);
                         transform.Translate    (Vector3.forward * speed * Time.deltaTime);
             }
         }
     
     }
 
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 arturoza · Sep 15, 2014 at 02:24 AM 0
Share

I will also appreciate if you help me to get rid of unnecessary parts

avatar image AlucardJay · Sep 15, 2014 at 02:35 AM 1
Share

Show the code you have converted as well (so this doesn't just read as a 'convert-my-code' question.

Everything is practically the same, except of course for variable declarations; method prefixes; new keyword for class/structs.

 private Quaternion qTo;
 void Start()
 something = new Quaternion .....

Here's some links I found useful in converting between C# and JS :

  • http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html

  • http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?

  • http://fragileearthstudios.com/2011/10/18/unity-converting-between-c-and-javascript-2/

avatar image arturoza · Sep 15, 2014 at 02:22 PM 0
Share

thanks you so much alucardj, I fixed some errors, some of them by simply adding a f after the float, I didn't know even if is 10,0 you have to add an f, but I still have an error left. I updated the question and added the code in C# as you told me, hope you can help me.

1 Reply

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

Answer by HarshadK · Sep 15, 2014 at 02:23 PM

Actually it is a very simple thing that you are missing.

On the line with the error you are setting the position as Quaternion where as it should be rotation like:

Current:

 transform.position = Quaternion.Slerp(transform.rotation, qTo, Time.deltaTime * rotateSpeed);

Change to:

 transform.rotation = Quaternion.Slerp(transform.rotation, qTo, Time.deltaTime * rotateSpeed);

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 arturoza · Sep 15, 2014 at 02:27 PM 0
Share

thank you so much Harshad$$anonymous$$, I'm still wondering how the code can work with transform.position in US, and with transform.rotation in C#, odd, but thank you so much!!! you da real $$anonymous$$VP!

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

24 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

Related Questions

Need help with storing Rotation in a Quaternion 1 Answer

Fixing the rotation around a particular axis using Quaternions? 2 Answers

Quaternion, Eulers and Problems 1 Answer

Is there no way to get reliable angles from a rigidbody? 2 Answers

How to fix flipping Euler anlges? 0 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