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

Rotating a fire sprite to face the opposite direction of where a ball sprite is headed.

Discussion in 'Scripting' started by ShaeHumphries, Apr 16, 2015.

  1. ShaeHumphries

    ShaeHumphries

    Joined:
    Feb 5, 2015
    Posts:
    1
    Hi guys, I'm currently working on a school assignment where I need to 'pimp out' breakout. The game is using 3D physics with 2D sprites in an orthographic view. As such, all the main rotation for the ball is on the z-axis and the ball moves upwards on the y-axis and side to side on the x-axis. I currently have a ball, and as a child of the ball I have an animation of a flame, giving the ball the appearance that it's on fire. I also have a trail renderer attached to the ball to give it the appearance of a flame trail being left behind it. My question is, is there a way to rotate the fire so that it either:

    A. Goes opposite the direction the ball is moving, so it appears that it's being affected by 'wind'
    B. The tip of the fire always stays at the start of the trail renderer, which is already following the ball how I would like the fire to.

    I've attached an image of what I'd LIKE the fire to do (set it up during a paused test).

    C# preferred!

    Thanks guys :)
     

    Attached Files:

  2. Sydious

    Sydious

    Joined:
    Apr 1, 2010
    Posts:
    172
    I see you haven't gotten any replies yet. Maybe I can shed some light on how I would go about dealing with this.

    I would set the sprite up to be rotated such that when the ball is traveling RIGHT and flames would be behind the ball pointing LEFT (I think that is what you are looking for).

    Then I would attach a script to the sprite object to make it face the direction it is traveling.

    Something like this. Sorry it is in Java:
    Code (JavaScript):
    1. var dir = rigidbody2D.velocity;
    2. var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
    3. var q = Quaternion.AngleAxis(angle, Vector3.forward);
    4. transform.rotation = Quaternion.RotateTowards(transform.rotation, q, rotateSpeed * Time.deltaTime);
     
  3. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    *twitch twitch* you mean... java... script? *twitch*

    ;)
     
  4. Sydious

    Sydious

    Joined:
    Apr 1, 2010
    Posts:
    172
    yes..yes....Java Script....