- Home /
Duplicate Question
How to get angle from unit to mouse click?
Hi, I can't figure out how to get an angle between unit and mouse click. I need it in C#. Angle from charactercontroller which is casting the spell to point where i clicked while ago by mouse.
Can you get it now? :P I cast a spell (C# trigger) and i want to get this angle.
Please be more specific as to what "unit" is and what sort of an angle you need.
What have you tried? Do you have any logic that you've come up with?
Questions of the form "I need this script--do it for me" usually don't get a lot of help
Answer by Joyner · Apr 08, 2014 at 05:37 PM
i guess it's the same question
http://answers.unity3d.com/questions/340102/trouble-calculating-angle-between-an-object-and-mo.html
Oh, its what i were looking for X_x, thanks idk how couldnt i find it.
Answer by djfatsteve2 · Apr 08, 2014 at 05:37 PM
Is THIS what you're lookin' for? :) I typed it up in Windows Sticky Notes so I'm absolutely unsure as to if this works. It's along the lines of what you're trying to do though.
#pragma strict
var projectile:GameObject;
var projectileSpeed:float=10;
if (Input.GetMouseButtonDown(0)){
var touchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
var shootDir = new Vector3(transform.position.x - touchPosition.x, transform.position.y - touchPosition.y , transform.position.z - touchPosition.z);
shootDir.Normalize();
strike(shootDir);
}
function strike(shootDir : Vector3){
var rotation1 = Quaternion.LookRotation(shootDir);
var projectile = Instantiate(projectilePrefab, Vector3(transform.position.x,transform.position.y,transform.position.z), rotation1);
projectile.rigidbody.velocity = projectile.transform.up*-projectileSpeed;
}
Well... Nevermind.... You want it in C# =/
Just a note,
new Vector3(v.x - u.x, v.y - u.y, v.z - u.z)
Is exactly the same as:
v - u
And
f(new Vector3(v.x, v.y, v.z))
Is exactly the same as:
f(v)
You should also type
your variables, as it not only makes your code more efficient, it also reveals bugs ;)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Flip an object by 180° (C#) 0 Answers
how to get a correct rotation 2 Answers
Rotate a particle emmiter 0 Answers