- Home /
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.
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.
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.
$$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.
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.
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.
Your answer
Follow this Question
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