- Home /
 
Rotate Z or X Axis of tree Away from player.
I am trying to get a tree to fall, when tree is out of wood resource(random amount). At the moment i have the tree so it will very slowly, snail speed, sink into the ground. But at same time i want it to fall/rotate away from the player on the Z or X axis. I do not want the tree to fall on the player.
How can i achieve this?
Thanks in advance.
EDIT* Let me explain it better(hopefully): Say the player chops the tree from its south, the tree should fall to the north.Vice Versa: Player chops at trees west tree falls east. (Except -x, +x, -z, +z, instead of north, east,west and south)
Hope that explains a bit better! :)
CODE:
 void Update () {
         if(resourceAvailable <= 0){
             objectDead = true;
 
             switch(ObjectType){
             case Type.Tree:
 
 
                 TreeDead();
                 break;
             case Type.Rock:
                 
                 break;
             case Type.Animal:
                 
                 break;
             default:
                 break;
             }
 
         }
     }
 
     void TreeDead(){
             Vector3 fallPos = transform.position;
             fallPos.y -= treeSinkSpeed * Time.deltaTime;
             transform.position = fallPos;
     
             if(transform.rotation.x < 90){
                 Vector3 dir = transform.position - Player.position;
                 Vector3 axis = Vector3.Cross(dir, Vector3.up);
                 transform.rotation = Quaternion.AngleAxis(treeFallSpeed * Time.deltaTime, axis);
     //            Quaternion fallRotation = transform.rotation;
     //            fallRotation.x += treeFallSpeed * Time.deltaTime;
     //            transform.rotation = fallRotation;
             }
         }
 
              While i have been waiting for an answer, i managed to get it to fall but on the X axis only(not away from player).
Anyone got an answer.
Answer by robertbu · Feb 09, 2014 at 06:56 PM
There are a lot of unknowns here. If you are talking about the direct manipulation of the transform, you can solve it by using an angle/axis rotation. Vector3.Cross() can be used to calculate the axis. Something like:
 var dir = transform.position - character.transform.position;
 var axis = Vector3.Cross(dir, Vector3.up);
 
               Then in update you can rotate your tree:
 transform.rotation = Quaternion.AngleAxis(speed * Time.deltaTime, axis);
 
               Note this will rotate the object around the pivot point. If this point is not at the base of the tree, you'll have more work to do.
I am trying this solution now, but it seems to do nothing but "Wobble".
In the absence of the rest of your code, I cannot say why it is not working. $$anonymous$$y guess is that you have more than one thing impacting the rotation...another script or colliders or something.
Your answer
 
             Follow this Question
Related Questions
Constant Rotation to an Object 7 Answers
How to stop rotation in x and y axis??? 2 Answers
Distance between 2 objects without their Z axis. 1 Answer