I’m learning C# and am trying to make a simple scene with a ball where you click it, pull the mouse, release and it should launch in the opposite direction.
I’ve gotten the scene to work but for some reason it sticks to only 2 dimensions and not the two I actually need.
Here is the code I’ve got so far. What I need help with is figuring out why I’m stuck on 2 axis but no matter which axis I lock to 0, the ball never moves on the x,z axis (the result I want).
Vector3 mMouseDownPos;
Vector3 mMouseUpPos;
public float speed = .1f;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
void OnMouseDown()
{
mMouseDownPos = Input.mousePosition;
Debug.Log( "the mouse down pos is " + mMouseDownPos.ToString() );
mMouseDownPos = Input.mousePosition;
var MouseDownPos = 1;
Debug.Log( "the mouse down pos is " + mMouseDownPos.ToString() );
mMouseDownPos.z = 0;
}
void OnMouseUp()
{
mMouseUpPos = Input.mousePosition;
mMouseUpPos = Input.mousePosition;
mMouseUpPos.z = 0;
var MouseUpPos = 1;
var direction = mMouseDownPos - mMouseUpPos;
direction.Normalize();
rigidbody.AddForce (direction * speed);
Debug.Log( "the mouse up pos is " + mMouseUpPos.ToString() );
}
}
As I said, I’m extremely new to C# so any help would be appreciated. Thanks.