Reputation:
Here's my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GoToMouse : MonoBehaviour
{
private Transform tf;
private bool Selected = false;
// Start is called before the first frame update
void Start()
{
tf = GetComponent<Transform>();
}
private void OnMouseDown()
{
if (Selected == false)
{
Selected = true;
}
if (Selected == true)
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
tf.position = mousePos;
}
}
private void OnMouseUp()
{
if (Selected == true)
{
Selected = false;
}
if (Selected == false)
{
}
}
// Update is called once per frame
void Update()
{
}
}
In this script, I want to do two things. I want an object to become selected when clicked and unselected when you let go of the mouse. When an object is selected I want it to move towards the mouse cursor. Basically, you can drag it around with the mouse cursor and throw it with physics.
This script has a couple problems.
Whenever I click the object it completely vanishes. I have no background or anything it could be going behind, so I don't know what is causing this. The object also doesn't move anywhere (I checked its transform) So it appears it's sprite just stops rendering
Whenever I select it and try to move it, it moves less that 1 unit along the X and Y axis and then stops. For some reason, it deselects itself or stops moving before I let go of the mouse. I don't know why this would be since the only way to deselect an object is by letting go of the mouse.
This is a unity2D project BTW, and this script is the backbone of the game I'm making. Please help!
thanks.
Upvotes: 0
Views: 54
Reputation:
I ended up figuring it out, I'm not sure what I was doing wrong but this new script seems to work great. Here's the script for people who want to plagiarize.
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
public class MoveTowards : MonoBehaviour
{
private Rigidbody2D rb;
public float force = 1f;
private bool selected = false;
public void Awake()
{
//Get rigidbody from the gameobject this script is attatched to
rb = GetComponent<Rigidbody2D>();
}
public void OnMouseDown()
{
//become selected when clicked
selected = true;
}
public void OnMouseUp()
{
//become deselected when you let go of left click
selected = false;
}
void Update()
{
if (selected == true)
{
//move towards mouse if selected
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 dir = (mousePos - transform.position);
dir.z = 0.0f;
Vector3 dirNormalized = dir.normalized;
Vector2 relativePos = mousePos - gameObject.transform.position;
rb.AddForce(relativePos * force);
}
}
}
Upvotes: 0
Reputation: 87
I understand tf as a character which is needed to move.
According to the OnMouseDown function,
OnMouseDown is called when the user has pressed the mouse button while over the Collider.
You will require a background and script may attach to it. Get the mouse position from background. Than set the position to the tf(character). 2D game z position always should be 0. And of course, you can change the size of the background as large as you need.
Image and script can explain better.
Here is GoToMouse.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GoToMouse : MonoBehaviour
{
public Transform tf;
private bool Selected = false;
// Start is called before the first frame update
void Start()
{
// the tf is drag and drop from unity
//tf = GetComponent<Transform>();
}
private void OnMouseDown()
{
if (Selected == false)
{
Selected = true;
}
if (Selected == true)
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
tf.position = new Vector3(mousePos.x,mousePos.y,tf.position.z); // notice the tf.position.z
}
}
private void OnMouseUp()
{
if (Selected == true)
{
Selected = false;
}
}
}
Upvotes: 0