- Home /
Player transform should always be zero
The problem that I am having is that I want to calculate the angle between my raycasts origin (the player) and the hit.point. I want my player to shoot a ray which is always at 0 degrees regardless where they stand in worldspace but this isn't happening (as shown above).
It's hard to tell what you mean by "Transform should be zero", and "shoot a ray which is always at 0 degrees". Degrees relative to what? Transforms aren't values, you can think of them as triplets deter$$anonymous$$ing an object's position, orientation, and scale in the world.
Are you trying to shoot the ray in a straight line ins$$anonymous$$d of from the player's position?
What I mean is that the raycasts position (the raycast shoots out from the players transform.forward), regardless of where the player is standing in worldspace, should always be 0 degrees so that when it hits the surface of a gameobject it calculates the angles between 0 and whatever angle the gameobject has. As it is now it calculates the players place in worldspace as well which is wrong. I know it is hard to explain but do I make some sense? Is the picture helping?
It might be easier to just link your code? Unity doesn't force raycasts to shoot out from a transform's position along its forward vector, so you are telling it to do that somewhere.
Answer by Mrroundtree22 · Mar 20, 2019 at 02:03 PM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ProjectileReflection : MonoBehaviour {
public int maxReflectionCount = 5;
public float maxStepDistance = 200;
private float angle = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnDrawGizmos()
{
DrawPredictedReflectionPattern (this.transform.position + this.transform.forward * 0.75f, this.transform.forward, maxReflectionCount);
}
void DrawPredictedReflectionPattern(Vector3 position, Vector3 direction, int reflectionsRemaining)
{
if (reflectionsRemaining == 0)
return;
Vector3 startingPosition = position;
Ray ray = new Ray (position, direction);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, maxStepDistance))
{
direction = Vector3.Reflect (direction, hit.normal);
position = hit.point;
angle = Vector3.Angle (startingPosition, position);
print (angle);
}
else
{
position += direction * maxStepDistance;
}
Gizmos.color = Color.yellow;
Gizmos.DrawLine (startingPosition , position);
DrawPredictedReflectionPattern (position, direction, reflectionsRemaining - 1);
}
}
This is the reflection script I got. I want to check the angle of the origin of the ray (which I want to always be 0) + the angle of the hit.point (position).