- Home /
Client Shoot GameObject
I'm trying to make what is basically 3D pong. I have the players network.instantiate(ing) correctly and everything. And the Gameball is network.instantiate(d) when the server is initialized.
Everything works fine on the server side. If the player is touching the ball and pressing the space bar it allows them to shoot the ball across the field.
The problem comes when the client tries to do the same thing. It goes up to the ball and knows that it is touching the GameBall, but when they hit the SpaceBar to shoot it, the ball doesn't move with any of the force that should be applied through the script, it just kind of rolls forward a bit. Or it blinks back and forth between the position from where it was shot, and the where it would be if the correct force were applied. But it always just ends back up right in front of the client.
This is the code attached to the GameBall:
void Start(){
if(!networkView.isMine){
enabled = false;
}
}
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info){
if(stream.isWriting){
Vector3 pos = transform.position;
stream.Serialize(ref pos);
}
else{
Vector3 posRec = Vector3.zero;
stream.Serialize(ref posRec);
transform.position = posRec;
}
}
And this is the script that is supposed to shoot the ball... (Works for server, not for client):
//======================================================//
//================ VARIABLES =======================//
//======================================================//
public Transform aimBallPoint;
public int force = 75000;
public bool touchingBall = false;
public GameObject ball;
//======================================================//
//================ SET AIM POINT ===================//
//======================================================//
void Start(){
if(!aimBallPoint){
aimBallPoint = GameObject.FindWithTag("aimPoint").transform;
}
}
//======================================================//
//================ SHOOT BALL ======================//
//======================================================//
void FixedUpdate() {
if(!ball){
ball = GameObject.FindWithTag("gameBall");
}
if(touchingBall == true && Input.GetKeyDown(KeyCode.Space)){
//ball = GameObject.FindWithTag("gameBall");
ball.transform.LookAt(aimBallPoint);
ball.rigidbody.AddForce(ball.transform.forward * force);
touchingBall = false;
}
}
//======================================================//
//================ TOUCHING GAME BALL? ==============//
//======================================================//
void OnCollisionEnter(Collision collision){
if(collision.transform.tag == "gameBall"){
touchingBall = true;
}
}