find vector3

45788-how-to-find.jpg

anyone can help me?

how to find vector3 value about this pict?

Isn't that just the direction multiplied by the range? Do you know how to get the direction vector?

–

+1 Just for having a great image in the question ^^. Even though questions like this has been asked several times before, we're ultimately here to collect good questions and good answers.

–

3 Answers

3

float range = 3f;
Vector3 Tp = target.transform.position;
Vector3 Pp = player.transform.position;

			// vector from target to player
			Vector3 targetToPlayer = Pp - Tp;
			// vector with same direction as "targetToPlayer", but length of "range"
			Vector3 targetToPlayerWithRangeLength = targetToPlayer.normalized * range;

			// start from target and come towards player the "range amount".
			Vector3 positionOfSpot = Tp + targetToPlayerWithRangeLength;

First, you need to get the vector3 of the orientation towards the target.
After that, you will need to get only the part up to the circle around the target.

To get the orientation, simply substract them from one another :

Vector3 orientation = (target.position - player.position);

Afterwards, substract the value of the range from it, like so :

//Get the orientation of previously found vector
Vector3 GreenVector = orientation.normalized;

//Get the lenght of the previous vector minus the range
GreenVector = GreenVector * (orientation.magnitude - range); 

That should get you the green vector on the picture.

EDIT: Now, if you want the position of the point, simply add the player’s position to GreenVector.

Vector3 DesiredPosition = PlayerPosition + GreenVector;

Well if you swap your points you don't need that orientation.magnitude thing ^^. See NoseKills answer It's always good to have the [head-minus-tail][1] in mind. [1]: http://www.maths.usyd.edu.au/u/MOW/vectors/vectors-3/v-3-7.html

–

btw: your "myAnswer" vector just contains a relative vector with the lenth of the distance between the player and the target minus the range. To get the actual point you have to add the player's position as well.

–

You're right about that last part, the questions isn't really precise about what it asks. I'll modify it so it gives the position as well.

–

if(hit.collider.gameObject.tag == “Tower”){
Transform tower = hit.collider.gameObject.transform;

					Vector3 Tp = tower.position;
					Vector3 Pp = this.gameObject.transform.position;
					
					Vector3 targetToPlayer = Pp - Tp;
					Vector3 targetToPlayerWithRangeLength = targetToPlayer.normalized * rangeShoot;
					
					Vector3 positionOfSpot = Tp + targetToPlayerWithRangeLength;
					target.position = positionOfSpot;

					Instantiate(marker, positionOfSpot, Quaternion.identity);
				}}

hi All, thanks for responses, working perfect :slight_smile:

terimakasih (bahasa)