Need help for camera script

I use this script to main camera

var rotatetarget:Transform;
var xspeed:float = 100;
var yspeed:float = 100;
var targetObj:Transform;
var distance = 10.0;
var dy = 1.0;
private var x = 0.0;
private var y = 0.0;
var yMinLimit = -5;
var yMaxLimit = 45;

function Start () {

    var angles = rotatetarget.eulerAngles;
    x = angles.y;
    y = angles.x;

    // Make the rigid body not change rotation
    if (rigidbody)
        rigidbody.freezeRotation = true;
}
function Update() {

    //Drag 
    if(Input.GetMouseButton(0)) {
        x += Input.GetAxis("Mouse X") * xspeed * 0.1;
        y -= Input.GetAxis("Mouse Y") * yspeed * 0.1;
        
        y = ClampAngle(y , yMinLimit, yMaxLimit);
        
        var rotation = Quaternion.Euler(y, x, 0);
        var position = rotation * Vector3(0.0, dy, -distance) + targetObj.position; 
        
        rotatetarget.rotation = rotation;
        rotatetarget.position = position;
        
    }
    
    rotatetarget.position = Quaternion.Euler(y, x, 0) * Vector3(0.0, dy, -distance) + targetObj.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);
}

this script is running fine .and this script can control the camera freely have Y rotation up and down and ofcourse X rotation too. and can follow to character . when press left mouse button and drag it , the camera can look all around .but problem is when camera rotating around ,character controller can’t follow to camera transform and always shows his owen transform .if camera turns to right character can’t fix his transform automaticly to camera site . what must i do ?

alt text

alt text

alt text

as you can see the character transform needs to follow camera transform . help me for that …thanks

Ok, if I get what you mean, you’ve got 2 separated scripts: a camera orbit, which you posted and responds to mouse only, and a character controller, which responds to keyboard. And both work fine independently, but one is conflicting on the other.

If it was all set right, you seem to be describing the expected behavior: you rotate the camera to front of character, and the forward key will still make him walk forward, but will look like, from the camera point of view, we’re going backwards.

And you wanted to make the camera actually move the character as well (like a 3rd person shooter, maybe?).

Maybe you could make that work simply by adding this line into your Update, inside the //Drag part:

targetObj.rotation = rotation;

But if that works, you’ll probably get the character moving badly, very stiffly and hard. Then, try lerp.

You could also make a lot of tricks, like only lerping the camera back behind the character once it begins to move.

Disclaimer

When you ask questions here, the answer’s quality hardly will be much better than the question’s quality. Bring us more to work with and you’ll get more out of us. Right now, it’s hard to comprehend what you really want or what’s your real issue.

Also, seems like you’ve got quite many misconceptions in just so little piece of code…

You didn’t need to have a rotatetarget (weird naming by the way) set to camera there, if your script is already supposed to be attached to the camera. Simply use transform and it will get the game object’s transform in which the script is attached.

The camera doesn’t have a rigidbody and you’re still setting it up somehow on the Start. That’s pretty confusing as well.

I suggest you review your whole procedure and take a look at how someone else is doing, like the 3rd person shooter guys.