Hi. There is a part in my script where I need to make a ray from my mussel to enemy. This enemy could be anywhere around player. I found my ray is not moving to target position !. Code (CSharp): public Transform target,Bullet_position; void Update { if (target != null) { RaycastHit hit; if (Physics.Raycast(Bullet_position.position, target.position, out hit, 500)) { Debug.DrawRay(Bullet_position.position, target.position, Color.blue, 500); if (hit.collider.tag == "enemy") { print(" In Range and i can see you! "); } else { print(" None!! "); } } } else { } }
I think you have to calibrate your raycast: -set the position -when you move/rotate your player make sure that the position of raycast follow the player Code (CSharp): public Vector3 frontSensorsPosition0 = new Vector3(0f, 0.5f, 0.1f); // we can change it at runtime public Transform target; Vector3 startPosition = transform.position; startPosition += transform.forward * frontPosition.z; // for same direction startPosition += transform.up * frontPosition.y; // keep the height if (Physics.Raycast(startPosition, transform.forward, out hit, 500)) if (hit.collider.CompareTag("player")) Debug.log("I can see you!"); Add the script to bullet.
The Raycast function uses the second vector as a direction from the origin, ie. if it's Vector3.down, it will tell you if there's ground below you. Replace target.position with target.position - Bullet_position.position in your raycast function.