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 ThierryH · Sep 08, 2014 at 09:58 AM · raycastinghovercraft

two Raycast`s causes lag

Hello, I need your help. I make a Hover-car movement script. Distance to ground and the Hover movement is calculated by Raycast, I make another Raycast, because there was a problem - hover flew too high if you hold the button pressed. But now with two Raycast it cause a lag - Hover moves in jerks. Interpolate does not help.

How can I make smooth movement without jerking? Please, help.

 public class hoverController_v7 : MonoBehaviour {
     
         Rigidbody carBody;
         float deadZone = 0.1f;
     
         public float hoverForce = 9.0f;        
         public float hoverHeight = 2.0f;    
         public GameObject[] hoverPoints;
     
         public float forwardAcceleration = 100.0f;    
         public float backwardAcceleration = 25.0f;
         public float currThrust = 0.0f;
     
         public float turnStrength = 10.0f;        
         public float currTurn = 0.0f;                
     
         public GameObject leftAirBrake;
         public GameObject rightAirBrake;
     
         int layerMask;
     
         
         void Start()
         {
             carBody = GetComponent<Rigidbody>();
     
             layerMask = 1 <<     LayerMask.NameToLayer("Characters");
             layerMask = ~layerMask;
     
     
         }
     
         void OnDrawGizmos()
         {
             // сила hoverForce
             RaycastHit hit;
             for(int i = 0; i < hoverPoints.Length; i++)
             {
                 var hoverPoint = hoverPoints[i];
                 if(Physics.Raycast(hoverPoint.transform.position,
                                    -Vector3.up, out hit,
                                    hoverHeight,
                                    layerMask))
                 {
                     Gizmos.color = Color.yellow;
                     //Color if correctly alligned
                     Gizmos.DrawLine(hoverPoint.transform.position, hit.point);
                     Gizmos.DrawSphere(hit.point, 0.5f);
                 }
                 else
                 {
                     Gizmos.color = Color.red;
                     //Color if incorrectly alligned
                     Gizmos.DrawLine (hoverPoint.transform.position, hoverPoint.transform.position - Vector3.up * hoverHeight);
                 }
             }
         }
     
         void Update()
         {
             // main thrust
             currThrust = 0.0f;
             float aclAxis = Input.GetAxis("Vertical");
             if(aclAxis > deadZone)
             {
                 currThrust = aclAxis * forwardAcceleration;
             }
             else if(aclAxis < -deadZone)
             {
                 currThrust = aclAxis * backwardAcceleration;
             }
     
             // Turning
             currTurn = 0.0f;
             float turnAxis = Input.GetAxis("Mouse X") * 2;
             if(Mathf.Abs(turnAxis) > deadZone)
                 currTurn = turnAxis;
     
         }
     
         void FixedUpdate()
         {
             // hover force
             RaycastHit hit;
             for(int i = 0; i < hoverPoints.Length; i++)
             {
                 var hoverPoint = hoverPoints[i];
                 if(Physics.Raycast(hoverPoint.transform.position, 
                                    -Vector3.up, out hit,
                                    hoverHeight, layerMask))
                     carBody.AddForceAtPosition(Vector3.up * hoverForce * (1.0f -(hit.distance / hoverHeight)), 
                                                hoverPoint.transform.position);
                 else
                 {
                     if(transform.position.y > hoverPoint.transform.position.y)
                         carBody.AddForceAtPosition(hoverPoint.transform.up * hoverForce, hoverPoint.transform.position);
                     else
                         //add force to car
                         carBody.AddForceAtPosition(hoverPoint.transform.up * -hoverForce, hoverPoint.transform.position);
                 }
             }
     
             // forward
             RaycastHit hit2;
             if(Physics.Raycast(transform.position, -transform.up, out hit2))
             {
                 if(hit2.distance < 2)
                 {
                     if(Mathf.Abs(currThrust) > 0)
                     {
                         carBody.AddForce(transform.forward * currThrust);
                                 
                     }
                 }
             }
     
             // turn
             if(currTurn > 0)
             {
                 carBody.AddRelativeTorque(Vector3.up * currTurn * turnStrength);
             }
             else if(currTurn < 0)
             {
                 carBody.AddRelativeTorque(Vector3.up * currTurn * turnStrength);
             }
     
             
         }
     }
 








Comment
Add comment · Show 2
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 robertbu · Sep 08, 2014 at 10:27 PM 1
Share

Two raycasts ins$$anonymous$$d of one should not be a performance problem. Since your two raycast are almost identical, it is more likely the two bodies of code inside the two raycasts are somehow fighting with each other.

avatar image khenkel · Sep 09, 2014 at 07:24 AM 0
Share

I think the lags are not meant in terms of performance (fps) but in terms of the vehicle's movement. That's not entirely clear here though.

2 Replies

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

Answer by ThePersister · Sep 09, 2014 at 08:12 AM

Hmmm, tough one. If you'd like to track down your perfomance issues, I'd suggest using the Unity Profiler, look up some tutorials on Youtube, etc.

Once you get the hang of the profiler, you can track down all the way to your methods where your performance is being used the most and how much % of the total.

Right now, it's pretty hard to see / understand how the script applies in the game. I mean, I can see a hovervehicle flying about, and the player being able to trust it about, but pictures of your scene would help.

Performance tricks:

  • Each .transform is actually a "GetComponent()", so consider storing it in a variable at start once, and use that variable in the rest of the script. (Saves 11+ GetComponents per frame)

Code for that:

 Transform mTransform, mHoverPointTransform;
 
 void Start()
         {
             // Use further down below :)
             mTransform = transform;
             mHoverPointTransform = hoverPoint.transform;
 
             carBody = GetComponent<Rigidbody>();
             layerMask = 1 <<     LayerMask.NameToLayer("Characters");
             layerMask = ~layerMask;
         }

  • Furthermore, you're using OnDrawGizmos, these will only aid you in the scene view, and I'm not sure how heavy they are on your system, but for as far as game performance, you can ignore it. So, testing your actual performance, perhaps you should close the scene view, or point the camera to an inactive area, so Unity doesn't have to process too much there. If this does cause issues, you'll see this in the profiler as "Overhead", that's when you know the scene view or Debug.Logs are fcking you over :3

Aside from the performance tricks, it seems weird having to apply 2 raycasts to stop you from going up, you should add your own friction based on your velocity.

Add more scene pictures, and I might be able to help with the whole, don't fly away hovercraft please issue. :)

But the performance tricks should help in general :)

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 ThierryH · Sep 09, 2014 at 01:06 PM

Thanks for the answer. I attached two images:
1(idle) - when the game is running, but I`m nothind doing 2(moving) - when alt textthe hover is moving


