• 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.

avatar image ExcitedMormon · Feb 21, 2015 at 02:42 AM 0
Share

Okay but I thought all the code was relevant, since it's the thing that causes the movement of the camera. Do you know what I can do to stop the rotation without stopping the camera from focusing on the players for orthographic lordlycastle?

Edit: I've removed all of the non-relevant code. I just have to say, if someone can help me convert this to C#, then I'm sure I'll be able to figure out how to stop the rotation on my own.

avatar image Wolfdog · Feb 21, 2015 at 10:59 AM 0
Share

transform.LookAt is a function for rotating an object (towards something). Remove that.

avatar image ExcitedMormon · Feb 21, 2015 at 11:03 AM 0
Share

Thank you for trying to help but without transform.LookAt it doesn't keep focus on the Players in the game. It just zooms out when the players get farther from each other, and zooms in when they get closer. But it doesn't focus on the players, so when it zooms in the players get cut off if say they were on the left or right side of the level.

When I have transform.LookAt, it keeps focus on the Players and doesn't cut them off. It moves according to the distance of the players and always keeps them on screen(I love this code because of it).

avatar image siaran · Feb 21, 2015 at 01:51 PM 0
Share

Hmm. I'm not going to write real code for this since I'm not sure it's what you want but what I think could work would be something like

-calculate the center point of all gameobjects you want in your camera view, lets call this v1.

-calculate the largest distance between the gameobjects you are following, let's call this d1.

-and also the z position of the closest object, let's call this c1

(I'm assu$$anonymous$$g that your camera looks and zooms in the world z direction)

Now, you want your camera set so that

-Its x and y positions are (0,0) relative to v1.

-Its z coordinate is so that c1 is in front of it

-its orthographic size is slightly larger than d1.

avatar image siaran · Feb 21, 2015 at 03:26 PM 0
Share

EDIT: Since I'm a bit stuck on my own game atm I'm going to try to put my comment as a script (C#, because I hate javascript) anyways even though I said I wouldnt.

 public Camera thisCamera;
 public GameObject[] players;
 
 
  void CameraStuff(){
 
  Vector3 playerCenter;
  float biggestDistance = 0;
  float closestZ = 0;
 
  for(int i = 0; i < players.Length; i++){
   playerCenter += players[i].transform.position;
   foreach(GameObject go in players){
    float distance = Vector3.Distance(go.transform.position, players[i].transform.position);
   if(distance > biggestDistance) biggestDistance = distance;
   }
   if(players[i].transform.position.z < closestZ) closestZ = players[i].transform.position.z;
 
  }
  playerCenter /= players.Length;
 
  transform.position = new Vector3(playerCenter.x, playerCenter.y, closestZ-1);
  thisCamera.ortographicSize = biggestDistance;

  }

  void Update(){
   CameraStuff();
  }


Cannot guarantee thta this compiles, just typed it here, there might be typos.

what it is supposed to do is find the position that is in the center of your players (just using center of mass here), place the camera at that position and back so that the closest player is in front of it, then set it's orthographic size large enough that all players are visible. I'm assu$$anonymous$$g your camera's field of view is in the world z direction, if not it shouldn't be hard to alter this little script.

Also note that this is a really inefficient way of getting the largest distance (its O(n^2)) so if your number of players is large you may want to find some smarter algorithm for doing that

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

How to freeze the rotation or the position on the camera transform? 1 Answer

Need help on the 3ds max style camera control 0 Answers

How to move camera up and down 1 Answer

Changing The Camera Position & Offsetting LookAt 2 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