the two scripts below do the similar things,
get the key press and calculation the direction
after the direction calculated, they multiple with the same speed, say 0.05f,
while the character and the camera, do not move at the same speed,
and the character move faster than the camera.
if i rotate the camera a bit and move the character, the character move even faster than camera.
i believe there are something wrong in the variable direction, i would like to like how can i fix it so that both of them can move at the same speed in every direction.
Thank you.
character movement script:
animation.CrossFade("moving");
direction = Vector3.zero;
myposition= transform.position;
if(Input.GetKey("i")) {
animation.CrossFade("moving");
direction += Vector3.forward;
currentkeydown=true;
}
if(Input.GetKey("k")){
animation.CrossFade("moving");
direction += Vector3.back;
currentkeydown=true;
}
if(Input.GetKey("j")){
animation.CrossFade("moving");
direction += Vector3.left;
currentkeydown=true;
}
if(Input.GetKey("l")){
animation.CrossFade("moving");
direction += Vector3.right;
currentkeydown=true;
}
//if two key press
if(Mathf.Abs(direction.x)+Mathf.Abs(direction.y)+Mathf.Abs(direction.z)>1){
direction.x/=2;
direction.y/=2;
direction.z/=2;
}
//if direction change
if (direction != Vector3.zero) {
direction = Quaternion.Euler(0.0f, Camera.main.transform.localEulerAngles.y, 0.0f) * direction;
transform.rotation = Quaternion.LookRotation(direction);
}
if(currentkeydown){
transform.position+=direction*speed;
}
and camera script
if(Input.GetKey("i")) {
direction += Vector3.forward;
currentkeydown=true;
}
if(Input.GetKey("k")){
direction += Vector3.back;
currentkeydown=true;
}
if(Input.GetKey("j")){
direction += Vector3.left;
currentkeydown=true;
}
if(Input.GetKey("l")){
direction += Vector3.right;
currentkeydown=true;
}
if(Input.GetKey("u")){
transform.RotateAround(player.transform.position,Vector3.up,-2f);
}
if(Input.GetKey("o")){
transform.RotateAround(player.transform.position,Vector3.up,2f);
}
direction = transform.TransformDirection(direction);
direction.y = 0;
if(currentkeydown){
if(Mathf.Abs(direction.x)+Mathf.Abs(direction.y)+Mathf.Abs(direction.z)>1){
direction.x/=2;
direction.y/=2;
direction.z/=2;
}
transform.position+=direction*speed;
}