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 Jessica · Dec 01, 2010 at 12:59 PM · transformvector3

I want to turn around an object

var targetA : GameObject;

var targetB : GameObject;

var speed : float = 0.1;

function FixedUpdate () { var weight = Mathf.Cos(Time.time speed 2 Mathf.PI) 0.5 + 0.5; transform.position= targetA.transform.position weight + targetB.transform.position (1-weight);

 if(transform.position==targetA.transform.position
    && gameObject.tag=="chicken"){
     transform.eulerAngles=Vector3(0,0,0);
 }

 if(transform.position==targetB.transform.position
    && gameObject.tag=="chicken"){
     transform.eulerAngles=Vector3(0,180,0);
 }

}

That is no to turn around an object, I want to know what is wrong.

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

2 Replies

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

Answer by skovacs1 · Dec 01, 2010 at 04:19 PM

This is what your code does:

//Let's assume these are actually assigned somewhere var targetA : GameObject; var targetB : GameObject;

var speed : float = 0.1;

function FixedUpdate () { //Weighting function which works about the same as //transform.position = Mathf.Lerp(targetA.transform.position, // targetB.transform.position, // Mathf.Smoothstep(0.0f,1.0f,Mathf.PingPong(Time.time speed,1.0f))); var weight = Mathf.Cos(Time.time speed 2 Mathf.PI) 0.5 + 0.5; transform.position = targetA.transform.position weight + targetB.transform.position * (1-weight);

 //Check if the position equals that of targetA's position and
 //the tag is "chicken" and then set the euler angles to 0
 if(transform.position==targetA.transform.position
    && gameObject.tag=="chicken"){
     transform.eulerAngles=Vector3(0,0,0);
 }

 //Check if the position equals that of targetA's position and
 //the tag is "chicken" and then set the euler angles to 0 and y to 180
 if(transform.position==targetB.transform.position
    && gameObject.tag=="chicken"){
     transform.eulerAngles=Vector3(0,180,0);
 }

}

This is essentially the same, but shorter:

//Let's assume these are actually assigned somewhere //Since you only use the transforms, just store those var targetA : Transform; var targetB : Transform;

var speed : float = 0.1;

function FixedUpdate () { transform.position = Mathf.Lerp(targetA.position, targetB.position, Mathf.Smoothstep(0.0f,1.0f,Mathf.PingPong(Time.time * speed,1.0f)));

 if(gameobject.tag == "chicken") {
     if(transform.position == targetA.position)
         transform.eulerAngles = Vector3.zero;
     if(transform.position == targetB.position)
         transform.eulerAngles = Vector3(0.0f,180.0f,0.0f);
 }

}

Your code does not rotate "around an object". There is no code here that gets a relative position and rotation to the object and rotates around it. That would be most easily done with Transform.RotateAround.

Your code will only ever rotate when it rotates the gameobject to which it is attached when its position is exactly equal to the position of targetA or targetB. Because Time.time is a floating point number and will almost never equal a whole number exactly (be it because of floating point error or simply because your FixedUpdate step is a little out of sync or something like that), let alone some exact multiple of 0.5/speed which you would need for your weight to equal exactly 1.0f or 0.0f and your position to be set exactly equal to targetA's or targetB's position, unless your FixedUpdate step and speed coincide perfectly the code in your if statements will almost never execute if at all.

To fix the problem, you need to check when you switch directions rather than your position and this can be most easily done by checking the weight you are using.

Something like:

var targetA : Transform; var targetB : Transform;

var speed : float = 0.1; private var weight : float = 0.0f; private var rising : boolean = true;

