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 ExcitedMormon · Feb 20, 2015 at 12:30 PM · camerarotationjavascriptpositionlookat

How Can I Stop Rotation From This Script?

Hello fellow Unity users, I need to know how can I stop this script from rotating, and to only move the position. It uses transform.LookAt. I tried modifying it already, and I was able to make it better than it was originally, but it still rotates. Please help or point me in the right direction in order to stop the rotation. I found this script online and modified it, but it's in Java Script and it's not my native language for coding(C# is). Anyways here's the script so you know:

 #pragma strict
 
 private var isOrthographic : boolean; 
 var targets : GameObject[]; 
 var currentDistance : float; 
 var largestDistance : float; 
 var theCamera : Camera; 
 var height : float = 5.0; 
 var avgDistance; 
 var distance = 0.0; 
 var speed = 1; 
 var offset : float;
 
 function Start () {
 
  targets = GameObject.FindGameObjectsWithTag("Player"); 
  
  if(theCamera) isOrthographic = theCamera.orthographic;
  
 
 }
 
 
 function LateUpdate ()
 
 {
 
  targets = GameObject.FindGameObjectsWithTag("Player"); 
  
  if (!GameObject.FindWithTag("Player"))
  
  return;
  
  var sum = Vector3(0,0,0);
  
  for (var n = 0; n < targets.length ; n++){
  
      sum += targets[n].transform.position;
  
  }
  
    var avgDistance = sum / targets.length;
  
 // Debug.Log(avgDistance);
 
    var largestDifference = returnLargestDifference();
  
    height = Mathf.Lerp(height,largestDifference,Time.deltaTime * speed);
  
      if(isOrthographic){
  
         //theCamera.transform.position.x = avgDistance.x ;//
  
          theCamera.orthographicSize = largestDifference;
  
         // theCamera.transform.position.y = height;//
  
          theCamera.transform.LookAt(avgDistance);
  
      } else {
  
          theCamera.transform.position.x = avgDistance.x ;
          
          ///theCamera.orthographicSize = largestDifference;
          
          //theCamera.transform.position.z = avgDistance.z;
  
          theCamera.transform.position.z = avgDistance.z - distance + largestDifference;
  
          //theCamera.transform.position.y = height;
  
          theCamera.transform.LookAt(avgDistance);
  
      }
  
 }
 
 function returnLargestDifference(){
 
  currentDistance = 0.0;
  
  largestDistance = 0.0;
  
  for(var i = 0; i < targets.length; i++){
  
      for(var j = 0; j <  targets.length; j++){
  
          currentDistance = Vector3.Distance(targets[i].transform.position,targets[j].transform.position);
  
          if(currentDistance > largestDistance){
  
              largestDistance = currentDistance;
  
          }
  
      }
  
  }
  
  return largestDistance;
  
 }

I tried taking out some code with //, and also tried the quick Update fix by changing the Vector 3 rotation to 0, 0, 0, but that didn't work at all. Not sure what to do next for now.

Comment
Add comment · Show 10
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 Nymisu · Feb 20, 2015 at 03:52 PM 0
Share

Define "rotates"? You're being awfully vague. What is it doing that it shouldn't? Should it move to a position ins$$anonymous$$d of looking at it? If that's the case, use $$anonymous$$oveToward ins$$anonymous$$d of LookAt.

avatar image siaran · Feb 20, 2015 at 04:16 PM 0
Share

If you don't want it to rotate, why not simple remove the transform.LookAt() line?

Also you have a variable called avgDistance that appears to be a Vector3, which is kind of strange.

Now, I suspect you want a camera that is always at a relative position to an object? I think what you then want to do is something like

 var target : Transform;
 
 var relativePosition : Vector3
 
 void Update(){
  transform.postion = target.TransformPoint(relativePosition);
 }

But like the commenter above me said, you are being a bit vague about what you want.

avatar image ExcitedMormon · Feb 20, 2015 at 10:40 PM 0
Share

$$anonymous$$y apologies everyone. What I mean by rotate is that it rotates the Camera's x, y, and z position in orthographic mode. I have two Game objects that are called Player. When I move one player away from the other to the left, it rotates the Camera's y position to 300 or so and goes down by how fast I'm moving the Player(since the camera follows both players and keeps them in range of view). When I move right it also rotates the y position except the opposite way.

avatar image ExcitedMormon · Feb 20, 2015 at 11:10 PM 0
Share

Two things here. Currently the camera does almost exactly what I want $$anonymous$$us the rotation of it's axis and zoo$$anonymous$$g out too much when both players get too far from each other. The code above that siaran posted changes the Z axis position of the camera too much, since it's following the target's axis. The sprites that I'm using disappear because of this(the camera get's too close basically).

I need to be able to follow multiple GameObjects and still be in range of everyone. So far I've tested two Gameobjects with the array code and the find all game objects called Player. Again it works well but the rotation of the camera's axis is what keeps it from being perfect.

I've already tried to get rid of this line of code: theCamera.transform.LookAt(avgDistance); But when I do that it doesn't follow the players when they move to the left and right. It's not focused on them. All it does is move it's Z axis position.

avatar image lordlycastle · Feb 20, 2015 at 11:40 PM 0
Share

Only post the code which you think is relevant, not everything. We are here to help, not waste our time. If we need more, we'll ask.

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Look at wont update? 2 Answers

Rotating Main Camera 1 Answer

RTS Camera movement wrong after rotation 1 Answer

Make camera 1 transform equal to camera 2 transform 1 Answer

Camera rotation and position problem,Camera rotation and camera position 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