Question by
abers1997 · Aug 27, 2017 at 01:41 AM ·
movementtransformpositiononmousedown
How to move gameObject along a Vector3 with OnMouseDrag()?
I've been stuck on this for months.
I followed a good tutorial to make a rail system where the object moves along either a Linear path or Catmull path and it works great. All that's left to do is to have OnMouseDrag() move the gameObject on Vector3 LinearPosition and/or on Vector3 CatMullPosition but I've had no luck achieving this.
This is the target concept:
Here are both scripts I'm working with:
Rail script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public enum PlayMode
{
Linear,
Catmull,
}
[ExecuteInEditMode]
public class Rail : MonoBehaviour {
#if UNITY_EDITOR
public Transform[] nodes;
private void Start()
{
nodes = GetComponentsInChildren<Transform>();
}
public Vector3 PositionOnRail(int seg, float ratio, PlayMode mode)
{
switch(mode)
{
default:
case PlayMode.Linear:
return LinearPosition(seg, ratio);
case PlayMode.Catmull:
return CatmullPosition(seg, ratio);
}
}
public Vector3 LinearPosition(int seg, float ratio)
{
Vector3 p1 = nodes[seg].position;
Vector3 p2 = nodes[seg + 1].position;
return Vector3.Lerp(p1, p2, ratio);
}
public Vector3 CatmullPosition(int seg, float ratio)
{
Vector3 p1, p2, p3, p4;
if (seg == 0)
{
p1 = nodes[seg].position;
p2 = p1;
p3 = nodes[seg + 1].position;
p4 = nodes[seg + 2].position;
}
else if (seg == nodes.Length - 2)
{
p1 = nodes[seg - 1].position;
p2 = nodes[seg].position;
p3 = nodes[seg + 1].position;
p4 = p3;
}
else
{
p1 = nodes[seg - 1].position;
p2 = nodes[seg].position;
p3 = nodes[seg + 1].position;
p4 = nodes[seg + 2].position;
}
float t2 = ratio * ratio;
float t3 = t2 * ratio;
float x =
0.5f * ((2.0f * p2.x)
+ (-p1.x + p3.x) * ratio
+ (2.0f * p1.x - 5.0f * p2.x + 4 * p3.x - p4.x)
* t2 + (-p1.x + 3.0f * p2.x - 3.0f * p3.x + p4.x)
* t3);
float y =
0.5f * ((2.0f * p2.y)
+ (-p1.y + p3.y) * ratio
+ (2.0f * p1.y - 5.0f * p2.y + 4 * p3.y - p4.y)
* t2 + (-p1.y + 3.0f * p2.y - 3.0f * p3.y + p4.y)
* t3);
float z =
0.5f * ((2.0f * p2.z)
+ (-p1.z + p3.z) * ratio
+ (2.0f * p1.z - 5.0f * p2.z + 4 * p3.z - p4.z)
* t2 + (-p1.z + 3.0f * p2.z - 3.0f * p3.z + p4.z)
* t3);
return new Vector3(x, y, z);
}
public Quaternion Orientation(int seg, float ratio)
{
Quaternion q1 = nodes[seg].rotation;
Quaternion q2 = nodes[seg + 1].rotation;
return Quaternion.Lerp(q1, q2, ratio);
}
private void OnDrawGizmos()
{
for (int i = 0; i < nodes.Length - 1; i++)
{
Handles.DrawDottedLine(nodes[i].position, nodes[i + 1].position, 3.0f);
}
}
#endif
}
Mover script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Mover : MonoBehaviour {
public Rail rail;
public PlayMode mode;
public float speed = 2.5f;
public bool isReversed;
public bool isLooping;
public bool pingPong;
private int currentSeg;
private float transition;
private bool isCompleted;
private void Update()
{
if (!rail)
{
return;
}
if (!isCompleted)
{
Play(!isReversed);
}
}
private void Play(bool forward = true)
{
float m = (rail.nodes[currentSeg + 1].position - rail.nodes
[currentSeg].position).magnitude;
float s = (Time.deltaTime * 1 / m) * speed;
transition += (forward) ? s : -s;
if (transition > 1)
{
transition = 0;
currentSeg++;
if (currentSeg == rail.nodes.Length - 1)
{
if (isLooping)
{
if (pingPong)
{
transition = 1;
currentSeg = rail.nodes.Length - 2;
isReversed = !isReversed;
}
else
{
currentSeg = 0;
}
}
else
{
isCompleted = true;
return;
}
}
}
else if (transition < 0)
{
transition = 1;
currentSeg--;
if (currentSeg == - 1)
{
if (isLooping)
{
if (pingPong)
{
transition = 0;
currentSeg = 0;
isReversed = !isReversed;
}
else
{
currentSeg = rail.nodes.Length - 2;
}
}
else
{
isCompleted = true;
return;
}
}
}
transform.position = rail.PositionOnRail(currentSeg, transition, mode);
transform.rotation = rail.Orientation(currentSeg, transition);
}
}
untitled.png
(138.9 kB)
Comment
Your answer
Follow this Question
Related Questions
Have a character constantly face an enemy. C# 0 Answers
Make object move back and forth 2 Answers
how can I move an object around a parent object manually? 0 Answers
Stop moving gameObject and push it back 0 Answers
Player teleport not working 0 Answers