- Home /
bouncing a puck of a vertical wall through code
Hello everybody! I searched through all the questions related to this, but I seem not to get it correctly. I am really lost now so every help is very much appreciated :) I want to do a airhockey game. There are two players (playing with Kinect through networking) that toss a puck with their hands over a field. I got a nice puck movement when using AddForce but when they play remotely it gets all jittery, so I decided not to rely on the physics and bouncing materials but code the movement instead. For the bouncing of the hands it works fine, but when the puck should bounce of the framing walls each side of the field, nothing works. I wanted to reflect the angle, but the puck is flying off to nirvana although initially no y-movement is applied (puck should only move in x and z). This is the code I have so far (attached to the puck):
private var camScript2 : GameObject;
private var speed = Vector3(0,0,3);
function Start () {
Physics.gravity = Vector3(0, -50, 0);
//rigidbody.AddForce(0,0,-5, ForceMode.Impulse); //direction patient
//rigidbody.AddForce(0,0,5, ForceMode.Impulse); //direction healthy
camScript2 = GameObject.Find("Camera");
}
function Update () {
transform.Translate(speed * Time.deltaTime);
}
function OnCollisionEnter(collision : Collision) {
if(Network.isServer){
var normalpoint : Vector3 = collision.contacts[0].normal;
var hitpoint : Vector3 = collision.contacts[0].point;
for (var contact : ContactPoint in collision.contacts) {
if (contact.otherCollider.name == "GraspFeedbackSphere"){
camScript2.GetComponent(PointsGUI).playerCol = "A";
networkView.RPC("updatePuck1", RPCMode.AllBuffered, normalpoint);
}
if (contact.otherCollider.name == "GrasphSphere"){
camScript2.GetComponent(PointsGUI).playerCol = "B";
networkView.RPC("updatePuck2", RPCMode.AllBuffered, normalpoint);
}
if (contact.otherCollider.name == "Bound_L"){
var angle = Vector3.Reflect(transform.position, normalpoint);
speed = angle;
}
if (contact.otherCollider.name == "Bound_R"){
var angle2 = Vector3.Reflect(transform.position, normalpoint);
speed = angle2;
}
}
}
}
@RPC
function updatePuck1 (impact: Vector3){
//move right
if (impact.x > 0.2){
//rigidbody.AddForce(3,0,5, ForceMode.Impulse);
speed = Vector3 (3,0,5);
}
//move to left
if (impact.x < -0.2){
//rigidbody.AddForce(-3,0,5, ForceMode.Impulse);
speed = Vector3 (-3,0,5);
}
//go straight
else {
//rigidbody.AddForce(0,0,5, ForceMode.Impulse);
speed = Vector3 (0,0,5);
}
}
@RPC
function updatePuck2 (impact: Vector3){
//move right
if (impact.x < -0.2){
//rigidbody.AddForce(-3,0,-5, ForceMode.Impulse);
speed = Vector3 (-3,0,-5);
}
//move to left
if (impact.x > 0.2){
//rigidbody.AddForce(3,0,-5, ForceMode.Impulse);
speed = Vector3 (3,0,-5);
}
//streight
else {
//rigidbody.AddForce(0,0,-5, ForceMode.Impulse);
speed = Vector3 (0,0,-5);
}
}
The movement of the puck will be sent through RPC as well later, when I got it right :)
Thanks a lot for every hint or help!!!
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Rocket Jumping Woes 1 Answer
Smoothing Network Movement with OnSerializeNetworkView 0 Answers
Networked player movement very choppy, rotation works fine - UNET 1 Answer