using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class RaycastVillagerPickUp : MonoBehaviour
{
private Vector3 godHandEmptyPosition;
VillagerScript villagerScript;
public GameObject groundEmpty;
public List<GameObject> villagerObjects;
GameObject villagerObject;
private void Update()
{
if (Input.GetMouseButtonDown(1))
{
PickUpVillager();
}
if (Input.GetMouseButtonDown(0) && villagerObjects.Count > 0 )
{
DropVillager();
villagerObjects.Clear();
}
if (villagerObjects.Count > 0)
{
godHandEmptyPosition = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z);
villagerObjects[0].transform.position = godHandEmptyPosition;
}
}
public void PickUpVillager()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, Vector3.down * 1000, out hit) && hit.collider.tag == "Villager")
{
villagerObjects.Add(hit.transform.gameObject);
villagerObjects[0].GetComponent<VillagerScript>().VillagerPickedUp = true;
}
}
public void DropVillager()
{
villagerObjects[0].transform.position = groundEmpty.transform.position;
villagerObjects[0].GetComponent<VillagerScript>().VillagerPickedUp = false;
}
}
So I’ve got code here that works absolutely fine and I decided to make a new topic on this since I’m talking about adding something else now, you pick up a villager with a right mouse click and then drop it with a left click however what I’d really like to do is make it so that I pick up with the right mouse click and then drop the villager again with another right mouse click.
I’ve researched this and all I can find is multiple references to short double clicks and so on or using UI buttons, nothing that indicate how to get this working with one click and then cycle through another function with another click let alone pressing the same key.
Would someone mind pointing me to some tutorial I might have missed or explaining this to me? I thought about using else if where I check the villagerObjects count but nothing happens when I click the right mouse button and I think that’s of course because I’ve got the if statement that’s already checking the count to keep the villager following the hand.
With the code at hand, and without changing it too much, all you really need to do is changing the mouse button index, and the order you process the conditions. That is, your code needs to give precedence to “pick-up input” && “villager picked up” over a simple “pick-up input”.
You can do that in different ways. Some ways are more aesthetic than others though, but use what’s the most intuitive for you.
That was a fast bunch of replies guys @Suddoha your suggestion worked perfectly and your right I didn’t even have to change any code, this is what I’ve ended up with.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class RaycastVillagerPickUp : MonoBehaviour
{
private Vector3 godHandEmptyPosition;
VillagerScript villagerScript;
public GameObject groundEmpty;
public List<GameObject> villagerObjects;
GameObject villagerObject;
private void Update()
{
if (Input.GetMouseButtonDown(1) && villagerObjects.Count > 0)
{
DropVillager();
villagerObjects.Clear();
}
if (Input.GetMouseButtonDown(1))
{
PickUpVillager();
}
if (villagerObjects.Count > 0)
{
godHandEmptyPosition = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z);
villagerObjects[0].transform.position = godHandEmptyPosition;
}
}
public void PickUpVillager()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, Vector3.down * 1000, out hit) && hit.collider.tag == "Villager")
{
villagerObjects.Add(hit.transform.gameObject);
villagerObjects[0].GetComponent<VillagerScript>().VillagerPickedUp = true;
}
}
public void DropVillager()
{
villagerObjects[0].transform.position = groundEmpty.transform.position;
villagerObjects[0].GetComponent<VillagerScript>().VillagerPickedUp = false;
}
}
I’m a bit baffled by that, but it looks like I have to do some research into how the Update function in Unity orders everything because it’s working brilliantly now and I had a similar problem where I had to clear my list after I dropped the villager. I would have thought that checking for the villager to be picked up first makes more sense since you need to have the villager list actually have a villager in existence to be able to drop it again but I’m clearly misunderstanding how the update function works.
Oooo it’s satisfying having everything work with one button now, I’ll be able to do this with other objects as well now too.