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 adgeofgalaxy · Dec 15, 2014 at 11:27 AM · transform.positionstartraystartingrays

[CLOSED] Unexplainable fluctuations when setting a gameobject's transform.position

Attach the following script to any gameobject with a mesh filter and renderer.

Does anyone have any idea why the gameobject very randomly starts fluctuating in its x and y coordinates? I swear I've debug.log tested it to clearly show that xOffset and yOffset variable remains steady, yet for some reason when trying to apply it to an actual transform.position, they suddenly start fluctuating violently. I suspect it may have something to do with the trig functions, but I don't see how. Anyone?

 using UnityEngine;
 using System.Collections;
  
 public class Test : MonoBehaviour
 {
  
     public float speed;
     public Plane playerPlane;
 
     public float distanceFromPlayer = 5;
 
     private float xOffset;
     private float yOffset;
     
     Vector3 newPosition;
 
     void Start ()
     {
         playerPlane = new Plane (Vector3.back, transform.position);
     }
 
 
     void FixedUpdate () 
     {
         print ( newPosition );
 
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
  
         float hitdist = 0.0f;
 
         if (playerPlane.Raycast (ray, out hitdist)) 
         {
             Vector3 targetPoint = ray.GetPoint(hitdist);
  
             float AngleRad = Mathf.Atan2 (targetPoint.y - transform.position.y, targetPoint.x - transform.position.x);
 
             xOffset = Mathf.Cos ( AngleRad ) * distanceFromPlayer;
             yOffset = Mathf.Sin ( AngleRad ) * distanceFromPlayer;
             print ( newPosition );
             newPosition = new Vector3 ( xOffset, yOffset, 0 );
                transform.position = newPosition;
 
                newPosition = new Vector3 ( xOffset, yOffset, 0 );
             transform.position = newPosition;
         }
     }
 }

Comment
Add comment · Show 12
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 taxvi · Dec 15, 2014 at 11:32 AM 0
Share

why wouldn't you take the contents of the LateUpdate and put them at the line 42?

avatar image adgeofgalaxy · Dec 15, 2014 at 11:39 AM 0
Share

oh that was from a previous test, I should but it doesn't take away the fluctuations

avatar image taxvi · Dec 15, 2014 at 11:41 AM 0
Share

mm... how about changing the declaration of the newPosition to Vector3? tho it should not matter for Unity

avatar image adgeofgalaxy · Dec 15, 2014 at 11:43 AM 0
Share

yeah the problem still persists

avatar image taxvi · Dec 15, 2014 at 11:43 AM 0
Share

going a little off topic, there is a constant in unity $$anonymous$$athf.RadToDeg that you can multiply your radian values to get degrees equivalent. and yea, that's basically the (180f / $$anonymous$$athf.PI)

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by meat5000 · Dec 15, 2014 at 11:52 AM

Move your code from FixedUpdate to Update. LateUpdate is running at framerate whilst FixedUpdate is running independently from that.

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 adgeofgalaxy · Dec 15, 2014 at 11:54 AM 0
Share

yep, done but still the fluctuations

avatar image
0

Answer by Baste · Dec 15, 2014 at 11:45 AM

So I can't quite tell what you're trying to do with your trig stuff, but both Cos and Sin takes radians, not degrees. I'm pretty sure that you're sending in values as if the functions took degrees. That should explain the behavior not being what you expect.

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 adgeofgalaxy · Dec 15, 2014 at 11:48 AM 0
Share

ah yes: I'm trying to make the gameobject orbit around a point in a way that faces the mouse; changing it to radians, its now definitely better resembling my desired motion, but the fluctuations still seem to persist, although less violently.

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

29 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 avatar image avatar image

Related Questions

Method in Start() not running, but in Update(), why? 2 Answers

How to make the object stop moving within a certain distance with the target. 1 Answer

Initialising List array for use in a custom Editor 1 Answer

move player to finger position 1 Answer

Why can I not start Unity 5 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