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 /
This question was closed Mar 23, 2013 at 06:50 AM by Fattie for the following reason:

IP Infringement

avatar image
0
Question by Duckaz · Mar 23, 2013 at 03:58 AM · rotationjavascript

Object is the wrong way around

I have looked for ages to find a solution, but cant seem to figure out why the object that has the applied script is back to front. Here is the sample code :

 #pragma strict
  
 public var TargetLookAt : Transform;
  
 public var Distance : float = 5.0;
 public var DistanceMin : float = 3.0;
 public var DistanceMax : float = 10.0;  
 private var mouseX : float = 0.0;
 private var mouseY : float = 0.0;
 private var startingDistance : float = 0.0; 
 private var desiredDistance : float = 0.0;  
 public var X_MouseSensitivity : float = 5.0;
 public var Y_MouseSensitivity : float = 5.0;
 public var MouseWheelSensitivity : float = 5.0;
 public var Y_MinLimit : float = -40.0;
 public var Y_MaxLimit : float = 80.0;   
 public var DistanceSmooth : float = 0.05;   
 private var velocityDistance : float = 0.0; 
 private var desiredPosition : Vector3 = Vector3.zero;   
 public var X_Smooth : float = 0.05;
 public var Y_Smooth : float = 0.1;
 private var velX : float = 0.0;
 private var velY : float = 0.0;
 private var velZ : float = 0.0;
 private var position : Vector3 = Vector3.forward;  
  
  
 function Start() 
 {
     Distance = Mathf.Clamp(Distance, DistanceMin, DistanceMax);
     startingDistance = Distance;
     Reset();
 }
  
 function LateUpdate()
 {
     if (TargetLookAt == null)
        return;
  
     HandlePlayerInput();
  
     CalculateDesiredPosition();
  
     UpdatePosition();
 }
  
 function HandlePlayerInput()
 {
     var deadZone = 0.01; // mousewheel deadZone
  
     if (Input.GetMouseButton(0))
     {
        mouseX += Input.GetAxis("Mouse X") * X_MouseSensitivity;
        mouseY -= Input.GetAxis("Mouse Y") * Y_MouseSensitivity;
     }
  
     // this is where the mouseY is limited - Helper script
     mouseY = ClampAngle(mouseY, Y_MinLimit, Y_MaxLimit);
  
     // get Mouse Wheel Input
     if (Input.GetAxis("Mouse ScrollWheel") < -deadZone || Input.GetAxis("Mouse ScrollWheel") > deadZone)
     {
        desiredDistance = Mathf.Clamp(Distance - (Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity), 
                                                  DistanceMin, DistanceMax);
     }
 }
  
 function CalculateDesiredPosition()
 {
     // Evaluate distance
     Distance = Mathf.SmoothDamp(Distance, desiredDistance, velocityDistance, DistanceSmooth);
  
     // Calculate desired position -> Note : mouse inputs reversed to align to WorldSpace Axis
     desiredPosition = CalculatePosition(mouseY, mouseX, Distance);
 }
  
 function CalculatePosition(rotationX : float, rotationY : float, distance : float)
 {
     var direction : Vector3 = Vector3(0, 0, -distance);
     var rotation : Quaternion = Quaternion.Euler(rotationX, rotationY, 0);
     return TargetLookAt.position + (rotation * direction);
 }
  
 function UpdatePosition()
 {
     var posX = Mathf.SmoothDamp(position.x, desiredPosition.x, velX, X_Smooth);
     var posY = Mathf.SmoothDamp(position.y, desiredPosition.y, velY, Y_Smooth);
     var posZ = Mathf.SmoothDamp(position.z, desiredPosition.z, velZ, X_Smooth);
     position = Vector3(posX, posY, posZ);
  
     transform.position = position;
  
     transform.LookAt(TargetLookAt);
 }
  
 function Reset() 
 {
     mouseX = 0;
     mouseY = 10;
     Distance = startingDistance;
     desiredDistance = Distance;
 }
  
 function ClampAngle(angle : float, min : float, max : float)
 {
     while (angle < -360 || angle > 360)
     {
        if (angle < -360)
          angle += 360;
        if (angle > 360)
          angle -= 360;
     }
  
     return Mathf.Clamp(angle, min, max);
 }
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 AlucardJay · Mar 23, 2013 at 06:37 AM 1
Share

If you are going to use one of my scripts, but not ask a question where you found it, you could at least give me credit or provide the link to where you found it for future readers :

  • http://answers.unity3d.com/questions/246709/rotate-around-the-center-of-the-world-with-mouse.html

robertblu is correct : check the orientation of your objects

avatar image AlucardJay · Mar 23, 2013 at 07:01 AM 0
Share

Thanks Fattie, I appreciate the thought, but really my answer is an IP Infringement of 3D Buzz. This is more the point I was trying to make. From that answer :

  • EDIT 2 : I want to clarify this is from an excellent tutorial by 3D Buzz in C#, I have simply converted it to unityJavaScript. (and moved a function to make one script)

Am still a bit heated from this previous question (check the last comment where my temper is showing, but still not fully unleashed) : http://answers.unity3d.com/questions/422522/slender-type-game-need-help.html

I wish I had Doc's Delorean to go back 12 months and stop myself from writing that bloody guide !

avatar image AlucardJay · Mar 23, 2013 at 07:13 AM 0
Share

@Fattie : I have noticed a problem since the start of the year (apart from all the scripts I have put out there! The one on this question was actually my own) : http://answers.unity3d.com/questions/422377/my-problem-is-this.html#comment-422412

I keep telling myself "I am going to stop, or only give API links", but then in the next spare moment I find myself co$$anonymous$$g back here ....

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Mar 23, 2013 at 06:31 AM

I tested the script above, and it worked just fine for me. The problem is most likely your model. In unity forward is the side of the model that is facing positive 'z' with (0,0,0) rotation. If the issue is your model, you might be able to fix it by using an empty game object as the parent (with this script attached), and then having the rotated model as a child. Or you can fix it in the modeling program.

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

Follow this Question

Answers Answers and Comments

11 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

Related Questions

Slerp Problem?: Enemy in Constant Rotation! 1 Answer

using a custom rotation in "Instantiate" 4 Answers

Smooth rotation in 90° increments 0 Answers

How To Check If X Rotation Is Between Two Numbers?? 1 Answer

space flying system 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