I’ve got a Raycast set up from my character that creates a sound when it hits a collider in front of it:
function Update (){
var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.forward, hit, 5)){
if (hit.collider.gameObject.tag == "josh01" characterHit == false) {
audio.PlayOneShot (josh01Sound);
What I want to do, is for the Raycast to do the same, but when the collider is at a 45 degree angle to the left of the character.
I’ve been looking at everything I can find about Raycasts and Vector3, but I’ve not been able to get my head round Vector 3.
I’m sure I’d be able to grasp it once I’ve seen an example I could understand.
Is there anyone who could please help by telling me how I’d change the above script (which currently collides with objects to the front) so that it includes Vector3 coordinates to collide with objects at a 45 degree angle to the left.
If so, I, and anyone else as confused as I am, would be extremely grateful.
According to the documentation, the 2nd paramter gets
a Vector3. static function Raycast (origin : Vector3, direction : Vector3, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool
You can tweak it simply, instead of puting -transform.right u can put something like Vector3(0,-transform.right / 2 ,0)
or just -transform.right / 2
I’m not sure it’ll work because I never tried it, refer to it as a Psuedo code,
/ 2 won’t get you any different result to what you have now. The direction is still the same its magnitude is just different.
if you want 45 deg angles, use right / -right and add forward / -forward to it. that way it will be 45 degrees. Towards which direction depends on the combination of ± forward ± right, you can cover all 4 possibilities
I can’t recall my trig very well off the top of my head, but I hope we can agree that a vector whose absolute X and absolute Y is the same is also 45 degrees or 135 or 225 or 315 (45 degrees off an axis).
It is also called Magnitude I believe. It just means all negative values are converted to positive.
i.e. 3,3 or -4,4 or -23, -23 are all vectors whose absolute X and absolute Y are the same.
And as far as it being 45 degrees you have to remember the starting point
A<---
^
|
|
B
goes from B to A and so the resultant Vector from forward - right is:
A
\
\
B
And if you perform the trig for:
^
X |
\ |
\
You will find that angle to be 45 (the arrow being forward).