- Home /
Raycast length or camera to between mousePosition and character
Hello guys, i have simple question. in my game project i will use camera script that positioning between cursor and character. can i use length/2 of my raycast or what can i use? can you answer me in simply? thanks a lot guys... (the game is 2d perspective and processing in z=0 so character, cursor and bullets , all moving on just x and y axes.)
(EDIT1: in my platform game, my cahracter will cast a Fireball spell to direction of a cursor,mouse position. also my camera will focus to show between character and cursor. I mean smiliar to Soldat patform shooter game's camera style.)
Hi
your answer is little-bit confusing. I can't understand what you want to position and for what purpose. I asume you use ray-cast for finds traget where you click and move bullet in which direction.
$$anonymous$$ouse lives in 2D space (screen), but your game objects live in 3D space. This is no distance between the mouse position and an object position. You can calculate the distance between the camera position and the mouse.
Answer by Scribe · Jun 01, 2013 at 03:56 PM
If I understand the question then you can do this quite simply using:
var MousePos : Vector3;
var CharObject : Transform;
function Update () {
MousePos = Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, -transform.position.z));
transform.position.x = ((MousePos.x - CharObject.position.x)/2.0)+CharObject.position.x;
transform.position.y = ((MousePos.y - CharObject.position.y)/2.0)+CharObject.position.y;
}
What I understood was that you want the camera to be centred between your character and your mouse position. This will work if you set 'CharObject' to your character model, though I have not included any clamping of the mouse position so this will work best in fullscreen at the moment, though clamping would be easy to add.
Hope that helps,
Scribe
Thanks a lot, this is what I want. I learn that i can chose axis for distance select ($$anonymous$$ousePos*.y*) also thanks for that. I will attach what its looks like. but first i will edit a little bit.
Scribe's Code in Game. Thanks a Lot Scribe. http://www.youtube.com/watch?v=dlaZqEu1WEo
Your answer