- Home /
Make event stop
So I have basic script for a mobile VR device which just about works, in that when Mouse0/Fire1 is activated, the Camera begins to move in whichever direction it is pointing. However, I want to be able to cancel this section of the script when the button is pressed again, i.e
Go from stationary to moving when Fire1 is pressed - Then moving to stationary when pressed again.
Any tips? Script below (excuse the mess, I'm pretty new to this so most of it's been sourced and modified)
public Transform vrCamera;
private Rigidbody cc;
public float forwardSpeed = 50f;
float rotationSpeed = 300f;
Vector3 axis;
float rotationY;
float rotationX;
float rotationZ;
bool start = false;
void Start () {
cc = GetComponent<Rigidbody> ();
vrCamera = Camera.main.transform;
}
void Update () {
if (Input.GetButtonDown ("Fire1")) {
start = true;
}
if(start == true)
{
FlightMode();
}
}
void FlightMode(){ rotationX = vrCamera.transform.localRotation.x / 2;
rotationY = vrCamera.transform.localRotation.y / 2;
rotationZ = vrCamera.transform.localRotation.z;
axis = new Vector3 (rotationX, rotationY, rotationZ);
transform.Rotate (axis * Time.deltaTime * rotationSpeed);
cc.velocity = vrCamera.transform.forward * forwardSpeed;
}
}
Answer by frederik99 · Nov 04, 2016 at 03:56 PM
replace
if (Input.GetButtonDown ("Fire1")) {
start = true;
}
with
if (Input.GetButtonDown ("Fire1")) {
start = !start;
}
this will toggle the state everytime Fire1 is pressed.
Thanks for the help - this does stop it to an extent, although rather than come to a complete stop, the camera just continues moving in the direction it was going regardless of where the camera is pointing. Anything else that might be tripping it up?
Well yeah, you're setting velocity manually. This is typically a no-no for reasons like this, but you'll want to set that velocity to zero when start isn't true anymore.
Yeah that makes sense. I've not really worked with velocity before to be honest, how would you embed that into the code? Thanks for the help
Answer by ToasterKyle · Nov 04, 2016 at 07:18 PM
cc.velocity = start ? vrCamera.transform.forward * forwardSpeed : Vector3.zero;
This will reset the velocity to zero when start = false, but it looks like a few other things should also be reset when start = false so this might not be the best way
Yeah I might have to rebuild the script, that snipped takes away the existing functionality :/ Thanks for the help though
EDIT: No it didn't, that was me being an idiot, the functionality's still there but it didn't stop the camera, might be because I don't have any clause that can set start = false but I really don't know at this point
EDIT 2: $$anonymous$$anaged to get it up and running, thanks for your help :)
Answer by DLEnfield · Nov 04, 2016 at 08:18 PM
I seem to have managed to make it work by moving around snippets I've been given here and after a few shifts I got it right - still quite a messy code but I've taken out a lot of elements that weren't actually contributing and it's functional enough now. Thanks to everyone who contributed to this, honestly expected to just get berated for being inexperienced, means a lot that you'd help the way you did.