- Home /
Question by
FiredJungle · Jul 01, 2018 at 07:26 PM ·
movementclick
How do I move my object a certain distance along the x axis when clicked on?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Blue_car_moving : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnMouseDown() {
transform.Translate(transform.position.x - 2, transform.position.y, transform.position.z);
}
}
Comment
Nothing seems wrong. Please describe your problem. Just showing us code does not really help
my only guess is that this code reacts to every click, not just a click on the object.
in $$anonymous$$ouseDown, do a Raycast with ScreenPointToRay and compare the result hit with your object. if it's the same, do your work.
Answer by MeteorLublin · Jul 04, 2018 at 05:52 AM
Hi @FiredJungle try something like this:
void OnMouseDown() {
transform.Translate(Vector3.right);
}
By default distance is 1. You can multiply that vector to get bigger values for example Vector3.right * 15f
Thank you for your answer! I must be missing something simple here... I placed this bit of code in my object and when clicked on it still will not move.