Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by bakkoto · Aug 11, 2017 at 05:19 PM · c#unity 5rotationrigidbody2dmouseposition

Orbital cannon jitters while following/facing mouse position

Hello everyone, I have a simple scenario where a flying 2d dynamic Rigibody "Spaceship" gameobject floating in space using addforce and addtorque (translation and rotation). A child (of "Spaceship") gameobject "Cannon" that orbits around the spaceship relative to the current mouse position. The cannon can only move between object x and y (limited angle) and is always facing the mouse position while in range. See the picture below for more details:

alt text

Everything was ok. But after implementing a camera script that follows the spaceship (in LateUpdate and it´s attached to the main camera), the orbital cannon starts to jitter while the ship is moving. My guess is that the mouse position (ScreenToWorldPoint) is now changing constantly while the camera is moving and the orbital cannon is trying to follow and face the position. Probably that´s what´s causing this weird jitter motion!! You can have a look at the OrbitalCannon controller. The cannon object does not have a 2d Rigibody component attached to it. Only the spaceship does.

 using UnityEngine;
 
 public class OrbitalCannonController : MonoBehaviour
 {
     public float SlidingSpeed = 1;
     private Transform orbitalCannon;
     private float orbitRadius;
     private float currentAngle;
     private Vector3 centerToMouseVector; // vector from Ship center to the world mouse position
     private Vector3 nexPosition;
 
    void Awake()
     {
         orbitalCannon = this.transform; // cach transform.
         orbitRadius = Vector2.Distance(Ship.position, orbitalCannon.position);
         centerToMouseVector = orbitalCannon.position - Ship.position;
     }
 
     private void Update()
     {
         //get the vector between mouse position (projected in game world) and the center of the circle (Ship)
         centerToMouseVector = Camera.main.ScreenToWorldPoint(Input.mousePosition) - Ship.position;
         centerToMouseVector.z = 0; // zero z axis since we are using 2d
 
         float x = Vector2.SignedAngle(centerToMouseVector, Ship.up);
         if ((x > 20 && x < 70) || centerToMouseVector.magnitude < orbitRadius) return;
 
         // we normalize the new direction so you can make it equal to the radius length
         // then we add it to the circle (aka Ship) center position
         nexPosition = Ship.position + (orbitRadius * centerToMouseVector.normalized);
 
         // get the correct angle between the circle (Ship) and mouse position in the world space
         currentAngle = Mathf.Atan2(centerToMouseVector.y, centerToMouseVector.x) * Mathf.Rad2Deg;
 
         orbitalCannon.position = Vector3.MoveTowards(orbitalCannon.position, nexPosition, Time.deltaTime * SlidingSpeed); // lerp cannon to the position
         orbitalCannon.rotation = Quaternion.Euler(new Vector3(0, 0, currentAngle - 90)); // rotate orbital cannon to face the mouse.
     }
 }

Can anyone help me solve this problem? What am I doing wrong here? And if there is any better/optimized approach to achieve the same control, I would love to know it (using rotateAround() or something similar) because I believe that the current implementation is a performance overkill.

spaceship-with-orbital-cannon.png (24.7 kB)
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 Chanisco · Aug 11, 2017 at 06:22 PM 0
Share

Do you have multiple camera's in your scene?

avatar image bakkoto · Aug 11, 2017 at 07:59 PM 0
Share

No. Only one with a simple script attached to it

 using UnityEngine;
 
 public class CameraController : $$anonymous$$onoBehaviour {
 
     public GameObject Target;        //Public variable to store a reference to the player game object
     private Vector3 offset;            //Private variable to store the offset distance between the player and camera
 
 
     void Start () 
     {
         // distance between the player's position and camera's position.
         offset = transform.position - Target.transform.position;
     }
     
     
     void LateUpdate () 
     {
         // Follow target
         transform.position = Target.transform.position + offset;
     }
 }
avatar image bakkoto · Aug 17, 2017 at 02:04 PM 0
Share

I tried to move the whole calculation stuff to LateUpdate but it didn´t work. Any suggestions?

1 Reply

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

Answer by Igor_Vasiak · Aug 17, 2017 at 03:06 PM

Try to put the calculation stuff in FixedUpdate(). If it doesn't work change the camera’s script’s calculation to Update() or FixedUpdate().

This is what every method do:

  • Update runs every frame, just before LateUpdate.

  • LateUpdate runs just after Update does, and runs just before the Camera renders.

  • FixedUpdate runs on a constant timing, and by default as close as possible to the physics calculation.

Let me know if it worked.

Comment
Add comment · Show 3 · 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 bakkoto · Aug 21, 2017 at 07:56 PM 0
Share

Thank you for answering. Yes, It´s working if you put the calculation stuff inside "FixedUpdate" function or changing camera script´s calculation from "LateUpdate" to "Update". But It's generally a bad idea to use FixedUpdate for anything except physics. $$anonymous$$aybe not in my case because the mother ship movement is calculated inside "Fixedupdate". As for using "Update" ins$$anonymous$$d "lateUpdate" for the camera script, I would rather not do that because the camera is always tracking objects that might have moved inside Update already. So am I right? Any child of the 2d rigibody physics Spaceship, like the orbital cannon for example (or any other child) must have their stuff calculated inside FixedUpdate? Because they carry the physics behavior from the parent with them?

avatar image Igor_Vasiak bakkoto · Aug 22, 2017 at 02:14 PM 0
Share

Yep. That should be it. I mean, I don't know if every single child object needs to have the things implemented inside FixedUpdate(), but at least the ones that will move and use physics on their own.

I'm glad to know that I helped you out.

avatar image bakkoto Igor_Vasiak · Aug 23, 2017 at 08:45 PM 1
Share

I think so. But we still need to ask an expert about it just to be sure. Anyway, thank you for helping me sir :)

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

400 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 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 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 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 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 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 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 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 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 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 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 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 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 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

Flip over an object (smooth transition) 3 Answers

Cube Gravity - Apply Gravity to a Curve 0 Answers

Unity C# 2D Adding Velocity on rotation 1 Answer

Can't do Quaternion.AngleAxis twice. 1 Answer

How can I turn the direction of bicycle in Unity? 3 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