If button is not pressed fall.
Hello! What I want to do is when my character floats to the right and collides with an invisible mesh, I want the player to fall down if the mouse button isn't pressed but if it is pressed while it's colliding it should continue to hover to the right!
I have done the hovering to the right script and I can't do the thing with the falling. Help plox?
C# if you can!
Could you post the code you have currently and the code you've tried?
When OnTriggerEnter is called, is the Hover component already enabled/attached? If it isn't, you're not enabling it when the mouse isn't clicked.
EDIT: Or rather you're disabling it? I see there's an exclamation point in your Input call which means if it's false. So you're disabling the Hover script?
The one for falling. If I press the button it still falls. It must be a simple solution for that but I'm just blocked now Xd
OnTriggerEnter only occurs for one frame, so I have a feeling when it's called, the function is happening too quickly for the Input to register. You'd be better off looking at how triggers work and maybe making a system for the start of OnTriggerStay.
http://docs.unity3d.com/$$anonymous$$anual/CollidersOverview.html http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnTriggerEnter.html http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnTriggerStay.html
Either way, you're still disabling the Hover component when no input is received. Change that to true.
Answer by Bob-The-Zealot · Aug 31, 2015 at 11:21 AM
Try changing your chick check to:
if (!Input.GetButtonDown ("Fire1"))
I'm not sure if that's the problem, but try it.
Answer by Naminu · Aug 31, 2015 at 11:04 AM
I managed to fix it! I'm gonna leave the scripts here in case someone else has my problem.
Falling Code.
using UnityEngine;
using System.Collections;
public class Fall : MonoBehaviour {
public Collider coll;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerStay(Collider other) {
if (!Input.GetButton("Fire1"))
{
other.attachedRigidbody.useGravity = true;
other.GetComponent<Hover>().enabled = false;
}
else
{
other.attachedRigidbody.useGravity = false;
other.GetComponent<Hover>().enabled = true;
}
}
}
Hover Code
public class Hover : MonoBehaviour {
public float horizontalSpeed;
public float verticalSpeed;
public float amptitude;
public Vector3 tempPosition;
void Start ()
{
tempPosition = transform.position;
}
void Update ()
{
tempPosition.x += horizontalSpeed;
tempPosition.y = Mathf.Sin (Time.realtimeSinceStartup * verticalSpeed) * amptitude;
transform.position = tempPosition;
}
}
Your answer
Follow this Question
Related Questions
Detect if falling with custom gravity 1 Answer
Gravity is not working 0 Answers
Using rb.velocity causes low gravity. 2 Answers
3D Game RigidBody Problem 0 Answers