Hey, so I’m still pretty new to this whole thing, and I’m not that great at translating things sometimes. I see a few people who have posted solutions for this, but I seem to be missing something. All the solutions I’ve found are in JS, and I’m using C#, so I’m wondering if someone could help me figure it out.
All I want to do is click on an object and move it in the x direction, based on the mouse. I can get it to move, but barely.
Here’s the code I have so far. What do I need to add to get it to actually move along with the mouse? Please try to explain it. I don’t want someone to just give me an answer, but I want to learn how it works.
using UnityEngine;
using System.Collections;
public class bottomPaddle : MonoBehaviour {
public float posX;
public float posY;
public float posZ;
public Vector3 mousePos;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//if (Input.GetMouseButton(0))
//{
// transform.position = new Vector3(Input.GetAxis("Mouse X"), transform.position.y, transform.position.z);
//}
posX = transform.position.x;
posY = transform.position.y;
posZ = transform.position.z;
}
void OnMouseDrag()
{
mousePos = Camera.main.ScreenToViewportPoint(Input.mousePosition);
transform.position = new Vector3(mousePos.x, transform.position.y, transform.position.z);
}
}