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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

How to change the sprite and direction at the moment of impact?

Discussion in 'Scripting' started by polan31, Apr 1, 2018.

  1. polan31

    polan31

    Joined:
    Nov 20, 2017
    Posts:
    149
    Hey everyone!

    I'm trying to create a 2D "Bullet" that bounces off the every Box Collider 2D.

    I added to the Bullet: Rigidbody2D ( with zero gravity) and Polygon Collider 2D.

    I have no problem with creating a Bullet movement, but I want rotate Bullet in the direction of movement and change the sprite when it hits Box Collider 2D.

    The bullet should change sprite only for a second (at the moment of impact) and then return to the previous sprite.

    I tried to change it with animation, but it didn't look convincing.

    Also I still don't know how to rotate the Bullet.


    This situation is best explained by the pictures.

    The first and fourth pictures shows how the movement looks like.
    (The bullet moves in the right way but without sprite rotation).
    I understand that I should change it using a script, but I don't know how.

    The second picture shows how the bullet should change the sprite for a moment when hit Box Collider 2D.
    The change should take a second and then returns to the first sprite.

    The third picture shows how the further move should look .

    Could someone help me solve my problem?

    Forgive me for such a poor picture form, but I am waiting in the queue for the doctor and I decided to draw a situation with which I have a problem and ask :)
     

    Attached Files:

  2. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    638
    Look into collision.contacts.normal

    That should get you where you need to be
     
  3. polan31

    polan31

    Joined:
    Nov 20, 2017
    Posts:
    149

    Hi
    First of all, thank you for your answer.
    I tried but unfortunately it doesn't work.
    I'll try a little more and let you know what and how.
    Thanks again
     
  4. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    638
    My bad... i just now realised youre trying to rotate sprite, not the direction.
     
  5. polan31

    polan31

    Joined:
    Nov 20, 2017
    Posts:
    149
    Yes, exactly, rotate not direction. I've already solved the problem of changing the image, but I still can't change the rotation by 90 degrees.
     
  6. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    638
    Couldnt you just calculate the angle and invert it?
     
    polan31 likes this.
  7. polan31

    polan31

    Joined:
    Nov 20, 2017
    Posts:
    149
    I tried to avoid this because I don't understand this method, but I will try to do something :)
    Thanks again for answer ;)
     
    Homicide likes this.
  8. polan31

    polan31

    Joined:
    Nov 20, 2017
    Posts:
    149
    Ok. I have something like that:

    float zRotation = 90.0f;
    transform.eulerAngles = new Vector3( transform.eulerAngles.x, transform.eulerAngles.y, zRotation);

    It allows me to rotate 90 degrees after hitting in Box Collider, but how to transform it so that it changes with every hit in the direction of movement?
     
  9. polan31

    polan31

    Joined:
    Nov 20, 2017
    Posts:
    149
    Okay, I changed it completely and now there is a rotation in the direction of movement, but ...

    Now I have a different problem:

    As you can see in the first picture, the sprite is positioned in a specific position, but when I press the play the sprite changes it (look picture number II) .

    Important, the ball is still moving in the right direction .

    Just sprite is inverted.

    Does anyone know how to solve this?

    My new code:

    void Update() {

    rb.velocity = constantSpeed * (rb.velocity.normalized);

    // Make it look in direction of travel

    float angle = Mathf.Atan2(rb.velocity.y, rb.velocity .x) * Mathf.Rad2Deg;

    rb.rotation = angle - 90;
    }
     

    Attached Files: