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
0
Question by solocs · Oct 22, 2018 at 09:11 AM · cameraraycastdirectionclampstutter

Need help with third-person camera stuttering involving raycasts and clamp

I'm trying to create a script for a third-person camera that will prevent the camera from having its view obstructed by walls by having the camera move in front of the player. The script works but the camera stutters very badly and I've tried rearranging functions inside Update/FixedUpdate for the last couple of days to no avail.

**Edit: I found one possible source to the problem and that is Raycast simultaneously returns true and false. I tested Raycast in a brand new project with very basic code and Raycast does the exact same thing when it's put into any Update/OnTriggerStay. At this point, I have no idea on why it's doing this. How can I get Raycast to only return true or false exclusively?

 public float mouseSens; //mouse sensitivity
 public float dstFromTarget;
 public Transform target; //the target the camera follows
 public Vector2 pitchMinMax; //pitch range values in the form of a vector2
 public Vector2 scrollMinMax;
 public Light minimapLight;


 public float rotationSmoothTime;
 Vector3 rotationSmoothVel;
 Vector3 currentRotation;

 public RaycastHit collisionInBetween;
 bool collisionExists;

 float yaw; //horizontal camera movement
 float pitch; //vertical camera movement
 float scrollFactor;
 float startClipPlaneValue;

 void Start () {
     scrollFactor = 0.2f;
     scrollMinMax = new Vector2(20, 30);
     dstFromTarget = scrollMinMax.y/scrollMinMax.x;
     mouseSens = 3.5f;
     pitchMinMax = new Vector2(-60, 85);
     rotationSmoothTime = 0.12f;
     startClipPlaneValue = 2.0f;
 }

 void Update () {
     yaw += Input.GetAxis("Mouse X") * mouseSens; //moves camera horizontally using mouse leftright
     pitch -= Input.GetAxis("Mouse Y") * mouseSens; //moves camera vertically using mouse updown
     pitch = Mathf.Clamp(pitch, pitchMinMax.x, pitchMinMax.y); //set the pitch range limit

     currentRotation = Vector3.SmoothDamp(currentRotation, new Vector3(pitch, yaw), ref rotationSmoothVel, rotationSmoothTime);
     transform.eulerAngles = currentRotation;

     ScrollForDistance();

     if (collisionExists)
     {
         //Debug.Log(targetToCamera.magnitude + ", " + (collisionInBetween.point - target.position).magnitude);
         //Camera.main.nearClipPlane = (collisionInBetween.point - target.position).magnitude;
         //Debug.DrawLine(transform.position, collisionInBetween.point, Color.red);
         float rayToTargetDistance = (collisionInBetween.point - target.position).magnitude;
         if (rayToTargetDistance <= (transform.forward * dstFromTarget).magnitude)
             transform.position = target.position - transform.forward * rayToTargetDistance;
     }
     else
     {
         transform.position = target.position - transform.forward * dstFromTarget; //make the position of this camera the same as the target's position
     }
 }

 private void FixedUpdate()
 {
     dstFromTarget = Mathf.Clamp(dstFromTarget, scrollMinMax.x, scrollMinMax.y);
     Vector3 targetToCamera = transform.position - target.position;
     Vector3 offsetValue = transform.forward * Camera.main.nearClipPlane; //from camera's forward transform vector, offset forward by cameranearplane units.
     Vector3 targetToCameraOffset = targetToCamera + offsetValue; //add offset starting from transform.forward and facing forward with respect to this object
     collisionExists = Physics.Raycast(target.position, targetToCamera, out collisionInBetween, targetToCameraOffset.magnitude);
 }

 private void OnPreCull()
 {
     minimapLight.enabled = false;
 }

 private void OnPostRender()
 {
     minimapLight.enabled = true;
 }

 void ScrollForDistance() {
     if (Input.GetAxis("Mouse ScrollWheel") > 0f) // forward
     {
         dstFromTarget -= scrollFactor;
     }
     else if (Input.GetAxis("Mouse ScrollWheel") < 0f) // backwards
     {
         dstFromTarget += scrollFactor;
     }
 }
Comment
Add comment · Show 1
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 solocs · Oct 22, 2018 at 07:00 PM 0
Share

Still no progress

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

177 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

Related Questions

Raycasting with Rotation of Camera 0 Answers

Get 2D Collider with 3D Ray 2 Answers

Move character in direction its facing 1 Answer

Start raycast from object 1 Answer

How to get Camera Collision Detection on Terrain 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