Search Unity

axis of another object?

Discussion in 'Scripting' started by karsten-heim, Jul 14, 2022.

  1. karsten-heim

    karsten-heim

    Joined:
    May 16, 2022
    Posts:
    63
    sorry to post so many questions in the forums, but how exactly do you get the vertical and horizontal axes of another object in a script? I tried this, but it does not work. I am currently attempting to make a shooter game where the camera rotates and moves along the axis the player is on, not the axis that the camera is on. for this, i must make a script where the position of the camera is dictated by the player object, so I must get the axes of the object "Cylinder_"
    I cannot find what I need in the unity scripting guide thingy and a few other places, so some insight on how to do this would be much appreciated. here is around what i need for this piece of code to do (all syntax aside).

    Code (CSharp):
    1.  float XMove = Cylinder_.Input.GetAxis("Horizontal") * speed;//  <---- something to reference "Cylinder_" to get it's Axes
    2.   float Zmove = Cylinder_.Input.GetAxis("Vertical") * speed;// <---- something to reference "Cylinder_" to get the Axes
    im sure somebody can help me with the exact syntax of this. also thanks for helping me with my formatting of my question @Kurt-Dekker
     
    Last edited: Jul 14, 2022
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    I dunno what the above is... Input is typically a static class.

    You can access another GameObject's Transform directly.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    You may edit your post above.
     
    Bunny83 likes this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,990
    Like Kurt said, input has actually no relation to any objects in your scene. Input has a relation to the real world. Specifically your mouse or keyboard. The horizontal input axis is a virtual "axis" that represents the "A" and "D" keys on your keyboard or maybe a mapping of some other input device like a game controller. When you press "D"
    Input.GetAxis("Horizontal")
    returns a value of
    1
    . When you press "A" it returns
    -1
    . If none of the two is pressed it returns
    0
    . That's all.

    It seems you now want to map this input axis to some actual object in your scene. You can do this like this:

    Code (CSharp):
    1. Vector3 movement = Cylinder_.transform.right * Input.GetAxis("Horizontal") * speed + Cylinder_.transform.forward *Input.GetAxis("Vertical") * speed;
    An alternative and a bit more readable version is this

    Code (CSharp):
    1. Vector3 localMovement = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
    2. Vector3 worldMovement = Cylinder_.transform.TransformDirection(localMovement) * speed;
     
  4. karsten-heim

    karsten-heim

    Joined:
    May 16, 2022
    Posts:
    63
    for some reason I cannot get this to work, so I'm just going hope one of you can check if I understand this correctly and If I am doing this right.
    Code (CSharp):
    1.  Vector3 localMovement = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical")); //.   <---- Gives the keybinds for horizontal and vertical movement along the x and z axes
    2.         Vector3 worldMovement = Cylinder_.transform.TransformDirection(localMovement) * speed; //   <----- Transforms the direction of the movement so that the movement should correspond with the rotation of of the cylinder, not the camera.
    3.         Vector3 movement = new Vector3(worldMovement.x/4, 0, worldMovement.z/4);//    <----- assigns it to a new variable, and decreases speed (probably not necessary).
    4. transform.Translate(movement.x,0,movement.z); //. <----- actually moves the object
    I have little prior experience with coding, but I would like to be able to solve these kinds of problems better, so knowing if I understand this correctly would be nice. also my comments are on the script if you scroll to the right
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Excellent! Let me advise you to start breaking your code into many vertical lines, each line doing one tiny thing.

    If you have more than one or two dots (.) in a single statement, you're just being mean to yourself.

    How to break down hairy lines of code:

    http://plbm.com/?p=248

    Break it up, practice social distancing in your code, one thing per line please.

    "Programming is hard enough without making it harder for ourselves." - angrypenguin on Unity3D forums

    "Combining a bunch of stuff into one line always feels satisfying, but it's always a PITA to debug." - Star Manta on the Unity3D forums

    After that, put your debugging hat on and get to work:

    You must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494