Question by
fialle · Aug 27, 2015 at 02:29 AM ·
mousemovement scriptmove an objectteleportfreezeposition
Teleporting 2D object once
Hi have a script that moves (change the position / teleport) an object to the mouse position every time that I click but I wanted to move it only once and freeze it right there. I tried but nothing seems to work.
using UnityEngine;
using System.Collections;
public class move : MonoBehaviour {
public float speed = 1.5f;
private Vector3 target;
void Start () {
target = transform.position;
}
void Update () {
if (Input.GetMouseButtonDown(0)) {
target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
target.z = transform.position.z;
}
transform.position = target;
}
}
Comment
Answer by shieldgenerator7 · Dec 31, 2015 at 06:28 PM
If you set the object's RigidBody.isKinematic to true, then it won't be affected by the physics engine. You can also set the gravityScale to 0, which means it won't be affected by gravity