1_idle.png (432.8 kB)
1_moving.jpg (258.9 kB)
Comment
Add comment · Show 2 · 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 ThePersister · Sep 09, 2014 at 03:21 PM 0
Share

Hey there, thx for the images, it's a bit hard to help out not having access to the project itself, but I won't ask for that, so don't worry.

I find it strange that your performance is better whenever you're moving. You have a lot of GPU usage when it comes down to Shadows, unfortunately I'm pretty unadept at shadows, so I won't be able to help much on that matter by personal experience. But you should check that up, because being at 60 FPS is the good life.

As far as CPU usage, it all comes down to the "Others" spikes. I'd suggest commenting out the Gizmos as I stated before, and closing the scene tab, so we don't have to worry about that overhead.

Then you should click and drag with your mouse in the CPU area, and put this vertical indication bar at one of the Brown Spikes, that's when you want to look at the Overview below your recorded data, where you will see the performance take-up at That time :)

Also, I now get why you need the Raycast, as you hover over the ground you want to always be a certain distance away from it, it makes sense now, my apologies.

Yea, I don't think there is any magic solution, just look through the profiler whilst you're at the Time of the Spikes and try to optimize the code / scenepiece that causes it.

If all this is worthy as an answer, please accept it :) Best of Luck! ;3

avatar image ThierryH · Sep 09, 2014 at 07:04 PM 0
Share

Thank you. I will accept your answer anyway. But just two more question)) 1) How to limit the hover max height above the terrain, in other way(not what I have now)? 2) And how to strafe (not turn) the hover, as in video below: http://www.youtube.com/watch?v=JE0ZLq5bobc I tried to add transform.rotation = Quaternion.Euler(0,0,25); but it work uncorrectly, and the code: carBody.AddTorque(Vector3.right * 20); or carBody.AddTorque(0,0,30); both didn`t work in general.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to steer a hovercraft that aligns itself to a surface normal? 0 Answers

Using CrossHair GUI to Determine Ray Spread 0 Answers

Raycast From Camera to Object 1 Answer

Audio play problem on raycast hit colider.gameObject.tag 1 Answer

Pushing away rigidbody from the direction of raycast hit 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