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 schmonk · Dec 11, 2013 at 10:39 PM · lerplinecastbehaviourwall collision

Camera Collision Lerp Problem

Hello there! I'm working on a 3D Jump'n'run. I have a camera script that uses Linecasts to move the camera closer to my Player if the Line collides with the layer that i set up for this purpose. Although the camera lerps towards it's target, it seems to stop lerping for no reason and can still be moved through the ground.Even though the Linecast still collides with my layer.

How do I prevent the camera from doing this?

Any help is appreciated, I thank you in advance :)

Here is a video that further illustrates my problem

(note: to prevent this behaviour, i have tried a few things myself, such as:

  • adding a rigidbody to the camera and using .addforce on collision

  • on collision: freezePosition and freezeRotation with the Rigidbody mentioned above

  • inverting x and y input on collision

  • add a float value to x and y on collision

  • multiply the values of x and y on collision

  • applying transform.translate on collision(with, corresponding to the line that collided, a vector3 "pulling" in the opposite direction)

and, at least the way i tried it, all these attempts failed.)

Here is my Script:

 using UnityEngine;
 using System.Collections;
 public class MouseOrbitCSHARP : MonoBehaviour 
     
 {
     public Transform Target;
 
     public float Distance = 20.0f;
     public float DistanceMin = 7.5f;
     public float DistanceMax = 25f;
 
     public float xSpeed = 200.0f;
     public float ySpeed = 200.0f;
     public float yMinLimit = -80.0f;
     public float yMaxLimit = 80.0f;
 
     private float x;
     private float y;
     public LayerMask layermask;
 
     void Awake()
     {
         Vector3 angles = transform.eulerAngles;
         x = angles.x;
         y = angles.y;
         Screen.lockCursor = true;
 
     }
         
     void LateUpdate()
     
     {
 
 
         if(Target != null)
         {
             x += (float)(Input.GetAxis("Mouse X") * xSpeed * 0.02f);
             y -= (float)(Input.GetAxis("Mouse Y") * ySpeed * 0.02f);
             y = ClampAngle(y, yMinLimit, yMaxLimit);
             Quaternion rotation = Quaternion.Euler(y, x, 0);
 
 
 
             Vector3 position = rotation * (new Vector3(0.0f, 0.0f, -Distance)) + Target.position;
 
             transform.rotation = rotation;
             transform.position = position;
             
             Vector3 topLeft = (new Vector3(transform.position.x-2,transform.position.y+2,transform.position.z+5));
             Vector3 topRight = (new Vector3(transform.position.x+2,transform.position.y+2,transform.position.z+5));
             Vector3 botRight = (new Vector3(transform.position.x+2,transform.position.y-2,transform.position.z+5));
             Vector3 botLeft = (new Vector3(transform.position.x-2,transform.position.y-2,transform.position.z+5));
             //Debugging the main line to the camera transform and the 4 lines related to the positions around the camera transform
             Debug.DrawLine(Target.position,transform.position, Color.green);
             Debug.DrawLine(Target.position,topLeft, Color.green);
             Debug.DrawLine(Target.position,topRight, Color.green);
             Debug.DrawLine(Target.position,botLeft, Color.green);
             Debug.DrawLine(Target.position,botRight, Color.green);
             //Cast lines, lerp on collision
                 if (Physics.Linecast(Target.position,transform.position,layermask))
                 {
                     Debug.DrawLine(Target.position,transform.position, Color.red);
                     Distance = Mathf.Lerp(Distance, DistanceMin, Time.deltaTime * 1.0f);
 
                         if(Distance == DistanceMin)
                             {
                             }
                 }
                 
                 else if (Distance < DistanceMax && !Physics.Linecast(Target.position,transform.position,layermask) )
                 {
                     Distance = Mathf.Lerp(Distance, DistanceMax + 0.1f, Time.deltaTime * 1.5f);
                 }
 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
             //top Left 
 
             if (Physics.Linecast(Target.position,topLeft, layermask))
                 {
                     Debug.DrawLine(Target.position,topLeft, Color.red);
                     Distance = Mathf.Lerp(Distance, DistanceMin, Time.deltaTime * 1.0f);
                     Debug.Log ("topLEFT");
                     
                         if(Distance == DistanceMin)
                         {
                         }
                 }
                 else if (Distance < DistanceMax && !Physics.Linecast(Target.position,topLeft,layermask) ) // there it is!
                 {
                     Distance = Mathf.Lerp(Distance, DistanceMax + 0.1f, Time.deltaTime * 1.5f);
                 }
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
             //top right 
             if (Physics.Linecast(Target.position,topRight,layermask))
                 {
                     Debug.DrawLine(Target.position,topRight, Color.red);
                     Distance = Mathf.Lerp(Distance, DistanceMin, Time.deltaTime * 1.0f);
                     Debug.Log ("topRIGHT");
                         
                         if(Distance == DistanceMin)
                         {
                 
                         }
                 }
                 
                 else if (Distance < DistanceMax && !Physics.Linecast(Target.position,topRight,layermask)) // there it is!
                 {
                     Distance = Mathf.Lerp(Distance, DistanceMax + 0.1f, Time.deltaTime * 1.5f);
                 }
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
             //bottom left 
             if (Physics.Linecast(Target.position,botLeft,layermask))
                 {
                     Debug.DrawLine(Target.position,botLeft, Color.red);
                     Debug.Log ("bottomLEFT");
 
                     Distance = Mathf.Lerp(Distance, DistanceMin, Time.deltaTime * 1.0f);
         
                         if(Distance == DistanceMin)
                         {
 
         
                         }
 
                     
                 }
                 
                 else if (Distance < DistanceMax && !Physics.Linecast(Target.position,botLeft,layermask) )
                 {
                     Distance = Mathf.Lerp(Distance, DistanceMax + 0.1f, Time.deltaTime * 1.5f);
                 }
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
             //bottom right 
                 if (Physics.Linecast(Target.position,botRight,layermask))
                 {
                     
                 Debug.DrawLine(Target.position,botRight, Color.red);
                 Debug.Log ("bottomRIGHT");
 
                     Distance = Mathf.Lerp(Distance, DistanceMin, Time.deltaTime * 1.0f);
     
                         if(Distance == DistanceMin)
                         {
                         }
                 }
                 else if (Distance < DistanceMax && !Physics.Linecast(Target.position,botRight,layermask)) // there it is!
                     {
                         Distance = Mathf.Lerp(Distance, DistanceMax + 0.1f, Time.deltaTime * 1.5f);
                     }
         }
     }
 
     private 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

16 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

Related Questions

Camera Stutter During Distance Correction 0 Answers

Return 3rd Person camera to original position 0 Answers

Weird artifacts in cg shader with lerp() 1 Answer

Lerp isn't completing itself 3 Answers

Lerp doesn't work after using a lerp? 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