Touch Inputs
Hi, So I have a vehicle which as a constant force applied to it. It is a 3d game, and I wish to make a system like this:
When the user touches the button, I want the car to steer (i.e rotate around a point to rotate the car 90 degrees).
For example, the red points would be transform positions, and when the user presses the button, the taxi rotates around that point, depending on how long they hold the button down for. So if the user held the button down, it would rotate the taxi around that point 10 degrees, and if they held it for two seconds, it would rotate by 20 degrees.
Hope this makes sense, and would love to hear any ideas about how this could work. Thanks, @Mavina
Answer by Mmmpies · Jun 11, 2016 at 03:04 PM
Add an EventTrigger component to the button. Add New Event Type and select Pointer Down.
Create a script that has a public function called TurnRightBy.
Have a float PressedFor and a bool isPressed declared at the top of the script. Then in TurnRightBy set the bool to true In Update add Time.deltaTime to that float if the bool is true.
Add another New Event Type (Pointer Up) and another public function called TurnMeRight. Take the float PressedFor and round off to get your time held and apply your right rotation, set the bool to false and reset PressedFor to zero.
drag the script onto the button and in the button inspector drag the button onto the empty slots. The for Pointer Down select YourScriptName -> TurnRightBy and for PointerUp select YourScriptName -> TurnMeRight.
Do the same for the Left button but with a left turn.
Could your write a script? I am new to unity @$$anonymous$$mmpies
This is the script I threw together to make sure I wasn't giving you duff information
using UnityEngine;
using System.Collections;
public class RightTurn : $$anonymous$$onoBehaviour {
private float PressedFor = 0f;
private bool isPressed = false;
void Update()
{
if(isPressed)
PressedFor += Time.deltaTime;
}
public void TurnRightBy()
{
isPressed = true;
}
public void Turn$$anonymous$$eRight()
{
print ("Turn right by " + PressedFor);
isPressed = false;
PressedFor = 0f;
}
}
All it does is output to the console how long the button was pressed for so it doesn't actually do the rotation but it's a good idea to try it yourself, look up
http://docs.unity3d.com/ScriptReference/Transform.Rotate.html
and to round off the float value
https://docs.unity3d.com/ScriptReference/$$anonymous$$athf.Round.html
I'm sure you can do it :)
I will let you know - thanks! @$$anonymous$$mmpies Would I need to add an empty game object as a child of the taxi as its rotation point? What is a 'Pointer Up'.. arrgghh what do I do :/ So confused
Now that depends on what you want to do with the taxi. You shouldn't need to if just turning it left or right but if you start getting into complex things like in air $$anonymous$$ario $$anonymous$$art spinning then you might.
$$anonymous$$eep it simple for now just work on Rotating the Transform of your taxi.
Incidentally you will need to times that float by 10 before rounding off if you want 10 degrees in a second. You might want more than that as well as that's still 9 seconds for a full right angle turn of 90 degrees.
Try and get the script to work on your own, the more you work on it yourself the faster you'll learn.
Take that PressedFor float and times it by 10 and round off the resulting number with $$anonymous$$ath.Round.
Then tranfrom rotate using Vector3.Right * the rounded number.
The example in those links are almost all you need. You can do this.
Do I need 2 scripts? @$$anonymous$$mmpies
You can do it with one but having an individual script for right and left isn't an issue with such a small script so yes do 2 scripts.
Your answer
![](https://koobas.hobune.stream/wayback/20220612082724im_/https://answers.unity.com/themes/thub/images/avi.jpg)