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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by tpkq · May 26, 2012 at 08:44 AM · javascriptlookatmovetowardsscreenpointtorayjerky

Jerky Camera or Object motion

First I would like to say Hello to everyone. I am new to this Forum, and relatively new to Unity. I hope to find the solution to my problem which has given me quite some headache. I am trying to make a 3D space shooter. The flying object is supposed to orientate itself by using the transform.LookAt function and accelerate using the rigidbody.AddRelativeForce. For now I have the camera following the Vessel by simply parenting it to it. It works great only that the movement is a bit jerky at times, it lags mostly noticeable when turning. (the script is attached to the Vessel)

 function Update()
 { 
     var scr2world: Ray;
     scr2world = Camera.main.ScreenPointToRay(Input.mousePosition);
     var vel = transform.position + transform.forward*4;
     var focus = Vector3.MoveTowards(vel,scr2world.GetPoint(80),Time.deltaTime * 10);
 
     transform.LookAt(focus);
 
 Debug.DrawRay(scr2world.origin,scr2world.direction*50,Color.yellow);
 Debug.DrawLine(transform.localPosition,scr2world.GetPoint(80),Color.red);
 Debug.DrawLine(transform.localPosition,focus,Color.black);
 }

The Rays and Lines I drew to verify the coordinates. I expect the black line to align itself with the red. It does but when I comment out transform.LookAt(focus); it remains at Vector3(vel) and vibrates. I have tired Lerp,Slerp,MoveTowards,smoothDamp. But ran into the same problems or others. Is my methode wrong to achieve the desired motion? Or is my code missing something? Is the jerky motion generated by the Vessles motion or the cameras motion?

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 whydoidoit · May 26, 2012 at 08:52 AM 0
Share

If that script is attached to the vessel then as it doesn't affect the camera's position any jerkiness must be due to the movement of the vessel. (Presu$$anonymous$$g nothing else moves the camera).

But is looks to me like the vessel isn't actually moving - you never seem to change its position; well not in that code at least.

I would guess at vibration being to do with the ray being relative to mouse coordinates and not quite getting the exact same result every time - especially when multiplied by 80.

avatar image whydoidoit · May 26, 2012 at 08:53 AM 0
Share

Oh yes, and welcome on board :)

avatar image tpkq · May 26, 2012 at 05:14 PM 0
Share

Thank you for you answer, The camera is for now simply a child of the vessel, although I have tried a self written camera follow script using Vector3.Slerp or Unitys standard assets script smooth follow. So far all seem to share the jerky motion problem. The vessel is accelerated using another script

 rigidbody.AddRelativeForce (0,0,velZ*velZb); //velZb stands for my boost factor

 if (Input.Get$$anonymous$$ey("x") && velZ < 520)
     {
     velZ += 40;
     rigidbody.drag = stDragZ; //stDragZ stand for my standard Drag value
     }

I see what you mean, but the vibration occurs even when the vessel is still. When I would expect the black line to be drawn in towards the specified point on the ray, in my case 80? Are there any suggestions to use a different methode to eventually achieve the same? I would really like to keep the LookAt function or emulate it. and keep the rigidbody.AddRelativeForce because it generates a nice Force Vector calculation.

1 Reply

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

Answer by whydoidoit · May 26, 2012 at 05:29 PM

So your problem is due to the fact that you are multiplying your focus position calculation by Time.deltaTime. This will be different on every call to the Update function, causing your focus position to fluctuate slightly. Then you are doing a LookAt which is also moving the camera very slightly.

Not sure why you need that Time.deltaTime as you are not actually moving anything here (your physics is doing that). This therefore seems like a calculation that should be at a fixed position - drop the Time.deltaTime and replace it with a constant (0.02 for the physics step etc).

Comment
Add comment · Show 4 · 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 tpkq · May 26, 2012 at 06:18 PM 0
Share

Thank you for your great help. You have gotten to the root of the fluctuation, I took your advice and got rid of the Time.deltaTime and the fluctuation is gone. The black line tries to align itself to the red. Depending on how large the constant is that you suggested deter$$anonymous$$es how far it aligns itself with the red. I assume thats normal since the constant represents the speed, it simply shows how far it makes it in one frame, considering that the Update function is called every frame. Although the jerky motion when flying persists. Now that the fluctuation is ruled out as the source of the problem. I believe it to be the fact that the Ray direction from the camera drives the the vessels orientation which drives the Cameras orientation which ultimately changes the Ray direction. Its like a loop that cant keep up. The Question is I need the camera to follow the vessel and want the ray direction to give the Vector3 for the Vessel to look at. Any suggestions on how to achieve that?

avatar image whydoidoit · May 26, 2012 at 06:41 PM 0
Share

First thought, and it's just off the top of my head, but try putting that code in LateUpdate() ins$$anonymous$$d, so that the physics and everything have moved and moved the camera first.

avatar image tpkq · May 26, 2012 at 07:08 PM 0
Share

YES !!! Definately the right direction. Although LateUpdate() didnt work. Ins$$anonymous$$d I tried the FixedUpdate(). which completely removes the jerky motion. Thank you for your Great Help and quick responses. I have another problem which is caused by the same code but unrelated to the jerky motion. Since im new to this forum I ask you. Should I make another post?

avatar image whydoidoit · May 26, 2012 at 07:09 PM 0
Share

Yes please

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make an object move towards an object at a constant pace, no matter how fast the object is? 0 Answers

Look at wont update? 2 Answers

transform.LookAt overrides rigidbody rotation 0 Answers

Slow down with multiple enmies using LookAt 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 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