Question by
KamiSama00 · Nov 27, 2019 at 02:19 AM ·
script.dragsmoothclamp
Smooth Drag
Hello Community,
I'm trying to create a script that controls a spheres movement. I'm attempting to use mousedrag and clamp to slide the sphere between two points on the x axis (-1,1). It currently works, however its pretty choppy, and it doesn't move forward as intended when the mousedrag function is activated. I'm inexperienced so any help will work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class plyrCntrl : MonoBehaviour
{
bool dragEnabled = false;
Vector3 dragStartPosition;
float dragStartDistance;
float speed = 3f;
void OnMouseDown()
{
dragEnabled = true;
dragStartPosition = transform.position;
dragStartDistance = (Camera.main.transform.position - transform.position).magnitude;
}
void Update()
{
transform.position -= transform.forward * Time.deltaTime * speed;
if (Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1))
{
dragEnabled = false;
}
}
void OnMouseDrag()
{
if (dragEnabled)
{
Vector3 worldDragTo = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, dragStartDistance));
transform.position = new Vector3(worldDragTo.x, dragStartPosition.y, dragStartPosition.z );
Vector3 pos = transform.position;
pos.x = Mathf.Clamp(transform.position.x,
-1.0f, 1.0f); transform.position = pos; } } }
Thanks In Advance
Comment
Your answer
Follow this Question
Related Questions
2D drag script Need Help! 1 Answer
how to drag a instantiate Panel (prefab)? 0 Answers
drag and merge object 0 Answers
Fade in and Fade Out Camera Shaking Effect Over Time 0 Answers
Smoothly multiply float 0 Answers