Search Unity

same magnitude on different axes?

Discussion in 'Scripting' started by theRelation, Aug 29, 2014.

  1. theRelation

    theRelation

    Joined:
    Aug 18, 2014
    Posts:
    33
    can you take the magnitude of an object on one axis and put it inside of a variable and give it to a separate object on a different axis? does magnitude not work that way?
     
  2. FlaSh-G

    FlaSh-G

    Joined:
    Apr 21, 2010
    Posts:
    212
    magnitude is simply the length of a vector.
    Accordingly, there is no magnitude of an object. I suppose you mean its position.
    So I guess what you want is... this:
    Code (JavaScript):
    1. someTransform.position.x = someTransform.position.z;
    ...or something like that?
     
  3. theRelation

    theRelation

    Joined:
    Aug 18, 2014
    Posts:
    33
    Is it easier to do stuff like this in Javascript rather than C#?
     
  4. theRelation

    theRelation

    Joined:
    Aug 18, 2014
    Posts:
    33
    Also, won't I have to put the position in a variable before writing that?
     
    Last edited: Aug 30, 2014
  5. FlaSh-G

    FlaSh-G

    Joined:
    Apr 21, 2010
    Posts:
    212
    This if one the few things that I consider better in JavaScript, you can just write this line. In C#, you indeed have to store it in a local variable, change it, and put it back in again.
     
  6. theRelation

    theRelation

    Joined:
    Aug 18, 2014
    Posts:
    33
    like this? i'm trying to make a jump (and other mid-air mechanics) in a zelda game by getting a sprite renderer to move on z based on the position on y of a separate rigidbody for the character, and it doesn't work...

    it's code i modified from this http://answers.unity3d.com/questions/12647/tranform-position.html

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class midAirController : MonoBehaviour
    5. {
    6.     private struct lastPosition;  // The last position.
    7.    
    8.    
    9.    
    10.     public Transform target;
    11.    
    12.     // Initialize in Start (or Awake):
    13.    
    14.    
    15.    
    16.     void Start()
    17.     {
    18.         lastPosition.y = target.localPosition;
    19.     }
    20.    
    21.     void Update()
    22.     {
    23.        
    24.        
    25.         if(target.position.y += .01)
    26.         {
    27.             transform.localPosition.z += .01; // Move to the up, or whatever you want.
    28.            
    29.         }
    30.         else if(target.position.y -= .01)
    31.            
    32.            
    33.            
    34.            
    35.            
    36.            
    37.         {
    38.             transform.localPosition.z -= .01; // Move to the down, or whatever you want.
    39.            
    40.            
    41.            
    42.            
    43.            
    44.            
    45.            
    46.         }
    47.     }
    48.  
    49. }
    50.  
     
  7. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Last time I checked Y was UP... :)
     
  8. theRelation

    theRelation

    Joined:
    Aug 18, 2014
    Posts:
    33
    not if the camera is turned 90 degrees :)
     
  9. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Even if the camera is turned 90 degrees :)
    Edit: ok... if you use localPosition you can be right... :)
     
  10. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class midAirController : MonoBehaviour
    5. {
    6.     private struct lastPosition;  // The last position.
    7.  
    8.  
    9.  
    10.     public Transform target;
    11.  
    12.     // Initialize in Start (or Awake):
    13.  
    14.  
    15.  
    16.     void Start()
    17.     {
    18.         lastPosition.y = target.localPosition; // <<<<< assign a Vector3 to a float?
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.    
    24.    
    25.         if(target.position.y += .01) // <<< this is not the same as: if(target.position.y == target.position.y + .01f)
    26.         {
    27.             transform.localPosition.z += .01; // Move to the up, or whatever you want.
    28.        
    29.         }
    30.         else if(target.position.y -= .01) //<<< same as above
    31.        
    32.        
    33.        
    34.        
    35.        
    36.        
    37.         {
    38.             transform.localPosition.z -= .01; // Move to the down, or whatever you want.
    39.        
    40.        
    41.        
    42.        
    43.        
    44.        
    45.        
    46.         }
    47.     }
    48.  
    49. }
    50.  
    [/QUOTE]
     
    Last edited: Sep 3, 2014
  11. theRelation

    theRelation

    Joined:
    Aug 18, 2014
    Posts:
    33
    isn't it okay if i assign lastPosition as a struct?

    how do i assign vector3 to a float? like this?:

    Code (CSharp):
    1. Vector3 lastPosition = transform.localPosition;
    2. lastPosition.y = target.localPosition;
    3.         transform.localPosition = lastPosition;
     
  12. theRelation

    theRelation

    Joined:
    Aug 18, 2014
    Posts:
    33
    when i do this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class midAirController : MonoBehaviour
    5. {
    6.     private float lastPosition;  // The last position.
    7.  
    8.  
    9.  
    10.     public Transform target;
    11.  
    12.     // Initialize in Start (or Awake):
    13.  
    14.  
    15.  
    16.     void Start()
    17.     {
    18.         Vector3 lastPosition = transform.localPosition;
    19.         lastPosition.y = target.localPosition;
    20.         transform.localPosition = lastPosition;
    21.  
    22.     }
    23.  
    24.     void Update()
    25.     {
    26.      
    27.      
    28.         if(target.position.y == target.position.y + .01f)
    29.         {
    30.             transform.localPosition.z += .01; // Move to the up, or whatever you want.
    31.          
    32.         }
    33.         else if(target.position.y == target.position.y - .01f)
    34.          
    35.          
    36.          
    37.          
    38.          
    39.          
    40.         {
    41.             transform.localPosition.z -= .01; // Move to the down, or whatever you want.
    42.          
    43.          
    44.          
    45.          
    46.          
    47.          
    48.          
    49.         }
    50.     }
    51.  
    52. }
    53.  
    i get these errors:

    Error CS0029: Cannot implicitly convert type `UnityEngine.Vector3' to `float' (CS0029)

    this has been the hugest stumbling block in this because i'm trying to pass the position of one axis to another. that seems crucial to doing this and i can't seem to come up with a workaround. also i get this error:

    Error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.localPosition'. Consider storing the value in a temporary variable (CS1612)

    which i guess is telling me the same thing! and also this:

    Error CS0664: Literal of type double cannot be implicitly converted to type `float'. Add suffix `f' to create a literal of this type (CS0664)

    does this error mean i need to do something like:

    Code (CSharp):
    1. LastPosition = lastPosition;
    or

    Code (CSharp):
    1. lastposition = lastPosition;
    or

    Code (CSharp):
    1. _lastposition = lastPosition;
    ?

    thanks so much!
     
  13. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    Use a Vector3 for lastPosition and when you need only 1 axis use something like lastPosition.y = transform.position.y.
    A Vector3 contains 3 float but you can't assign it to a float directly: you have to chose what of the 3 float you want.
    Also .01 is a double not a float so you get that error on double. Just use .01f.
     
  14. theRelation

    theRelation

    Joined:
    Aug 18, 2014
    Posts:
    33
    so when i did this:

    Code (CSharp):
    1. Vector3 lastPosition.y = transform.localPosition;
    2.         lastPosition.y = target.localPosition;
    3.         transform.localPosition = lastPosition;
    i get this error:

    Error CS1525: Unexpected symbol `.', expecting `,', `;', or `=' (CS1525)

    also, am i going crazy or am i just writing the same line of code over and over? how does this work? am i just stupid?
     
  15. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    OR you write: lastPosition.y = transform.localPosition.y ... // float = float OK!
    OR you write: lastPosition = transform.localPosition // Vector3 = Vector3 OK!
     
  16. theRelation

    theRelation

    Joined:
    Aug 18, 2014
    Posts:
    33
    i'm completely mystified by your last reply.

    notwithstanding, i tried:
    Code (CSharp):
    1.    
    2. usingUnityEngine;
    3. usingSystem.Collections;
    4.  
    5. publicclassmidAirController : MonoBehaviour
    6. {
    7. privatefloatlastPosition; //Thelastposition.
    8.  
    9.  
    10.  
    11. publicTransformtarget;
    12.  
    13. //InitializeinStart (orAwake):
    14.  
    15.  
    16.  
    17. voidStart()
    18.  {
    19. lastPosition.y = transform.position.y;
    20. lastPosition.y = target.localPosition.y;
    21. transform.localPosition.y = lastPosition.y;
    22.  
    23.  }
    24.  
    25. voidUpdate()
    26.  {
    27.  
    28.  
    29. if(target.position.y == target.position.y + .01f)
    30.  {
    31. transform.localPosition.z += .01f; //Movetotheup, orwhateveryouwant.
    32.  
    33.  }
    34. elseif(target.position.y == target.position.y - .01f)
    35.  
    36.  
    37.  
    38.  
    39.  
    40.  
    41.  {
    42. transform.localPosition.z -= .01f; //Movetothedown, orwhateveryouwant.
    43.  
    44.  
    45.  
    46.  
    47.  
    48.  
    49.  
    50.  }
    51.  }
    52.  
    53. }
    54.  
    i get: Error CS1061: Type `float' does not contain a definition for `y' and no extension method `y' of type `float' could be found (are you missing a using directive or an assembly reference?) (CS1061)

    and: Error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.localPosition'. Consider storing the value in a temporary variable (CS1612)

    with:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class midAirController : MonoBehaviour
    5. {
    6.     private float lastPosition;  // The last position.
    7.    
    8.    
    9.    
    10.     public Transform target;
    11.    
    12.     // Initialize in Start (or Awake):
    13.    
    14.    
    15.    
    16.     void Start()
    17.     {
    18.         Vector3 lastPosition = transform.position;
    19.          lastPosition = target.localPosition;
    20.          transform.localPosition = lastPosition;
    21.  
    22.     }
    23.    
    24.     void Update()
    25.     {
    26.        
    27.        
    28.         if(target.position.y == target.position.y + .01f)
    29.         {
    30.             transform.localPosition.z += .01f; // Move to the up, or whatever you want.
    31.            
    32.         }
    33.         else if(target.position.y == target.position.y - .01f)
    34.            
    35.            
    36.            
    37.            
    38.            
    39.            
    40.         {
    41.             transform.localPosition.z -= .01f; // Move to the down, or whatever you want.
    42.            
    43.            
    44.            
    45.            
    46.            
    47.            
    48.            
    49.         }
    50.     }
    51.  
    52. }
    i get this:

    Error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.localPosition'. Consider storing the value in a temporary variable (CS1612)

    my conclusion is that i get less errors with "Vector3 OK!" but is a struct incompatible with the increments i'm using?
     
  17. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    if you want to save in lastPosition only the Y value, you can declare float lastPosition and when you do the assignment you write lastPosition = target.localPosition.y (float = float OK!).
    Beware that in your Start() if you write Vector3 lastPosition you are declaring another local variable that hide the global one you defined before and that it's inaccessible out of Start(). So in your last example, if you want to use Vector3 you have to declare Vector3 lastPosition only at the beginning (instead of float).
    Anyway in all your code you make no use of localPosition so why bother? :)
     
  18. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Also note that none of your conditionals in Update will ever be true.
     
  19. theRelation

    theRelation

    Joined:
    Aug 18, 2014
    Posts:
    33
    Fraconte: i want to take the value or the position in Y and give it to X. this is the crux of what i'm trying to do. like if it's 50f on Y i need for it to be 50f on X. is this impossible? how should i express that. this is what i've been trying to get my game to do for the last month or so i've been writing on here.

    KelsoMRK, yes i don't doubt that it won't work because i don't really know what i'm doing. i was just told that i needed to put Vector3 somewhere and i looked at previous codes of mine that looked similar and it made sense to put that there. i have no idea what

    Code (CSharp):
    1. Vector3 lastPosition = transform.position;
    2.          lastPosition = target.localPosition;
    3.          transform.localPosition = lastPosition;
    really does, which is how i thought people code-- you just copy and paste and hope it works. i'd love a better explanation of what that code actually does so i don't waste everyone's time. reading the API is like talking to dungeon master on the old dungeons and dragons cartoon.
     
  20. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Then I'd strongly suggest doing some basic programming tutorials or picking up a book on C# and then going to the Learn section of this site and doing the tutorials there. If all you're doing is copy-pasting and hoping then you're just going to get frustrated and quit.
     
  21. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    This is what you want (I think)... but if you want to learn to code it's better you start from beginning script tutorials.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class midAirController : MonoBehaviour
    5. {
    6.     public Transform target;
    7.    
    8.     void Update()
    9.     {
    10.     // transform.position.z = target.position.y; // This line is all you want to do but you can't do that in c#
    11.                                             // So you have to do this:
    12.  
    13.         Vector3 position = transform.position; //assign the current position (x,y,z) to a variable
    14.         position.z = target.position.y;               // make the z of the variable equal to the y of the target position
    15.         transform.position = position;               // reassign the variable (x,y,z) to the current position
    16.     }
    17. }
    18.  
     
  22. theRelation

    theRelation

    Joined:
    Aug 18, 2014
    Posts:
    33
    Fraconte: i've tried something like that before and it doesn't seem to work for some reason. i just tried your code and it compiles just fine but it doesn't do what i would expect it to. the jump on y is visible in scene view but there is no related movement of the other object on the z axis in either scene or game view. no one seems to be able to tell me why and i don't know where to look for resources that explain why. are positioning values on separate axes incompatible data-wise?

    KelsoMRK: that was a joke.
     
    Last edited: Sep 5, 2014
  23. theRelation

    theRelation

    Joined:
    Aug 18, 2014
    Posts:
    33
    thanks guys, i figured it out.
    Code (CSharp):
    1. transform.position = new Vector3(0, 0, target.position.y);
     
    Fraconte likes this.