Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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: alt text

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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

156 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges