- Home /
Issues with 2d movement
Hi Two object, call them "red" and "blue". The aim is if I click "blue", the "red" moves exactly same position. Right now this move happens instantly, but would like to set the speed to see "red" moving. This is the script attached to "blue". Any help is appreciated.
using UnityEngine; using System.Collections;
public class ClickCheck : MonoBehaviour {
public float speed = 1.5f;
private Vector3 target;
void OnMouseUp()
{
target = Camera.main.ScreenToWorldPoint(transform.position);
target.z = transform.position.z;
GameObject.Find("red").transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
}
}
Hmm, the code you posted shouldn't work very well, because you are not calling $$anonymous$$oveTowards in a loop, so your red should only take one step towards blue (unless the distance is very small, or the speed very high). Do you also have some related code in Update? If so, post it, please.
Answer by prikolist · Jul 23, 2014 at 04:23 PM
Your object "Red" is forced to move only once, when you release the mouse button. Do that the OnMouseUp would set a boolean to true, and move in update function.
Answer by kristja · Jul 24, 2014 at 02:09 PM
Well... cant get this to work. Shortest code (attached to "blue") to move "red" instantly is here... whats the best way to modify this, so I can see the "red" actually moving?
public var check : boolean;
check = false;
var red = GameObject.Find("red");
function OnMouseOver () {if (Input.GetMouseButtonUp(0)) { check = true; }
if(check) {
red.transform.position = transform.position;
check = false;
}
}
Your answer
Follow this Question
Related Questions
Bullet not moving from script 3 Answers
rotation speed cap in 2D 0 Answers
moving object toward waypoint and alignment 0 Answers
Movement speed difference between the different devices 1 Answer