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

Rotate an object away from another

Discussion in 'Scripting' started by mate_veres, Apr 14, 2020.

  1. mate_veres

    mate_veres

    Joined:
    Oct 19, 2019
    Posts:
    72
    Good evening!

    I would like to implement a tree into my 3d game, which should fall to the opposite direction from the player when they collide. Can anyone help? Thanks in advance.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    To get the direction away from the player is as simple as:
    Code (CSharp):
    1. var awayFromPlayer = tree.transform.positon - player.transform.position.
     
  3. mate_veres

    mate_veres

    Joined:
    Oct 19, 2019
    Posts:
    72
    Hi, thank you for the quick answer. I tried it with transform.Rotate(treePosition-playerPosition) but it seems to fall in random positions.
     
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    No, you need to make the tree fall in direction awayfromPlayer (as calculated by PraetorBlue). If you always make the tree fall in the direction it is facing forwards, you first need to turn it so it faces the awayFromPlayer drirection (which may look stupid), and then initiate the fall over animation.

    You may need to realize that a direction is NOT a rotation. Use Quaternion.LookRotation for that.
     
  5. mate_veres

    mate_veres

    Joined:
    Oct 19, 2019
    Posts:
    72
    Thank you that's a good idea.
     
  6. mate_veres

    mate_veres

    Joined:
    Oct 19, 2019
    Posts:
    72
    Can you show me an example code to this?
     
  7. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    eses likes this.
  8. mate_veres

    mate_veres

    Joined:
    Oct 19, 2019
    Posts:
    72
    Thanks but does it have any other/correct way to do it, so the tree won't rotate on the y-axis and just then falls because it looks weird?
     
  9. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    I guess there are million ways of doing this, yes. How are you currently making your tree fall over? Perhaps post your code.
     
  10. mate_veres

    mate_veres

    Joined:
    Oct 19, 2019
    Posts:
    72
    No animation just physically fall down until it hits the ground.