- Home /
Change controls based on camera orientation.,How do I change my controls based on camera orientation.
I'm struggling with this one as well. I'm trying to get the player controls to rotate as the camera does. Any thoughts?
Player Controller:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed;
bool isMoving;
float distance;
Vector3 endPos;
void Start()
{
isMoving = false;
//rb = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
if (Input.GetKey("left") && isMoving == false)
{
isMoving = true;
RaycastHit hit;
Ray leftRay = new Ray(transform.position, Vector3.left);
if (Physics.Raycast(leftRay, out hit))
{
if (hit.collider != null)
{
endPos = new Vector3(hit.collider.transform.position.x + 1, endPos.y, endPos.z);
}
}
}
if (Input.GetKey("right") && isMoving == false)
{
isMoving = true;
RaycastHit hit;
Ray rightRay = new Ray(transform.position, Vector3.right);
if (Physics.Raycast(rightRay, out hit))
{
if (hit.collider != null)
{
endPos = new Vector3(hit.collider.transform.position.x - 1, endPos.y, endPos.z);
}
}
}
if (Input.GetKey("up") && isMoving == false)
{
isMoving = true;
RaycastHit hit;
Ray upRay = new Ray(transform.position, Vector3.forward);
if (Physics.Raycast(upRay, out hit))
{
if (hit.collider != null)
{
endPos = new Vector3(endPos.x, endPos.y, hit.collider.transform.position.z - 1);
}
}
}
if (Input.GetKey("down") && isMoving == false)
{
isMoving = true;
RaycastHit hit;
Ray downRay = new Ray(transform.position, -Vector3.forward);
if (Physics.Raycast(downRay, out hit))
{
if (hit.collider != null)
{
endPos = new Vector3(endPos.x, endPos.y, hit.collider.transform.position.z + 1);
}
}
}
distance = Vector3.Distance(transform.position, endPos);
//Debug.Log(distance);
if (distance > 0)
{
transform.position = Vector3.Lerp(
transform.position, endPos,
Time.deltaTime * speed / distance);
transform.rotation = Quaternion.identity;
}
if (distance == 0)
{
isMoving = false;
endPos = transform.position;
}
}
}
Camera Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PuzzleCamera : MonoBehaviour
{
public void RotateLeft()
{
transform.Rotate(Vector3.up, 90, Space.Self);
}
public void RotateRight()
{
transform.Rotate(Vector3.up, -90, Space.Self);
}
}
I'm trying to get the player controls to rotate as the camera does. Any thoughts?
Player Controller:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed;
bool isMoving;
float distance;
Vector3 endPos;
void Start()
{
isMoving = false;
//rb = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
if (Input.GetKey("left") && isMoving == false)
{
isMoving = true;
RaycastHit hit;
Ray leftRay = new Ray(transform.position, Vector3.left);
if (Physics.Raycast(leftRay, out hit))
{
if (hit.collider != null)
{
endPos = new Vector3(hit.collider.transform.position.x + 1, endPos.y, endPos.z);
}
}
}
if (Input.GetKey("right") && isMoving == false)
{
isMoving = true;
RaycastHit hit;
Ray rightRay = new Ray(transform.position, Vector3.right);
if (Physics.Raycast(rightRay, out hit))
{
if (hit.collider != null)
{
endPos = new Vector3(hit.collider.transform.position.x - 1, endPos.y, endPos.z);
}
}
}
if (Input.GetKey("up") && isMoving == false)
{
isMoving = true;
RaycastHit hit;
Ray upRay = new Ray(transform.position, Vector3.forward);
if (Physics.Raycast(upRay, out hit))
{
if (hit.collider != null)
{
endPos = new Vector3(endPos.x, endPos.y, hit.collider.transform.position.z - 1);
}
}
}
if (Input.GetKey("down") && isMoving == false)
{
isMoving = true;
RaycastHit hit;
Ray downRay = new Ray(transform.position, -Vector3.forward);
if (Physics.Raycast(downRay, out hit))
{
if (hit.collider != null)
{
endPos = new Vector3(endPos.x, endPos.y, hit.collider.transform.position.z + 1);
}
}
}
distance = Vector3.Distance(transform.position, endPos);
//Debug.Log(distance);
if (distance > 0)
{
transform.position = Vector3.Lerp(
transform.position, endPos,
Time.deltaTime * speed / distance);
transform.rotation = Quaternion.identity;
}
if (distance == 0)
{
isMoving = false;
endPos = transform.position;
}
}
}
Camera Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PuzzleCamera : MonoBehaviour
{
public void RotateLeft()
{
transform.Rotate(Vector3.up, 90, Space.Self);
}
public void RotateRight()
{
transform.Rotate(Vector3.up, -90, Space.Self);
}
}
Answer by DawidNorasDev · Jul 06, 2018 at 08:09 AM
I you don't want to use Math for it, you can brute force it. You can write on paper what LEFT should mean for every camera orientation (because as I can see you have only 4 orientations). So basically you will have :
normal -> LEFT
+90 degrees -> DOWN
+180 degrees -> RIGHT
+270 degrees -> UP
Now, you can map other directions. and use it respectively. If you would write all those down, you could see some relations of those numbers and maybe make something more generic. Unfortunately, I don't have time to think about this more generic answer.
Usually better is to break problem down to easier things to do, that you can do. And after that you can understand it more with better solutions.