I have a game in top-down view, with little ants who can walk up/down, left/right and diagonal (and all direction with Joystick).

My little player can move, but I can't rotate it properly... See what i mean in the youtube video of 7 seconds i made: https://www.youtube.com/watch?v=1mGMPhJ-u4s&feature=youtu.be

See the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Move : MonoBehaviour
public float lookSpeed = 10;
private Vector3 curLoc;
private Vector3 prevLoc;

void Update()
    transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(transform.position - prevLoc), Time.fixedDeltaTime * lookSpeed);

private void InputListen()
    prevLoc = curLoc;
    curLoc = transform.position;

    if (Input.GetKey(KeyCode.S))
        curLoc.y -= 1 * Time.fixedDeltaTime;
    if (Input.GetKey(KeyCode.Z))
        curLoc.y += 1 * Time.fixedDeltaTime;
    if (Input.GetKey(KeyCode.D))
        curLoc.x += 1 * Time.fixedDeltaTime;
    if (Input.GetKey(KeyCode.Q))
        curLoc.x -= 1 * Time.fixedDeltaTime;

    transform.position = curLoc;



I have tried a LOT of ways, but nothing worked... I have also an animator with a stylesheet (ants walking): enter image description here

enter image description here

thanks !

EDIT: i try another aproch for moving:

transform.Translate(Input.GetAxis("Horizontal") * speed * Time.deltaTime, Input.GetAxis("Vertical") * speed * Time.deltaTime, 0);

and for rotating:

transform.up = gameObject.GetComponent<Rigidbody>().velocity;

It move well.. but it doen't rotate when i move... BUT when i collide with another object, my player get some velocity, and then rotate correctly without touching any input, see in the video: https://www.youtube.com/watch?v=AS3agqJbVtI&feature=youtu.be

  • 1
    \$\begingroup\$ I cannot see the video \$\endgroup\$ – realUser404 Jan 30 '17 at 16:46
  • \$\begingroup\$ You want your character to be able to rotate and it's not working with the right joystick? \$\endgroup\$ – n_plum Jan 30 '17 at 16:56
  • \$\begingroup\$ Edited, i want to move my character, and set the rotation in the direction of movement... \$\endgroup\$ – Ugo Hed Jan 30 '17 at 17:19
  • 1
    \$\begingroup\$ Check here for some help: answers.unity3d.com/questions/510231/… \$\endgroup\$ – n_plum Jan 30 '17 at 17:25
  • 2
    \$\begingroup\$ Double-check your use of Quaternion.LookRotation. It assumes 3D rotation with the world up as the default upwards. For unity 2D this means the z-axis, in and out of the screen. The rigid body collision causes the proper rotation you're seeing because local (ie ant) x and y rotation are constrained, but rotation around the z-axis (again, the axis pointing in and out of the screen in unity 2D) is free. Your ant's local transform.up points along the length of its body, so that's the vector you should be rotating. \$\endgroup\$ – TOM__ Jan 30 '17 at 20:39

I Made it ! Thanks yo _TOM First, I draw guizmo to help me visualized the transform.up vector:

void OnDrawGizmos()
    Color color;
    color = (nb_player == 1) ? Color.blue : Color.red;
    // local up
    DrawHelperAtCenter(this.transform.up, color, 2f);

private void DrawHelperAtCenter(
                    Vector3 direction, Color color, float scale)
    Gizmos.color = color;
    Vector3 destination = transform.position + direction * scale;
    Gizmos.DrawLine(transform.position, destination);

Then I made my function of movement, called in Update():

void MoveNew()
    //I don't want any angular velocity when i don't move !
    gameObject.GetComponent<Rigidbody>().velocity = Vector3.zero;
    gameObject.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;

    if (nb_player == 1)//I have keyboard input ZQSD for player 1, and arrow key for player 2
        float heading = Mathf.Atan2(Input.GetAxis("Horizontal") * speed * Time.deltaTime, Input.GetAxis("Vertical") * speed * Time.deltaTime);
        //if not, the rotation will go back to 0 when no input happened, note the use of "GetAxisRaw" very important, because returning 0 when not moving
        if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0)
            transform.rotation = Quaternion.Euler(0f, 0f, -heading * Mathf.Rad2Deg);

        transform.Translate(((this.transform.up * Input.GetAxis("Vertical")) * speed) * Time.deltaTime);
        transform.Translate(((-this.transform.right * Input.GetAxis("Horizontal")) * speed) * Time.deltaTime);

    else if (nb_player == 2)
        float heading = Mathf.Atan2(Input.GetAxis("Horizontal2") * speed * Time.deltaTime, Input.GetAxis("Vertical2") * speed * Time.deltaTime);
        if (Input.GetAxisRaw("Horizontal2") != 0 || Input.GetAxisRaw("Vertical2") != 0)
            transform.rotation = Quaternion.Euler(0f, 0f, -heading * Mathf.Rad2Deg);

        transform.Translate(((this.transform.up * Input.GetAxis("Vertical2")) * speed) * Time.deltaTime);
        transform.Translate(((-this.transform.right * Input.GetAxis("Horizontal2")) * speed) * Time.deltaTime);

I havn't try it wet With Joystick, but i believe the result is the same...


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.