GameObject not changing position in PlayMode although the Transform changes
I have a problem and searched a whole day for a solution but can't seem to find one.
My problem is that I am trying to move a GameObject with the code provided at the bottom (But I am almost 100% sure that the code is NOT the problem).
But when I try to test it and hit Play in the Editor and then try to move a GameObject per mousedrag it does not move the GameObject.
Then I looked into the Editor and saw that only the BoxCollider has moved and the GameObject still is in the same place as before which is confusing to me because the Transform of the GameObject also moved to place where I dragged it...
I also have an other GameObject with a MeshCollider and when I try to drag and drop it around with the mouse in PlayMode it works...
Scripts:
using UnityEngine;
using System.Collections;
public class LevelEditorTouchInputControl : MonoBehaviour {
public LevelEditorManager levelEditorManager;
// Use this for initialization
void Start () {
}
// TODO test at home with android and convert to touchControl instead of MouseButtons
void Update () {
if (Input.GetMouseButtonDown(0))
levelEditorManager.snapObject();
if (Input.GetMouseButton (0))
levelEditorManager.dragObject ();
if (Input.GetMouseButtonUp (0))
levelEditorManager.releaseObject ();
}
}
using UnityEngine;
using System.Collections;
public class LevelEditorManager : MonoBehaviour {
private float dist;
private Transform toDrag;
private bool dragging = false;
private Vector3 offset;
public void snapObject() {
RaycastHit hit;
Vector3 v3;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray,out hit))
{
if (hit.collider.gameObject.transform.parent == null)
toDrag = hit.collider.gameObject.transform;
else
toDrag = hit.collider.gameObject.transform.parent;
Debug.Log ("ToDrag: " + toDrag.name);
dist = hit.transform.position.z - Camera.main.transform.position.z;
v3 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, dist);
v3 = Camera.main.ScreenToWorldPoint(v3);
offset = toDrag.position - v3;
dragging = true;
}
}
public void dragObject() {
Vector3 v3;
if (dragging) {
v3 = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, dist);
v3 = Camera.main.ScreenToWorldPoint (v3);
toDrag.position = v3 + offset;
}
}
public void releaseObject() {
dragging = false;
}
}
The working one:
Before I drag it:
http://imgur.com/me9a91h
After I dragged it:
http://imgur.com/C8ktVLV
The not working one:
Before I drag it:
http://imgur.com/GTM8nCF
After I dragged it:
http://imgur.com/gSVFgb4
The editor view:
http://imgur.com/HQqEarS
Here you clearly see that I am somehow just dragging the BoxCollider which is weird because the Transform of the parent GameObject changes which then should move every child like the working one does above and not just the BoxCollider. Any ideas which lead to a possible solution are very much appreciated.
Answer by ScaniX · Sep 24, 2016 at 09:25 AM
The second one is marked static, so you cannot move it.
Your answer
Follow this Question
Related Questions
Need help getting randomly moving particles to head to the nearest of 4 coordinates. 1 Answer
How to make enemies go through colliders? 0 Answers
Infinite Time inbetween if statements? 2 Answers
Make object move back and forth 2 Answers
Vector3.Lerp not working properly, making the player bounce around 2 Answers