- Home /
Go from mouse to multitouch?
Hey I am making a game were you are controlling 2 turrets. I have asigned a script to both of the turrets that makes them follow the mouse like this:
This is the script:
#pragma strict
function Start () {
}
var mouse_pos : Vector3;
var target : Transform; //Assign to the object you want to rotate
var object_pos : Vector3;
var angle : float=180;
function Update ()
{
mouse_pos = Input.mousePosition;
mouse_pos.z = 5.23; //The distance between the camera and object
object_pos = Camera.main.WorldToScreenPoint(target.position);
mouse_pos.x = mouse_pos.x - object_pos.x;
mouse_pos.y = mouse_pos.y - object_pos.y;
angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(Vector3(0, 0, angle));
};
What i want to do is making them follow your touch seperatly like this:
Imagine the screen is split in half were the left side controlls the movement of the left turret and the right side controls the right turret.
They only move when you touch the screen, when you move your fingers away they should stay in the same position as they where left. I would also like to implement an angle intervall were the turrets can move between, so that the pipe dont touch their "stand", maybe between 20 and 160 degrees.
I realize that these things are not easy tasks, but can you try to give me some input on how and where to start? Any thought on how to do all this?
Software development is all about breaking down problems to simpler steps. Think about how your problem can be broken down. - So you have them both rotating to the 1st touch position. - Split the screen in half and take the touch position from the left side and apply it to "left turret" only. same thing with the right. - then add when nothing is being touched keep the angle the last saved angle - add a range that the angle can be so it stays within the bounds.
Implementing step by step helps you solve problems quicker and if something breaks you know which step it was.
Being somewhat of a newbie in the area of coding makes me appretiate these kind of comments, thank you!
Answer by Paulius-Liekis · Dec 24, 2014 at 12:42 PM
Look into Input.touches. Find active touches, check which are on left side of the screen and which on the right. Use one for one turret, other for the other. Just as you use mouse input.
Thanks for the tip! I am looking into input.touches as we speak.