- Home /
How I can define the throw direction to a static GameObject?
Hello there! I'm making a game where the avatar can create clones who throw enemies around. During clone creation, the player can click anywhere on the screen to define the direction of the throw.
This is how I'm storing the click position:
void DefineCloneThrowDirection(){
Vector3 myMousePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition); //Use camera rect instead of screen dimensions.
myMousePosition.Set (myMousePosition.x, myMousePosition.y, 0); //"Transform" position from 3D to 2D.
Debug.DrawLine (myPos, myMousePosition,Color.yellow);
//Create clone, set Throw Direction and leave Summoning Mode.
if (Input.GetKeyDown (KeyCode.Mouse0)) {
GameObject newClone = (GameObject) Instantiate (cloneList [0], myPos,Quaternion.Euler(0,0,0));
newClone.layer = 10;
newClone.GetComponent<CloneThrow> ().SetThrowDirection (myMousePosition);
isSummoning = false;
Debug.DrawLine (myPos, myMousePosition.normalized,Color.green,10f);
}
}
This is the yellow DrawLine between avatar's myPos and mouse's myMousePosition:
But the problem is: I want just the direction, so I could define a fixed strengh to the throw. So I've tried to normalize the click vector. But this is what happens:
Why it isn't keeping the same direction?
Answer by ScaniX · Sep 21, 2016 at 12:20 PM
You need to normalize the delta vector, not the mouseposition.
Vector3 delta = (myMousePosition - myPos).normalized;
Debug.DrawLine (myPos, myPos + delta, Color.green, 10f);
Your answer
![](https://koobas.hobune.stream/wayback/20220612093048im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Normalizing vector doesn't give a constant direction. 2 Answers
What is a way to use a value and return it back to zero? 1 Answer
Obtain direction from a point relative to another point 3 Answers
C# Raycast from Object direction (z-axis) + another Vector3 1 Answer
Direction between two point - add unit distance to player 0 Answers