- Home /
c# Raycast going off at odd angles. Unity 5
I can't get a hit on my game object when I am standing in the red debug laser, but if I move to the left, far from the debug draw line, I get a hit. This is vexing me :( I got angry and named EVERYTHING "PlayerCube," but that did not fix anything.
using UnityEngine;
using System.Collections;
public class Sight : MonoBehaviour {
public Transform ObjectEye;
Vector3 playerPosition;
Vector3 subjectPosition;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 fwd = transform.TransformDirection(Vector3.forward);
playerPosition = transform.position;
subjectPosition = ObjectEye.position;
RaycastHit hit;
if (Physics.Raycast (transform.position, subjectPosition, out hit) && hit.transform.name == "PlayerCube") {
Debug.LogError ("poke");
}
Debug.DrawLine(transform.position, subjectPosition, Color.red);
}
}
what is the rotation of the emitter object? is it a strange value?
Answer by $$anonymous$$ · May 07, 2015 at 05:02 PM
Okey, so I finaly tested your code and it's working fine, your Raycast is correct. From the beginning it wasn't working but then I saw the rest of your Ccondition:
if (Physics.Raycast(...) && hit.transform.name == "PlayerCube")
So I've changed the name of my target Game Object to "PlayerCube" and sudenly it worked.
glad to hear you got it working please select the correct and answer so people know which way is correct , happy coding
still not working, I stand right between the two objects, and no poke. The object that is raycating is turned 180, the offset seems to become greater the more it is rotated, and I need it going in a straight line.
Okey, I'm not sure if you're trying to do this effect:
But if yes, this is what I've changed in Update();
private void Update()
{
Vector3 Direction = ObjectEye.position - transform.position;
RaycastHit hit;
if (Physics.Raycast(transform.position, Direction, out hit) && hit.transform.name == "PlayerCube")
{
Debug.LogError("poke");
}
Debug.DrawRay(transform.position, Direction, Color.red);
}
Answer by dudester · May 07, 2015 at 04:21 PM
this Vector3 fwd = transform.TransformDirection(Vector3.forward);
should be Vector3 fwd = transform.TransformDirection(transform.forward);
and then use if (Physics.Raycast (transform.position, fwd , out hit,1000f) && hit.collider.transform.name == "PlayerCube") { //something done here } Debug.DrawRay(StartPos, fwd, Color);
Haha dudester you're on the same questions as me! On a roll... lol
hahaha yeah im waiting on occlusion baking so nothing much else to do haha.
I would help you, but being the world's biggest noob has its pitfalls
no help needed ay just time , probably gonna take about 2 days or so to bake occlusion culling
Wow, your scene must be huge! Or your computer is a dell...
Your answer
Follow this Question
Related Questions
C# Raycast Code not working 2 Answers
Physics 2D Raycast is not working 3 Answers
Change Transparency 1 Answer
Need help with OnUse() script! 1 Answer
Want to move object slowly to where the mouse clicks? 1 Answer