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. Dismiss Notice

My rotations aren't applied to my game object

Discussion in 'Scripting' started by Jeff-MX, Jul 31, 2022.

  1. Jeff-MX

    Jeff-MX

    Joined:
    May 8, 2022
    Posts:
    4
    I have a model with an armature that has a body and a gun attech to it, all his parts can be transformed/rotated/scaled but the gun rotations in specific seems to be "locked". I can rotate in Scene view but in Game view it can only be rotated if I go into "Cube.002" parented with the bone.

    upload_2022-7-31_12-19-20.png

    Here is what it look like.
    ezgif.com-gif-maker.gif

    And it should do this (like the other cube)
    ezgif.com-gif-maker (1).gif

    Notes: those lines(if you can see them) are in debug and basically is what i use to get an angle to apply the rotation. The cyan is for the gun, and the magenta is for the cube.

    Code:
    Code (CSharp):
    1. void Update()
    2.     {
    3.         Vector3 GunPos = new Vector3(Gun.transform.position.x,
    4.                                       Gun.transform.position.y,0);
    5.  
    6.         Vector3 BallPos = new Vector3(Ball.transform.position.x,
    7.                                       Ball.transform.position.y,0);
    8.  
    9.         Debug.DrawLine(GunPos,BallPos,Color.magenta);
    10.  
    11.         float angle = Mathf.Atan2((GunPos.x-BallPos.x),(GunPos.y-BallPos.y))* Mathf.Rad2Deg;
    12.  
    13.        Gun.transform.localRotation = Quaternion.Euler(0,0,-angle+90);
    14.     }
    Update: Actually the bone that control the gun only rotates 360° of his starting rotation, so if it's in -90 I can only change the rotation in 270, 630, 960 and so on. I can't even seen it rotating.
     
    Last edited: Jul 31, 2022
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Looks like there's an animation driving the rotation, which is forcing a value? I can see the character bobbing slightly.

    You should also fix your mesh's scale and orientation, I'm guessing it's from Blender, it's way out of whack from Unity's.