How to change direction of RayCast based on camera angle?
I've posted this once before, but it was poorly worded. In the player controller, we use a RayCast to find an object in that direction. When it finds one, the player moves to that object. The problem is we want to change the direction of RayCast based on the camera angle (which only rotates around the y-axis in increments of 90 degrees), but I'm not sure how. I think is has something to do with this line: Ray leftRay = new Ray(transform.position, Vector3.left);
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);
}
}
Your answer
Follow this Question
Related Questions
How to have a child object camera with gaze move its parent object 0 Answers
How do I make the movement relative to the camera? 1 Answer
Third person movement with fixed cameras 1 Answer
How can we control two player objects at one time with RayCasting? 1 Answer
How to rotate camera diagonally over players shoulder while still facing towards players direction 0 Answers