c# - Right paddle not moving in pong game for unity -
i trying allow player, in pong game, control both paddles. however, reason 1 paddle controllable while other nothing. here image of paddle property bar.
and here code right paddle should controlled arrow keys.
using unityengine;
using system.collections;
public class paddleright : monobehaviour {
public vector3 playerposr; public float paddlespeed = 1f; public float yclamp; void start() { } // update alled once per frame void update() { float ypos = gameobject.transform.position.y + (input.getaxis("vertical") * paddlespeed); if (input.getkey(keycode.downarrow) || input.getkey(keycode.uparrow)) { playerposr = new vector3(gameobject.transform.position.x, mathf.clamp(ypos, -yclamp, yclamp), 0); print("right padddle trying move"); } gameobject.transform.position = playerposr; }
}
i can't seem figure out anywhere why won't move.. please, awesome because have checked everywhere @ point. thanks!
i recreated problem in project , found problem might forgetting yclamp
public , set 0 in inspector. make sure set yclamp
whatever should instead of 0.
i suggest moving ypos assignment setting position inside if statement arent changing if player isnt moving.
you can change gameobject.transform.position
plain transform.position
here refined code:
public vector3 playerposr; public float paddlespeed = 1f; public float yclamp; // make sure it's not 0 in inspector! // update alled once per frame void update() { if (input.getkey(keycode.downarrow) || input.getkey(keycode.uparrow)) { float ypos = transform.position.y + (input.getaxis("vertical") * paddlespeed); playerposr = new vector3(transform.position.x, mathf.clamp(ypos, -yclamp, yclamp), 0); transform.position = playerposr; } }
Comments
Post a Comment