function FixedUpdate () { //if your speed and/or your FixedUpdate step are large enough, //use this more complicated version to catch spill-over / weight += Time.deltaTime speed; //PingPong var remainder = weight / 2.0f; remainder -= Mathf.Floor(remainder); if(remainder >= 0.5f) { if(rising && gameobject.tag == "chicken") transform.eulerAngles = Vector3(0.0f,180.0f,0.0f); rising = false; weight = (1.0f - remainder) 2.0f; } else if(!rising && gameobject.tag == "chicken") transform.eulerAngles = Vector3.zero; rising = true; weight = remainder 2.0f; } */

 //Otherwise, the simple version will work just fine:
 //PingPong
 if(rising) weight += Time.deltaTime * speed;
 else weight -= Time.deltaTime * speed;
 if(rising && weight >= 1.0f) {
     rising = false;
     weight = 2.0f - weight;
     if(gameobject.tag == "chicken")
         transform.eulerAngles = Vector3(0.0f,180.0f,0.0f);
 } else if(!rising && weight <= 0.0f) {
     rising = true;
     weight = -weight;
     if(gameobject.tag == "chicken") transform.eulerAngles = Vector3.zero;
 }

 transform.position = Mathf.Lerp(targetA.position, targetB.position, 
                                 Mathf.Smoothstep(0.0f,1.0f,weight));

}

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 Mike 12 · Dec 02, 2010 at 12:23 AM

What do you mean by "Turn around"? Are you trying to get your object to rotate around another a point in an elliptical trajectory, or are you trying to get your object to spin along an axis?

If you're trying to get your object to rotate around a point without it having to be a child of another game object, here's what you can do (Javascript):

private var distance:float; //This is the distance from the point that you want the object to rotate around. If you're going to have your object rotate around the game world's origin then the distance would be the magnitude of your object's vector var velocity:float = 1.0; enum RotationAxis {X, Y, Z}; var rotationAxis:RotationAxis = RotationAxis.Y; private var theta:float; private static var omega:float = 2.0*Mathf.PI; function Start() { t = 0.0; distance = transform.position.magnitude; switch(rotationAxis) { case RotationAxis.X: theta = Mathf.Deg2Rad(transform.eulerAngles.x); case RotationAxis.y: theta = Mathf.Deg2Rad(transform.eulerAngles.y); case RotationAxis.z: theta = Mathf.Deg2Rad(transform.eulerAngles.z); default:

         break;
 }

}

function FixedUpdate() {

 switch(rotationAxis)
 {
     case RotationAxis.X:
         transform.Translate(new Vector3(0.0, Mathf.Cos(theta)*distance, Mathf.Sin(theta)*distance) - transform.position, Space.World);
     case RotationAxis.y:
         transform.Translate(new Vector3(Mathf.Cos(theta)*distance, 0.0, Mathf.Sin(theta)*distance) - transform.position, Space.World);
     case RotationAxis.z:
         transform.Translate(new Vector3(Mathf.Cos(theta)*distance, 0.0, Mathf.Sin(theta)*distance) - transform.position, Space.World);
     default:
         break;
 }

}

If you're trying to spin your object along a pivot axis, then here's what you can do (JavaScript):

private var axisRotation:float;

enum RotationAxis {X, Y, Z}; var rotationAxis:RotationAxis = RotationAxis.Y; static var t:float;

var fullPeriod:float = 2*Mathf.PI; private var t:float; //Time ticker var updateRate:float = 1.0; //This is the rate at which your time ticker (t) is updated. function Start()

{

 transform.localRotation = Quaternion.identity;

 transform.localPosition = Vector3(0.0,0.0,0.0);

 axisRotation = 0.0;

 t = 0.0;

}

function FixedUpdate () {

 axisRotation = fullPeriod*(t/global.eSecondsDay);
 switch(rotationAxis)
     {
     case RotationAxis.X:
         transform.Rotate(axisRotation,0.0,0.0);
     case RotationAxis.y:
         transform.Rotate(0.0,axisRotation,0.0);
     case RotationAxis.z:
         transform.Rotate(0.0,0.0,axisRotation);
     default:
             break;
 }
     t += updateRate;

}

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

No one has followed this question yet.

Related Questions

A node in a childnode? 1 Answer

Transform continue direction 0 Answers

Clamp X position of object 1 Answer

Middle of screen vector 3? 2 Answers

Finding covered distance of a gameobject 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