How to project an angle from a vector

Answered many thanks for all your help everyone with special thanks to Louis with his single line of code answer :slight_smile:

I am trying to find a Vector3 point along an angle projected from a Vector. This is the closest I have got and it realy misses the mark. I want to say ‘X’ degrees from forward facing, in plane ‘y’ project the ‘range’ and obtain the ‘point’ Vector3(x,y,z).

TO TRY AND HELP CLARIFY MY QUESTION
Sparkzbarca quote - ‘send a ray along an angle and get the point that is RANGE distance away from the origin of the ray?’. I would like the origin angle to be related to the original transforms forward facing. I would like to do it without altering existing transformations.

Please see alucardj picture for absolute clarity (many thanks alucardj ) I would like to find the vector at the green X.


public var scanSpeed : float = 10.0f;
public var range : float = 5.0f;
public var speed : float = 10.0f;
public var rotationSpeed : float = 10.0f;

private var shadowSelf : Transform;

function Start ()
{
	shadowSelf = transform;
}

function Update () 
{
	var rotAngle : Vector3;
	
	//Show forward direction
	Debug.DrawLine(shadowSelf.position, shadowSelf.forward * (range + 1), Color.green);
	
	for (var x : int = 0; x < 90; x = x + 1)
	{
		// Sweep scan beam left n right 1-90 degrees
		rotAngle = shadowSelf.TransformDirection(Quaternion.AngleAxis(x, Vector3.up).eulerAngles);
		Debug.DrawLine(shadowSelf.position, rotAngle * range, Color.red);
		rotAngle = shadowSelf.TransformDirection(Quaternion.AngleAxis(-x, Vector3.up).eulerAngles);
		Debug.DrawLine(shadowSelf.position, rotAngle * range, Color.magenta);
		DebugPrintOut(rotAngle, "rotAngle");
	}	
}

function DebugPrintOut(vec3 : Vector3, prnStr : String)
{
	Debug.Log(prnStr + " = Vector3(x-" + vec3.x + ",y-" + vec3.y + ",z-" + vec3.z + ")");
}

To rotate a vector:

Quaternion * Vector3 = rotated vector

So.

position = (Quaternion.Euler( angleX, angleY, angleZ ) * transform.forward ).normalized * range;

Ray myray;

myray.origin = gameobject.transform.position;

next we need to take the forward direction and add the additional angle.

A directional vector is really the slope of a line. The slope of an angle is defined as its rise over its run, or its hypotenuse over its base. The hypotenuse over base is a common trigometric identity called its tangent.

The tangent of an angle is the angle slopes.
The slope of an angle added to forward should give us a vector which is angle degrees
off center from forward.

myray.direction = gameobject.transform.forward + mathf.tan(angle);

ray.GetPoint(distance);

that gets you a point at set distance at a set angle.

This is the answer guys and you dont have to create any objects to get there


#pragma strict

public var range : float = 5.0f;

function Update () 
{
	var targetPos : Vector3;	// REQUIRED answer position
	
	for (var x : int = 0; x < 90; x = x + 1) // Angle from forward
	{
		// the fantastic single line of code provided by Loius
		targetPos = (Quaternion.Euler( 0, x, 0 ) * transform.forward ).normalized * range;
		// visual output
		Debug.DrawLine(transform.position, targetPos, Color.green);
		// Print to console window
		DebugPrintOut(targetPos,"targetPos");
		
	}		
}

function DebugPrintOut(vec3 : Vector3, prnStr : String)
{
	Debug.Log(prnStr + " = Vector3(x:" + vec3.x + ",y:" + vec3.y + ",z:" + vec3.z + ")");
}