Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Position X is not changing when condition is met.

Discussion in 'Scripting' started by cabell95, Aug 13, 2019.

  1. cabell95

    cabell95

    Joined:
    Nov 8, 2018
    Posts:
    3
    When the arm rotates, the Y flips just fine when rotPosZ is within the defined condition, but the X position of the arm does not move. The print statements correctly show what the values should be in the console however the actual position stays the same.

    Pardon the messy code ;)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ArmRotation : MonoBehaviour {
    5.  
    6.     public int rotationOffset = 0;
    7.     SpriteRenderer spriteRenderer;
    8.  
    9.  
    10.     void Start()
    11.     {
    12.  
    13.         spriteRenderer = GetComponent<SpriteRenderer>();
    14.        
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update() {
    19.         Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;      
    20.         difference.Normalize();                                                                            
    21.        
    22.         float posX = transform.localPosition.x;
    23.         float posY = transform.localPosition.y;
    24.         float flipX = posX * -1;
    25.         float rotPosZ = transform.eulerAngles.z;
    26.         float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;                              
    27.         transform.rotation = Quaternion.Euler(0f, 0f, rotZ + rotationOffset);
    28.  
    29.         if ( rotPosZ > 90 && rotPosZ < 270)
    30.         {
    31.             spriteRenderer.flipY = true;
    32.             transform.position = new Vector3(flipX, posY, 0f);
    33.             print(flipX);
    34.         }
    35.         else
    36.         {
    37.             spriteRenderer.flipY = false;
    38.             print(posX);
    39.         }
    40.    
    41.     }
    42. }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Not sure what you're trying to do, but this code could result in the x position flipping back and forth between positive and negative every frame over and over. Whether you notice that depends on what the value is. For example if the x position was 0f then you wouldn't see any change.
     
  3. cabell95

    cabell95

    Joined:
    Nov 8, 2018
    Posts:
    3
    In a 2D arm rotation script, I am trying to move the arm object's x position when the eulerAngles.z value is within a certain range. I can get the Y to flip when the condition is met, but the X does not move like i expect it to in line 32. My print statements show in the console the correct x value I want, but the object itself does not move on the x axis at all.
     
  4. cabell95

    cabell95

    Joined:
    Nov 8, 2018
    Posts:
    3
    See the uploaded screenshots for what I am trying to achieve. The first two rows of screenshots are what I am currently getting while testing in unity, and the third row of screenshots is what I am trying to achieve.
     

    Attached Files: