- Home /
some behavior on over-shoulder view and static var in update()
Hi, my game use third-person view.And in normal state camera will always follow player back.Now I have some problem in makeing the aim state. the situation is
When player press RMB,camera will move to over-shoulder view.(during this process,player can't move)
After camera reach the desired position, fix the camera view to player(which means camera always follow player in fixed position, now is lower left-corner on screen).
Then recover the player movement ability and mouse look.
Once release RMB, camera convert to normal view.
My problem is I use lerp to smooth camera when it convert to over-shoulder view.It seems to update every frame.I am not sure that how to realize that : If camera reached the over-shoulder view position,stop update the smooth function.I also has problem in getting variable from other script.Now I create one function to check the current distance and wanted distance of camera.If it didn't reached desired distance, use first follow function.Else use second follow function.I also create a ` static var canmove which can be accessed by playercontrolscript.The playerconrolscipt get the variable and change walk speed . But the variable in playercontrolscript seems not update every frame following my camera script variable.
I tried a lot method but still not work well.....Can someone give me any idea? Thanks..
edit (copied from comment and fixed indent)
var target : Transform;
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -20;
var yMaxLimit = 80;
var initDis = 10;
var minDis = 2.0;
var maxDis = 10.0;
var currentdis ;
var wheelSpeed = 5;
static var x = 0.0;
static var y = 0.0;
static var distance =5.0;
var fixdis;
private var position;
private var rotation;
var changeonce = 0;
static var IsMoving =1;
function Start ()
{
x = 30;
y = 0;
if (rigidbody)
rigidbody.freezeRotation = true;
}
private var smoothTime = 0.5;
private var velocity = 0.0;
private var onetime = 0;
function Update ()
{
if (target)
{
checkdis();
currentdis = Vector3.Distance(target.position,transform.position);
if(Input.GetMouseButton(1))
{
print("fixdis"+fixdis);
if ((currentdis-fixdis)>0.07)
{
IsMoving = 1; //check if camera position approach the over-shoulder view
Overshoulder();
print("currentdis"+currentdis);
}
else if((currentdis-fixdis)<=0.07)
{
IsMoving = 0;
print("Moving false");
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
rotation = Quaternion.Euler(y, x-10, 0);
position = rotation * Vector3(0.3, 1.8, -1.2)+ target.position;
transform.rotation = rotation;
transform.position= position;
target.rotation = Quaternion.Euler(0,x,0);
}
}
else
{
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
rotation = Quaternion.Euler(y, x, 0);
position = rotation * Vector3(0.5, 1.5, -2)+ target.position;
transform.rotation = rotation;
transform.position= position;
target.rotation = Quaternion.Euler(0,x,0);
}
}
}
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);
}
function checkdis()
{
if(changeonce == 0)
{
rotation = Quaternion.Euler(y, x-15, 0);
position = rotation * (Vector3(0.3, 1.8, -1.2) ) + target.position;
fixdis = Vector3.Distance(target.position,position);
changeonce = 1;
}
}
//smooth the camera moving to overshoulder view
function Overshoulder()
{
rotation = Quaternion.Euler(y, x-15, 0);
position = rotation * (Vector3(0.3, 1.8, -1.2) ) + target.position;
var newY = Mathf.SmoothDamp(0, position.x - transform.position.x, velocity, smoothTime);
var factor = newY / (position.x - transform.position.x);
transform.position += Vector3(Mathf.Lerp(0, position.x - transform.position.x, factor),
Mathf.Lerp(0, position.y - transform.position.y, factor),
Mathf.Lerp(0, position.z - transform.position.z, factor));
}
Would be useful to see some of the code you've written until now. Also why do you use to different scripts for you camera movement?
Actually I just start learning script ,I don't know well how to organize them. here is my code.Sorry for the orderless script..
var target : Transform;
var xSpeed = 250.0; var ySpeed = 120.0;
var y$$anonymous$$inLimit = -20; var y$$anonymous$$axLimit = 80;
var initDis = 10; var $$anonymous$$Dis = 2.0; var maxDis = 10.0;
var currentdis ; var wheelSpeed = 5;
static var x = 0.0; static var y = 0.0;
static var distance =5.0; var fixdis; private var position; private var rotation; var changeonce = 0;
static var Is$$anonymous$$oving =1;
function Start () {
x = 30; y = 0;
if (rigidbody) rigidbody.freezeRotation = true;
}
private var smoothTime = 0.5; private var velocity = 0.0;
private var onetime = 0;
function Update () {
if (target)
{
checkdis();
currentdis = Vector3.Distance(target.position,transform.position);
if(Input.Get$$anonymous$$ouseButton(1)) {
print("fixdis"+fixdis);
if ((currentdis-fixdis)>0.07)
{
Is$$anonymous$$oving = 1; //check if camera position approach the over-shoulder view
Overshoulder();
print("currentdis"+currentdis);
}
else if((currentdis-fixdis)<=0.07)
{
Is$$anonymous$$oving = 0;
print("$$anonymous$$oving false");
x += Input.GetAxis("$$anonymous$$ouse X") * xSpeed * 0.02;
y -= Input.GetAxis("$$anonymous$$ouse Y") * ySpeed * 0.02;
y = ClampAngle(y, y$$anonymous$$inLimit, y$$anonymous$$axLimit);
rotation = Quaternion.Euler(y, x-10, 0);
position = rotation * Vector3(0.3, 1.8, -1.2)+ target.position;
transform.rotation = rotation;
transform.position= position;
target.rotation = Quaternion.Euler(0,x,0);
}
} else { x += Input.GetAxis("$$anonymous$$ouse X") xSpeed 0.02; y -= Input.GetAxis("$$anonymous$$ouse Y") ySpeed 0.02;
y = ClampAngle(y, y$$anonymous$$inLimit, y$$anonymous$$axLimit);
rotation = Quaternion.Euler(y, x, 0);
position = rotation * Vector3(0.5, 1.5, -2)+ target.position;
transform.rotation = rotation; transform.position= position;
target.rotation = Quaternion.Euler(0,x,0); }
}
}
static function ClampAngle (angle : float, $$anonymous$$ : float, max : float) { if (angle < -360) angle += 360; if (angle > 360) angle -= 360; return $$anonymous$$athf.Clamp (angle, $$anonymous$$, max); }
function checkdis() { if(changeonce == 0) { rotation = Quaternion.Euler(y, x-15, 0); position = rotation * (Vector3(0.3, 1.8, -1.2) ) + target.position;
fixdis = Vector3.Distance(target.position,position);
changeonce = 1;
}
}
//smooth the camera moving to overshoulder view function Overshoulder() {
rotation = Quaternion.Euler(y, x-15, 0);
position = rotation * (Vector3(0.3, 1.8, -1.2) ) + target.position;
var newY = $$anonymous$$athf.SmoothDamp(0, position.x - transform.position.x, velocity, smoothTime);
var factor = newY / (position.x - transform.position.x);
transform.position += Vector3($$anonymous$$athf.Lerp(0, position.x - transform.position.x, factor),
$$anonymous$$athf.Lerp(0, position.y - transform.position.y, factor),
$$anonymous$$athf.Lerp(0, position.z - transform.position.z, factor));
}