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 Imamul125 · Oct 15, 2018 at 07:43 AM · errorcamera rotatethird-person-camera

can someone help me out what is wrong in this code?

this script was working fine. untill i modified to add camera collision. my code was fine i got the result, i wanted to achieve i.e my mouse would rotate around the character freely(orbit). then i modified the script to add camera collision. now it is causing shaking camera movement. please note i have also different script in my camera to keep looking to my character.

     {
     public Transform target;
     public Transform target1;      // this is for collision purpose use player here
     public float distance = 10.0f;
 
     public float xSpeed = 250.0f;
     public float ySpeed = 120.0f;
 
     public float yMinLimit = -20f;
     public float yMaxLimit = 80f;
 
     private float x = 0.0f;
     private float y = 0.0f;
 
     public float moveSpeed = 5;
     public float returnSpeed = 9;
     public float wallPush = 0.7f;
     public float closestDistanceToPlayer = 2f;
     public float evenCloserDistanceToPlayer = 1f;
 
     public LayerMask collisionMask;
     private bool pitchLock = false;
     public Vector2 pitchMinMax = new Vector2(-48, 85);
     float pitch;
     Vector3 currentRotation;
     public float rotationSmoothTime = 8f;
     Vector3 rotationSmoothVelocity;
 
 
     // @script AddComponentMenu("Camera-Control/Mouse Orbit")
 
     void Start()
     {
         var angles = transform.eulerAngles;
         x = angles.y;
         y = angles.x;
 
         // Make the rigid body not change rotation
         if (GetComponent<Rigidbody>())
             GetComponent<Rigidbody>().freezeRotation = true;
     }
 
     void LateUpdate()
     {
         if (target)
         {
            var position = new Vector3(0.0f, 0.0f, -distance) + target.position;
            CollisionCheck(position);
             if (!pitchLock)
             {
                 x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
                 y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
 
                 y = ClampAngle(y, yMinLimit, yMaxLimit);
 
                 var rotation = Quaternion.Euler(y, x, 0);
               //  var position = new Vector3(0.0f, 0.0f, -distance) + target.position;
 
                 transform.rotation = rotation;
                 transform.position = position;
             }
             else
             {
                 x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
                 pitch = pitchMinMax.y;
                 currentRotation = Vector3.Lerp(currentRotation, new Vector3(pitch, x), rotationSmoothTime * Time.deltaTime);
             }
         }
     }
 
     private void WallCheck()
     {
         Ray ray = new Ray(target1.position, -target1.forward);
         RaycastHit hit;
         if (Physics.SphereCast(ray, 0.5f, out hit, 0.7f, collisionMask))
         {
             pitchLock = true;
         }
         else
             pitchLock = false;
     }
 
 
     private void CollisionCheck(Vector3 retpoint)
     {
         RaycastHit hit;
         if(Physics.Linecast(target1.position, retpoint, out hit, collisionMask))
         {
             Vector3 norm = hit.normal * wallPush;
             Vector3 p = hit.point + norm;
 
 
             if(Vector3.Distance(Vector3.Lerp(transform.position, p, moveSpeed * Time.deltaTime), target1.position) <= evenCloserDistanceToPlayer)
             {
 
             }
             else
             {
                 transform.position = Vector3.Lerp(transform.position, p, moveSpeed * Time.deltaTime);
             }
             return;
         }
 
         transform.position = Vector3.Lerp(transform.position, retpoint, returnSpeed * Time.deltaTime);
         pitchLock = false;
     }
 
 
 
     static float ClampAngle(float angle, float min, float max)
     {
         if (angle < -360)
             angle += 360;
         if (angle > 360)
             angle -= 360;
         return Mathf.Clamp(angle, min, max);
     }
 }
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

181 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

Related Questions

How to limit the rotation of my third person camera? 1 Answer

How to make third person camera were the player walks in the direction in which the camera is directed 2 Answers

Compiler Error, Cannot Go Into Playmode 2 Answers

HELP!! unexpected token: If 1 Answer

Error CS8025: Parsing Error 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