Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Move a rotated object along it's X and Z-axis

Discussion in 'Scripting' started by Louis_Amerongen, Oct 30, 2018.

  1. Louis_Amerongen

    Louis_Amerongen

    Joined:
    Jul 21, 2017
    Posts:
    19
    Hi,

    We made a script where we can drag and rotate objects with the mouse with an arrow. When the object is unrotated, this works good. After the object is rotated however, the object stil move along it's x an z position.
    Unfortunately changing the transform.parent.parent.position to ....localPosition doesn't work.

    Does anyone have a solution to this problem?


    Thanks in advance,

    Louis


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MoveX : MonoBehaviour {
    6.  
    7.     private Vector3 mousePosition;
    8.     private Vector3 currentMousePosition;
    9.     private float distance;
    10.  
    11.     private void OnMouseDown()
    12.     {
    13.         currentMousePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y,
    14.                                                               -Camera.main.transform.position.z));
    15.  
    16.         distance = currentMousePosition.x - transform.position.x;
    17.     }
    18.  
    19.     private void OnMouseDrag()
    20.     {
    21.         mousePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y,
    22.                                                        -Camera.main.transform.position.z));
    23.  
    24.        
    25.         Vector3 distanceAsVector = new Vector3(distance, 0, 0);
    26.         Vector3 mousePositionAsVector = new Vector3(mousePosition.x, transform.position.y, transform.localPosition.z);
    27.         transform.parent.parent.position = mousePositionAsVector - distanceAsVector;
    28.     }
    29.    
    30.  
     
  2. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    449
    Use the objects forward and right instead of world axis?
     
  3. Louis_Amerongen

    Louis_Amerongen

    Joined:
    Jul 21, 2017
    Posts:
    19
    Hi FlashMuller,

    Thanks for you're reply.
    We've tried to use the vector3.forward, etc instead of the 0,0,blabla, but unfortunately without a good result.
    Can you be a little more specific or does anyone has an other solution to this problem.


    Thanks in advance,

    Louis
     
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    multiply the objects rotation by your input.
     
  5. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    I wanted to drop this in here earlier, too bad i don't have unity at work, haha

    Code (CSharp):
    1.         CamRigY.position += Quaternion.Euler(0,CamRigY.eulerAngles.y,0)//make WSAD relative to look rotation #Quaternion black magic
    2.             * new Vector3(Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"))// move X by HRZ, move Z by VRT
    3.             * moveSpeed * Time.deltaTime;
    this snippet is from my RTS camera controller.