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 /
  • Help Room /
avatar image
0
Question by Metalhawk93 · Mar 01, 2017 at 09:17 PM · c#camerascripting problemphysics.raycast

Object appears to glitch as it moves whenever the camera follows it.

Okay so this Question will seem a bit to big tackle, but I figured I'd ask for some help anyway.

So I am making a simple kart racing, and right now I'm I've got a good set on the physics of the car. Instead of using real physics, It relies on raycasting to influence its' movement simply as follows.

" By firing a raycast down from each corner of the vehicle a certain distance (hoverheight), and if it detects a hit, it adds an upward force."

It took a bit of brain work, but I think it I got the cars' controller script to work properly. The real problem is when it interacts with the camera.Now for the camera, I used a script I made based on this smooth follow script that I found on the UnifyCommunity website.

Go to site Here -->

now, when the camera views the player without the script, the player acts normally; smooth movement, no problems. However when the camera as the script, and when you move the player in game mode, that's when things get a bit problematic. As the camera views the player move around, the player glitches a bit. Not enough to break the game of course, though just enough for it to be noticed.

If anyone has any ideas, questions, or suggestions, be sure to reply or comment back on the matter. Just be sure to do some research and think over the question thoroughly. The coding for the scripts I'm using will be provided in this question for you to look over, in case you like to test out this scenerio for yourself. I'll also provide a link to the tutorial I used for making my character script, If your interested in making your own simple Kart racer. Think of said tutorial as a basis for more your own gameplay mechanics. Again, your words and support are well appreciated.

Thank you

Character Script

 using UnityEngine;
 using System.Collections;
 
 public class CarController : MonoBehaviour
 {
     Rigidbody body;
     float deadZone = 0.1f;
     public float m_groundedDrag = 3f;
     public float maxVelocity = 50;
     public float hoverForce = 1000;
     public float gravityForce = 1000f;
     public float hoverHeight = 1.5f;
     public GameObject[] hoverPoints;
  
     public float forwardAcceleration = 8000f;
     public float reverseAcceleration = 4000f;
     float thrust = 0f;
  
     public float turnStrength = 1000f;
     float turnValue = 0f;
  
     public ParticleSystem[] dustTrails = new ParticleSystem[2];
  
     int layerMask;
     
     void Start()
       {
         body = GetComponent<Rigidbody>();
         body.centerOfMass = Vector3.down;
      
         layerMask = 1 << LayerMask.NameToLayer("Vehicle");
         layerMask = ~layerMask;
       }
     
     // Uncomment this to see a visual indication of the raycast hit points in the editor window
 //  void OnDrawGizmos()
 //  {
 //
 //    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.blue;
 //        Gizmos.DrawLine(hoverPoint.transform.position, hit.point);
 //        Gizmos.DrawSphere(hit.point, 0.5f);
 //      } else
 //      {
 //        Gizmos.color = Color.red;
 //        Gizmos.DrawLine(hoverPoint.transform.position, 
 //                       hoverPoint.transform.position - Vector3.up * hoverHeight);
 //      }
 //    }
 //  }
     
     void Update()
       {
         // Main Thrust
         thrust = 0.0f;
         float acceleration = Input.GetAxis("Vertical");
         if (acceleration > deadZone)
             thrust = acceleration * forwardAcceleration;
         else if (acceleration < -deadZone)
             thrust = acceleration * reverseAcceleration;
      
         // Turning
         turnValue = 0.0f;
         float turnAxis = Input.GetAxis("Horizontal");
         if (Mathf.Abs(turnAxis) > deadZone)
             turnValue = turnAxis;
       }
     
     void FixedUpdate()
   {
 
     //  Do hover/bounce force
         RaycastHit hit;
         bool  grounded = false;
         for (int i = 0; i < hoverPoints.Length; i++)
         {
               var hoverPoint = hoverPoints [i];
             if (Physics.Raycast(hoverPoint.transform.position, -Vector3.up, out hit,hoverHeight, layerMask))
             {
                 body.AddForceAtPosition(Vector3.up * hoverForce* (1.0f - (hit.distance / hoverHeight)), hoverPoint.transform.position);
                 grounded = true;
             }
               else
               {
                 // Self levelling - returns the vehicle to horizontal when not grounded and simulates gravity
                 if (transform.position.y > hoverPoint.transform.position.y)
                 {
                     body.AddForceAtPosition(hoverPoint.transform.up * gravityForce, hoverPoint.transform.position);
                 }
                 else
                 {
                     body.AddForceAtPosition(hoverPoint.transform.up * -gravityForce, hoverPoint.transform.position);
                 }
               }
         }
             
         var emissionRate = 0;
         if(grounded)
         {
             body.drag = m_groundedDrag;
             emissionRate = 10;
         }
         else
         {
             body.drag = 0.1f;
             thrust /= 100f;
             turnValue /= 100f;
         }
 
         
 
     // Handle Forward and Reverse forces
     if (Mathf.Abs(thrust) > 0)
       body.AddForce(transform.forward * thrust);
 
     // Handle Turn forces
     if (turnValue > 0)
     {
       body.AddRelativeTorque(Vector3.up * turnValue * turnStrength);
     } else if (turnValue < 0)
     {
       body.AddRelativeTorque(Vector3.up * turnValue * turnStrength);
     }
 
     // Limit max velocity
     if(body.velocity.sqrMagnitude > (body.velocity.normalized * maxVelocity).sqrMagnitude)
     {
         body.velocity = body.velocity.normalized * maxVelocity;
     }
   }
 }
 

Camera Controller

 using UnityEngine;
 using System.Collections;
 
 public class CameraSmoothFollow : MonoBehaviour {
 
     public Transform target;
     // Use this for initialization
     public float distance;
     public float height;
     public float damping;
     public bool smoothRotation;
     public bool followBehind;
     public float rotationDamping;
     
     // Update is called once per frame
     void Update () {
         Vector3 wantedPosition;
         if(followBehind)
             wantedPosition = target.TransformPoint(0, height, -distance);
         else
             wantedPosition = target.TransformPoint(0, height, distance);
         
         transform.position = Vector3.Lerp (transform.position, wantedPosition, Time.deltaTime * damping);
         
         if (smoothRotation) {
             Quaternion wantedRotation = Quaternion.LookRotation(target.position - transform.position, target.up);
             transform.rotation = Quaternion.Slerp (transform.rotation, wantedRotation, Time.deltaTime * rotationDamping);
         }
         else transform.LookAt (target, target.up);
     }
 }
 


Arcade Style Bouncy Vehicle Physics Tutorials here! -->

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

0 Replies

· Add your reply
  • Sort: 

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

333 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

Related Questions

Making a pivot focused game 0 Answers

ScreenToWorldPoint not working 1 Answer

c# - error CS0103: The name `hit' does not exist in the current context (cardboard switching) 1 Answer

ScreenToWorldPoint error (C#) 1 Answer

my camera is rotating on Z axis when i just set it on X and Y 0 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