- Home /
I solved
button triggered using new input system
i am using unity new starter pack with new input system and i was creating game for android device first person joystick controller So here is my scene
here is my action map
i am created a script so i pick up the object those having pickable script in it. here is my whole code using UnityEngine; using UnityEngine.UI; using System.Collections;
public class PickupObject : MonoBehaviour
{
GameObject mainCamera;
bool carrying;
GameObject carriedObject;
public float distance;
public float smooth;
[SerializeField]
private PickObj pick;
void Awake()
{
pick = new PickObj();
}
void Start()
{
mainCamera = GameObject.FindWithTag("MainCamera");
}
public void Enable()
{
pick.Enable();
}
public void Disable()
{
pick.Disable();
}
// Update is called once per frame
void Update()
{
if (carrying)
{
carry(carriedObject);
checkDrop();
//rotateObject();
}
else
{
pickup();
}
}
void rotateObject()
{
carriedObject.transform.Rotate(5, 10, 15);
}
void carry(GameObject o)
{
o.transform.position = Vector3.Lerp(o.transform.position, mainCamera.transform.position + mainCamera.transform.forward * distance, Time.deltaTime * smooth);
o.transform.rotation = Quaternion.identity;
}
public void pickup()
{
if (pick.picking.p.triggered)
{
int x = Screen.width / 2;
int y = Screen.height / 2;
Ray ray = mainCamera.GetComponent<Camera>().ScreenPointToRay(new Vector3(x, y));
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Pickupable p = hit.collider.GetComponent<Pickupable>();
if (p != null)
{
carrying = true;
carriedObject = p.gameObject;
//p.gameObject.rigidbody.isKinematic = true;
p.gameObject.GetComponent<Rigidbody>().useGravity = false;
}
}
}
else if (Input.GetKeyDown(KeyCode.P))
{
int x = Screen.width / 2;
int y = Screen.height / 2;
Ray ray = mainCamera.GetComponent<Camera>().ScreenPointToRay(new Vector3(x, y));
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Pickupable p = hit.collider.GetComponent<Pickupable>();
if (p != null)
{
carrying = true;
carriedObject = p.gameObject;
//p.gameObject.rigidbody.isKinematic = true;
p.gameObject.GetComponent<Rigidbody>().useGravity = false;
}
}
}
}
void checkDrop()
{
if (Input.GetKeyDown(KeyCode.P))
{
dropObject();
}
else if (pick.picking.p.triggered)
{
dropObject();
}
}
void dropObject()
{
carrying = false;
//carriedObject.gameObject.rigidbody.isKinematic = false;
carriedObject.gameObject.GetComponent<Rigidbody>().useGravity = true;
carriedObject = null;
}
}
for testing i add old input to get object and it working fine as i expected but new input system blind for picking up is not working. i add a button and given a pick up name that button is not working
anyone can help me ???
thank
Follow this Question
Related Questions
How do I detect touch on another object and check by tag/name? 1 Answer
How can I stop getting mouse position of Ui Elements? 0 Answers
How can I make GUI button send an input message when I press it? 0 Answers
Input.getTouch() vs checking if button is clicked 1 Answer
Clicking a button will display that buttons text in a seperate text-field 1 Answer