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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Move object in rotating direction.

Discussion in 'Scripting' started by Abdou23, Dec 13, 2015.

  1. Abdou23

    Abdou23

    Joined:
    May 2, 2014
    Posts:
    77
    I have an object that is being rotated constantly, I want to move that object towards that moving direction on a mouse click.

    Code (CSharp):
    1. void Update ()
    2.     {
    3.         if (stationary)
    4.         {
    5.             transform.rotation = Quaternion.Euler(0,0, Mathf.Sin(Time.time * 2) * 60);
    6.  
    7.         }
    8.  
    9.         if (Input.GetMouseButtonDown(0))
    10.         {
    11.             Debug.Log("Button");
    12.             stationary = false;
    13.             myBody.velocity = new Vector2(transform.rotation.z , 10);
    14.         }
    15.  
    16.     }
    The code works, but the object always move up instead towards the direction it's facing, how to fix that?
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    What's the moving direction?

    A rotating object doesn't necessarily have a 'moving direction', it's rotating...

    A wheel might have a direction that results from when it touches a surface. But that's determined by the rotation when applied against a surface. A wheel rotating clockwise and laying on the floor may move right, but on the wall will move up, and on the ceiling will move left.

    So, you need more information.

    Or, do you mean something else. Like say it's an arrow rotating, and you want it to move in the direction the arrow points when you stop? Well, again, we need more information. What's "forward", the direction the arrow points when no rotation is applied. You then would have to rotate that direction about some axis (z?) the amount of rotation it is currently at.

    If say the 'forward' is <1,0> at zero rotation, and the axis rotates counter-clockwise, it my be:
    <cos(theta), sin(theta)>

    But again, it depends on what you define as forward, what you define as the axis of rotation, etc.
     
  3. Magison

    Magison

    Joined:
    Mar 1, 2013
    Posts:
    21
    If you want something like: an arrow rotating and by mouse click the arrow move in arrow-direction (forwards), use:

    Code (csharp):
    1.  
    2. 13.      myBody.velocity = myBody.forward * moveSpeed
    3.  
    4. or
    5.  
    6. 13.      transform.velocity = transform.forward * moveSpeed
    7.  
    This works if your object is orientated forwards in the z direction. If your object is orientated in an other axis, you have to use transform.up or transform.left instead.
     
  4. Abdou23

    Abdou23

    Joined:
    May 2, 2014
    Posts:
    77
    I tried this, but it doesn't go anywhere, it stays at it is:

    Code (CSharp):
    1. transform.Translate(Vector3.forward * 20);
    2. And
    3. myBody.velocity = Vector3.forward * 20;
     
  5. CloudKid

    CloudKid

    Joined:
    Dec 13, 2015
    Posts:
    207
    Well did you tried myBody.forward or Vector3.forward, they are not the same thing. Also, with Vector3.forward your object should move on z.
     
  6. Abdou23

    Abdou23

    Joined:
    May 2, 2014
    Posts:
    77
    Is there something called 'myBody.forward" ?! it keeps giving me errors, you do realise that "myBody" is a RigidBody, right ?
     
  7. CloudKid

    CloudKid

    Joined:
    Dec 13, 2015
    Posts:
    207
    My bad, use myBody.transform.forward, or transform.forward of the object you whant to move
    Code (CSharp):
    1. myBody.velocity=transform.forward*20;
    Also, if the rotation is weird, take note that forward represent a vector depicting the direction in 3 dimensional space your character is facing (assuming your character is setup where forward is initially along the Z axis). So take into account initial orientation.
     
  8. Abdou23

    Abdou23

    Joined:
    May 2, 2014
    Posts:
    77
    This worked, Thanks very much :)