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 d4nj0 · Nov 21, 2015 at 04:18 PM · script.camera-lookcamera movement

Orbit camera with mouse control script problems

Hi guys, I'm trying to make a third person marble platformer game and found this script online that enables me to orbit the player and move the view with the mouse but I got a problem. When the player moves behind an object the camera snaps to it's minimum distance from the player but doesn't snap back once the object is out of the way. This makes me have to scroll back with the mouse wheel every time it happens.

I've got no idea on how to fix this so any help would be greatly appreciated.

This is the script that I have: using UnityEngine; using System.Collections; [AddComponentMenu("Camera-Control/Mouse Orbit with zoom")] public class MouseOrbitImproved : MonoBehaviour { public Transform target; public float distance = 5.0f; public float xSpeed = 120.0f; %|-121265975_4|% public float yMinLimit = -20f; %|94790627_6|% public float distanceMin = .5f; %|-1309372864_8|% private Rigidbody rigidbody; float x = 0.0f; float y = 0.0f; // Use this for initialization %|1369685140_13|% %|-1425643440_14|% Vector3 angles = transform.eulerAngles; x = angles.y; y = angles.x; rigidbody = GetComponent<Rigidbody>(); // Make the rigid body not change rotation %|262759726_20|% { %|-1024185870_22|% } } void LateUpdate () { %|-803193625_27|% { %|566064197_29|% y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f; y = ClampAngle(y, yMinLimit, yMaxLimit); %|511844482_32|% distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*5, distanceMin, distanceMax); RaycastHit hit; if (Physics.Linecast (target.position, transform.position, out hit)) %|-2111377030_36|% distance -= hit.distance; %|-430734957_38|% Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance); %|-1086449615_40|% transform.rotation = rotation; %|1354344678_42|% } } public static float ClampAngle(float angle, float min, float max) { if (angle < -360F) angle += 360F; if (angle > 360F) angle -= 360F; %|-1542173141_51|% } }

Comment
Add comment · Show 3
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 Domo23000 · Nov 21, 2015 at 06:23 PM 0
Share

press the 101010 button and then paste the code in to make it a lot easier to read

avatar image d4nj0 Domo23000 · Nov 22, 2015 at 12:27 AM 0
Share

I did, dunno why it came out this way. it was looking fine when posted...`using UnityEngine; using System.Collections;

[AddComponent$$anonymous$$enu("Camera-Control/$$anonymous$$ouse Orbit with zoom")] public class $$anonymous$$ouseOrbitImproved : $$anonymous$$onoBehaviour { %|21353932_1|% %|2119513985_2|% public float distance = 5.0f; %|-1436610347_4|% public float ySpeed = 50f; %|-2045021080_6|% %|1526089662_7|% %|581643164_8|% %|1678793086_9|% %|1882850125_10|% %|-375249229_11|% %|-1675583003_12|% private Rigidbody rigidbody;

%|32840933_15|% float y = 0.0f; %|541982102_17|% %|1253687667_18|% %|818444616_19|% %|1581547236_20|% %|-996125286_21|% %|821362789_22|% %|-1243507389_23|% %|64174672_24|% %|-1616665804_25|% %|1648709565_26|% %|2060026496_27|% %|1175160055_28|% %|308302270_29|% %|-1656012909_30|% } %|-273750130_32|% %|1995535531_33|% %|1262513946_34|% { if (target) %|-310352386_37|% %|-2107229183_38|% %|-484912158_39|%

%|-2135468032_41|%

         Quaternion rotation = Quaternion.Euler(y, x, 0);

%|1848528464_44|% %|-866202068_45|% %|523656513_46|% %|-240059982_47|% if (Physics.Linecast (target.position, transform.position, out hit)) %|-1281019538_49|% %|-1741625481_50|% } %|1056605878_52|% %|-1328614516_53|% %|-648635059_54|% %|-722956583_55|% %|2118233532_56|% } %|1484113543_58|% %|848806890_59|% %|-1282872457_60|% %|-71424169_61|% %|1203695871_62|% %|-654435889_63|% %|1078620462_64|% %|-1848865403_65|% %|-142699574_66|% %|-1653633932_67|% }`

avatar image d4nj0 Domo23000 · Nov 22, 2015 at 12:27 AM 0
Share

using UnityEngine; using System.Collections;

[AddComponent$$anonymous$$enu("Camera-Control/$$anonymous$$ouse Orbit with zoom")] public class $$anonymous$$ouseOrbitImproved : $$anonymous$$onoBehaviour {

 public Transform target;
 public float distance = 5.0f;
 public float xSpeed = 50f;
 public float ySpeed = 50f;
 
 public float y$$anonymous$$inLimit = -1f;
 public float y$$anonymous$$axLimit = 80f;
 
 public float distance$$anonymous$$in = 2f;
 public float distance$$anonymous$$ax = 15f;
 
 private Rigidbody rigidbody;
 
 float x = 0.0f;
 float y = 0.0f;
 
 // Use this for initialization
 void Start () 
 {
     Vector3 angles = transform.eulerAngles;
     x = angles.y;
     y = angles.x;
     
     rigidbody = GetComponent<Rigidbody>();
     
     // $$anonymous$$ake the rigid body not change rotation
     if (rigidbody != null)
     {
         rigidbody.freezeRotation = true;
     }
 }
 
 void LateUpdate () 
 {
     if (target) 
     {
         x += Input.GetAxis("$$anonymous$$ouse X") * xSpeed * distance * 0.02f;
         y -= Input.GetAxis("$$anonymous$$ouse Y") * ySpeed * 0.02f;
         
         y = ClampAngle(y, y$$anonymous$$inLimit, y$$anonymous$$axLimit);
         
         Quaternion rotation = Quaternion.Euler(y, x, 0);
         
         distance = $$anonymous$$athf.Clamp(distance - Input.GetAxis("$$anonymous$$ouse ScrollWheel")*5, distance$$anonymous$$in, distance$$anonymous$$ax);
         
         RaycastHit hit;
         if (Physics.Linecast (target.position, transform.position, out hit)) 
         {
             distance -=  hit.distance;
         }
         Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
         Vector3 position = rotation * negDistance + target.position;
         
         transform.rotation = rotation;
         transform.position = position;
     }
 }
 
 public static float ClampAngle(float angle, float $$anonymous$$, float max)
 {
     if (angle < -360F)
         angle += 360F;
     if (angle > 360F)
         angle -= 360F;
     return $$anonymous$$athf.Clamp(angle, $$anonymous$$, max);
 }

}

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by yogihermawan1993 · Dec 12, 2015 at 04:04 AM

Try using this script, it is the same version as yours, but written in javascript, and has smoothed camera movement.

 #pragma strict
 #pragma implicit
 #pragma downcast
 
 @script RequireComponent(Rigidbody)
  
 var target : Transform;
 var layermask : LayerMask;
 var distance = 15.0;
 var damping : float = 6.0;
 
 var distanceMin = 2.0;
 var distanceMax = 15.0;
    
 var xSpeed = 120.0;
 var ySpeed = 120.0;
  
 var yMinLimit = -10;
 var yMaxLimit = 45;
  
 private var x = 0.0;
 private var y = 0.0;
  
 var smoothTime = 0.3;
  
 private var xSmooth = 0.0;
 private var ySmooth = 0.0;
 private var xVelocity = 0.0;
 private var yVelocity = 0.0;
  
 private var posSmooth = Vector3.zero;
 private var posVelocity = Vector3.zero;
  
 @script AddComponentMenu("Camera Control/Mouse Orbit")
  
 function Start () {
     var angles = transform.eulerAngles;
     x = angles.y;
     y = angles.x;
  
     // Make the rigid body not change the rotations.
     if (GetComponent.<Rigidbody>())
         GetComponent.<Rigidbody>().freezeRotation = true;
 }
  
 function LateUpdate () {              
  
     if (target){
         x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
         y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
        
         xSmooth = Mathf.SmoothDamp(xSmooth, x, xVelocity, smoothTime);
         ySmooth = Mathf.SmoothDamp(ySmooth, y, yVelocity, smoothTime);
  
         ySmooth = ClampAngle(ySmooth, yMinLimit, yMaxLimit);
  
         var rotation = Quaternion.Euler(ySmooth, xSmooth, 0);
         
         // Zoom feature using mouse wheel, and calculate the distance to the target.
         distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*5, distanceMin, distanceMax);
  
         // posSmooth = Vector3.SmoothDamp(posSmooth,target.position,posVelocity,smoothTime);
  
         posSmooth = target.position; // No follow smoothing.
  
         transform.rotation = rotation;
         transform.position = rotation * Vector3(0.0, 0.0, -distance) + posSmooth;
     }
    
     var hit : RaycastHit;
     if(Physics.Linecast(target.position, transform.position,hit,layermask)){
         var tempDistance = Vector3.Distance(target.position,hit.point);
         position = rotation * Vector3(0.0, 0.0, -tempDistance) + target.position;
         transform.position = position;
     }
 }
  
 static function ClampAngle (angle : float, min : float, max : float) {
     if (angle < -360)
         angle += 360;
     if (angle > 360)
         angle -= 360;
     return Mathf.Clamp (angle, min, max);
 }
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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to LookAt things when using Google Cardboard SDK? 1 Answer

Third person look around camera, jagged results 0 Answers

Make camera follow and look at object at the same time 0 Answers

Camera rotation 2 Answers

Best practice for disabling cursor functions .. Inventory / Camera movement 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