- Home /
Rigidbodies are disappearing when I click on them,How do I fix rigidbodies dissapearing when clicked?
Hello, as I am just starting to learn how to use unity, I am making a project to test unity which consists of a stickman that you can click and drag around the screen by the head. The problem is, the script I have on the head seems to cause the entire stickman to disappear when I click on it. I have the head as the parent of the torso and all limbs so that they move together, if that's important. I followed a tutorial to make this, but they didn't have the same problem in the video. Here is all of my code:
private float startPosX;
private float startPosY;
private bool isBeingHeld = false;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (isBeingHeld == true)
{
Vector3 mousePos;
mousePos = Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
this.gameObject.transform.localPosition = new Vector3(mousePos.x, mousePos.y, 0);
}
}
private void OnMouseDown()
{
if (Input.GetMouseButtonDown(0))
{
isBeingHeld = true;
}
}
private void OnMouseUp()
{
isBeingHeld = false;
}
Answer by spencerz · Mar 01, 2020 at 07:34 AM
make sure the camera's projection is set to orthographic.
also, heres something even better. its the same thing but cleaner code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragAndDrop : MonoBehaviour //make sure this ^ name matches the script name in the file explorer, young grasshopper
{
private bool isDragging;
public void OnMouseDown()
{
isDragging = true;
}
public void OnMouseUp()
{
isDragging = false;
}
void Update()
{
if (isDragging) {
Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
transform.Translate(mousePosition);
}
}
}
if you end up copying that, make sure the names match (the script and the scripts
Your answer
Follow this Question
Related Questions
,Rotate a gameobject around another while being attracted by its gravity 1 Answer
Prevent reseting of GameObject physics state on SceneManager.MoveGameObjectToScene(...) 0 Answers
Player Knockback Without Physics 0 Answers
how polygon collider 2d calculates center of mass? 2 Answers
How would I prevent ghost vertices? 2 Answers