Question by
xl_Spenny_lx · May 23 at 01:52 PM ·
camera-look2 playerfirstperson
How to look left and right with a 2 player controller when up and down is working
I have attempted to create 2 person local game but can only get both players to look up and down, but not left and right. It attempts left and right but to no avail. The first script with the movement and controller are as follows. This is then fed into a second script that is replicated with the unity input system which is the 2nd one
private float mouseSensitivity = 100f;
private float xRotation = 0f;
private Transform playerBody;
private Vector2 mouseLook;
[SerializeField]
private int playerIndex = 0;
private CharacterController controller;//reference to player controller
//move
private float moveSpeed = 3f;
private Vector3 moveDirection = Vector3.zero;//Vector3(0, 0, 0)passing through 3 axis
private Vector2 inputVector = Vector2.zero;//Vector2(0, 0, 0)passing through 2 axis - input vectr for movement
private void Awake()
{
playerBody = transform.GetChild(1);
Cursor.lockState = CursorLockMode.Locked;
controller = GetComponent<CharacterController>();//component for player controller
}
public void SetInputVector(Vector2 direction)//public method to set direction where we want to move
{
inputVector = direction;//inputvector at //Vector3(0, 0, 0) = direction
}
public void SetInputVectorLook(Vector2 looking)//public method to set direction where we want to move
{
xRotation = looking;//inputvector at //Vector3(0, 0, 0) = direction
}
public int GetPlayerIndex()
{
return playerIndex;
}
// Update is called once per frame
void Update()
{
//calculation for movement
moveDirection = new Vector3(inputVector.x, 0, inputVector.y);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= moveSpeed;
controller.Move(moveDirection * Time.deltaTime);//executive movement
float mouseX = mouseLook.x * mouseSensitivity * Time.deltaTime;
float mouseY = mouseLook.y * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90);
playerBody.transform.localRotation = Quaternion.Euler(xRotation, 0, 0);
playerBody.Rotate(Vector3.up * mouseX);//execute rotation
}
{}
private PlayerInput playerInput;
private Mover mover;
private void Awake()
{
playerInput = GetComponent<PlayerInput>();
var movers = FindObjectsOfType<Mover>();
var index = playerInput.playerIndex;
mover = movers.FirstOrDefault(m => m.GetPlayerIndex() == index);//get mover object for player number
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
public void OnMove(CallbackContext context)//a method to player input,
{
mover.SetInputVector(context.ReadValue<Vector2>());//set input vector on mover i.e. //read value form mover script
}
public void OnLook(CallbackContext context)//a method to player input,
{
mover.SetInputVectorLook(context.ReadValue<Vector2>());//set input vector on mover i.e. //read value form mover script
}
Comment
Your answer
Follow this Question
Related Questions
Orbit camera with mouse control script problems 1 Answer
Third person look around camera, jagged results 0 Answers
,how to set max rotation for camera in third person game 0 Answers
Using AR camera to control a player forward direction movement. 0 Answers
(NEED HELP!) Camera controlled by mouse Y, player controlled by mouse X 2 Answers