2
\$\begingroup\$

I'm using this script to move my character in my 2d world:

    public float speed;   //Movementspeed

public Rigidbody2D rbody;
Animator anim;

void Start () {

    rbody = GetComponentInChildren<Rigidbody2D> ();
    anim = GetComponent<Animator> ();
}

void FixedUpdate () {

    Vector2 movement_vector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));

    if (movement_vector != Vector2.zero) {
        anim.SetBool("iswalking", true);
        anim.SetFloat("input_x", movement_vector.x);
        anim.SetFloat("input_y", movement_vector.y);
    } else {
        anim.SetBool("iswalking", false);
    }

    rbody.MovePosition (rbody.position + movement_vector * speed * Time.deltaTime);

}

}

What I want is to somehow register where my character is facing/walking towards to be able to Instansiate a gameobject infront of it.

How do I do that?

\$\endgroup\$
  • \$\begingroup\$ Does anim.SetFloat("input_x", movement_vector.x); take care of rotating the main gameobject ?? What does this animation do ?? \$\endgroup\$ – Hash Buoy Aug 23 '15 at 20:22
  • \$\begingroup\$ Can't you just use the normalized movement_vector? Wouldn't that give you the direction your player is moving? I assume he moves in the direction he is facing and doesn't have a strafe. \$\endgroup\$ – Savlon Aug 24 '15 at 9:33
  • \$\begingroup\$ Yeah I figured it out using the movement_vector.x and y variables \$\endgroup\$ – Grimbox Aug 24 '15 at 13:51
1
\$\begingroup\$

Add a Vector2 private variable to your class. This variable will tells you at any time where your character is facing:

private Vector2 facingDirection = Vector2.zero;
...
void FixedUpdate () {

    Vector2 movement_vector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));

    if (movement_vector != Vector2.zero) {
        anim.SetBool("iswalking", true);
        anim.SetFloat("input_x", movement_vector.x);
        anim.SetFloat("input_y", movement_vector.y);
        facingDirection= movement_vector;  // You save the direction only when the object is moving
        direction.Normalize() // Normalize it as the magnitude does not give any valuable information
    } else {
        anim.SetBool("iswalking", false);
    }

    rbody.MovePosition (rbody.position + movement_vector * speed * Time.deltaTime);

}

// Example of usage
private void createObject()
{
    GameObject newObject;
    float distance = 3; // instantiate an object 3 units in front of the character

    // Now you can access the current direction of the character anywhere in the class
    Instantiate(newObject, transform.position + facingDirection*distance, Quaternion.identity);
}
\$\endgroup\$
0
\$\begingroup\$

You could try and check if the distance between two objects is increasing or decreasing or you can use an infinite raycast and check the object it hits.

\$\endgroup\$
0
\$\begingroup\$

Are you just looking for Transform.forward?

\$\endgroup\$

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.