- Home /
Mouse Click
Hello there, I have my targeting basic system:
if (Input.GetMouseButtonDown(0)){
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input. mousePosition), hit, 100)){
target = hit.collider.transform;
isTargetIsAttackable = target.GetComponent(NPC);
}
But there is one big problem on this. I rotate my camera with mouse click (click and drag). Whenever I click to rotate a camera, it sets a new target, a floor for example. Can I somehow lock my target, that when player will drag and rotate camera, target will stay the same?
Here is a hack. Record the camera rotation when the mouse goes down. Do the targeting when the mouse goes up, but only if the delta rotation of the camera is below some threshold. Or ins$$anonymous$$d of checking the rotation, you could check the time between the mouse down and the mouse up and only target if the time is below some threshold.
Answer by marshall50 · Feb 05, 2014 at 08:08 PM
So I did it as robertbu said.
Record the camera rotation when the mouse goes down. Do the targeting when the mouse goes up, but only if the delta rotation of the camera is below some threshold.
Thank you.
Remember to account for the scenario where the user drags the camera away, but then drags it back to the initial position (within delta range) -- this will trigger selection unless you record that the camera has already been dragged.
Your answer

Follow this Question
Related Questions
Unity Android Movement Problem 0 Answers
Strange Camera Behavior on iOS - Runs perfect in Unity 0 Answers
How to enable a childs camera 5 Answers
camera target keep angle and y position 1 Answer
Camera for the player (Move with mouse to move camera) 1 Answer