Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Need help with rotation to direction of movement

Discussion in '2D' started by hollysephiroth, Jan 6, 2015.

  1. hollysephiroth

    hollysephiroth

    Joined:
    Mar 25, 2014
    Posts:
    19
    Hello I m trying to make game with geometry wars / superstardust way of controlls .

    right now i am using his code
    Code (CSharp):
    1. Hello  I m trying to make game with geometry wars / superstardust way of controlls .
    2.  
    3. right now i am using his code
    4. using UnityEngine;
    5. using System.Collections;
    6.  
    7. public class GWConntorls : MonoBehaviour {
    8.  
    9.  
    10.     public Vector3 startPosition;
    11.     public Vector2 speed = new Vector2(1,0);
    12.  
    13.     void Start(){
    14.      
    15.  
    16.  
    17.     }
    18.     void Update () {
    19.      
    20.      
    21.         float inputX = Input.GetAxis("Horizontal");
    22.         float inputY = Input.GetAxis ("Vertical");
    23.      
    24.  
    25.      
    26.         Vector3 movement = new Vector3
    27.             (speed.x*inputX, speed.y*inputY,0);
    28.      
    29.         movement*= Time.deltaTime;
    30.         transform.Translate(movement)
    31.  
    32.     }  
    33.     }
    But what I need is for the object seen from the top to rotate in direction of movement .
    Also I think that it moves too fast in diagonal

    And Im not sure how to do that , I'm no programer :(

    Could anyone help me ?
    thank you
     
  2. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    If I understand it right, because you're using TRANSLATE, you're adding a specific amount of X and Y movement based on user input. This means that when you move in both X AND Y at the same time, ie diagonal, you will add all of the x distance and all of the y distance, which, on a diagonal, is further than a horizontal or vertical movement alone. ie you're moving across the diagonal of a box, instead of along the edges.

    What you need is that, when the user is moving in X and Y at the same time, scale down the distance moved.

    Since you're apparently not simulating 360 degree movement (because you'd have to maintain a vector or angle of movement), you actually are doing 8-way movement right now. So all you'd need to do is detect when both inputX and inputY are not zero ... and in that case, divide both speed.x and speed.y by around 1.414213562, which roughly gives you the hypotenuse of the diagonal without dipping into cos/sine math. See how that works out.

    Otherwise you're going to need to keep track of an angle, and a speed, as single numbers, and then when there is user input, convert the input to a vector or to an angle modifcation, and then use like transform.localPosition.x += speed * Mathf.cos(angle); and similarly transform.localPosition.y += speed * Mathf.sin(angle); or something like that... to get an x and y amount to add to the position.
     
    theANMATOR2b likes this.
  3. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    so like

    if (inputX != 0 && inputY != 0){
    speed.x = speed.x / 1.414213562;
    speed.y = speed.y / 1.414213562;
    }

    before you calculate the `movement` variable. but if this will happen every frame, so every frame you'll need to start out with initializing speed.x and y to their defaults.