Question by
zavisnik · Feb 17, 2017 at 01:32 AM ·
c#tower-defense
Clicking on a 2d image and placing 3d object?
What would be the best way to click on a 2d icon (turret from shop image), then hold 3d object of that turret in your mouse until its placed down?
Should I do it with On Click? Like I did with shop?
This is the turret I wanna "hold":
Another question, should I make script inside of a StandardTurret, and add it like this: http://prnt.sc/e9q9ok
Or should I make a standalone script?
And what script for following the mouse whould be the best in my case?
private SpriteRenderer spriteRenderer;
// Use this for initialization
void Start () {
this.spriteRenderer = GetComponent<SpriteRenderer>();
}
// Update is called once per frame
void Update ()
{
FollowMouse();
}
private void FollowMouse()
{
transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = new Vector3(transform.position.x, transform.position.y, 0);
}
public void Activate(Sprite sprite)
{
this.spriteRenderer.sprite = sprite;
}
public void Deactivate()
{
spriteRenderer.enabled = false;
}
Or something like this:
RaycastHit hit;
Ray ray;
void Update()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit, Mathf.Infinity)
{
transform.position = hit.point;
}
}
Thank you very much!
screenshot-1.png
(92.3 kB)
screenshot-2.png
(145.6 kB)
Comment
Your answer
