- Home /
moving objects relative to the player...
I have a bit of a complex idea, and I don't know to go about scripting it. I want to have the left mouse button move an object (tagged "red") closer to the main camera, and the right mouse button (also tagged "red") to have the object move farther away, so that only "red" tagged objects can be moved. You have to hold the LMB and RMB or else the object will stop moving, sort of like the gravity gun from half life. Thank you.
Answer by bubzy · Jun 22, 2013 at 06:30 AM
this will need some tweaking to fit your needs but it moves an object if both the left and right mouse button are clicked. as it is, it does require you to still be mousing over the object (for the raycast hit) for it to move, this could be remedied quite simply by temporarily storing the object you hit and manipulating it without a raycast everytime.
there is another way of getting the right mouse button, as this method will accept any of the potential "Fire1" and "Fire2" buttons. but the other way of getting the right mouse button does require you to code it for every item (if I've read correctly)
using UnityEngine;
using System.Collections;
public class rmb : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetButton("Fire2") && Input.GetButton("Fire1"))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
Debug.DrawLine(ray.origin, hit.point);
hit.collider.gameObject.transform.position += new Vector3(0,0,1); // change this vector3 to suit your needs
}
}
}