- Home /
Question by
TEKNOPARADOX · Jun 08, 2015 at 10:15 AM ·
positiontransform.positionvertical
Move Character WSAD Separate from Axis
So I am creating a simple pong game and I'm making a two player so essentially two people can play together on one laptop. The left using "W" & "S". The right using up and down arrow keys. I have got the right player sorted out and I figured out how to separate the arrow keys from WSAD keys in InputManager. Right now I can't figure out how to make the left player move exactly like the right except with "W" & "S". Below are my code.
Left Player Code (W AND S KEYS)
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class paddle : MonoBehaviour {
public AudioClip impact;
AudioSource audio;
public float paddleSpeed = 1F;
public Vector3 playerPos = new Vector3 (0,0,0);
void Start (){
audio = GetComponent<AudioSource> ();
}
void OnCollisionEnter(){
audio.PlayOneShot (impact, 10.0F);
}
void Update ()
{
float yPos = gameObject.transform.position.y + (Input.GetAxis ("Vertical") * paddleSpeed);
playerPos = new Vector3 (-20,Mathf.Clamp(yPos, -11.0F,11.0F),0);
gameObject.transform.position = playerPos;
}
}
Right Player Code (ARROW KEYS)
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class Opposition : MonoBehaviour {
public AudioClip impact;
AudioSource audio;
public float paddleSpeed = 1F;
public Vector3 playerPos = new Vector3 (0,0,0);
void Start (){
audio = GetComponent<AudioSource> ();
}
void OnCollisionEnter() {
audio.PlayOneShot (impact, 10.0F);
}
void Update ()
{
float yPos = gameObject.transform.position.y + (Input.GetAxis ("Vertical") * paddleSpeed);
playerPos = new Vector3 (20,Mathf.Clamp(yPos, -11.0F,11.0F),0);
gameObject.transform.position = playerPos;
}
}
Comment