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

Question GameObject moving in orbit and perpendicular to Camera rather than in a Cartesian manner?

Discussion in 'Scripting' started by scoopz, Jun 11, 2023.

  1. scoopz

    scoopz

    Joined:
    Feb 24, 2022
    Posts:
    4
    So I've cobbled this script together:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Actor_BaseScript : MonoBehaviour
    6. {
    7.  
    8.   // Things all actors have
    9.   private Vector3 moveDirection = Vector3.zero;
    10.  
    11.   public Transform camera_target;
    12.   public int health;
    13.   public int fatigue;
    14.   public float speed;
    15.   public int weapon;
    16.  
    17.   // Start is called before the first frame update
    18.   void Start()
    19.   {
    20.  
    21.   }
    22.  
    23.   // Update is called once per frame
    24.   void Update()
    25.   {
    26.  
    27.     // Movement
    28.     moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    29.     //moveDirection = transform.TransformDirection(moveDirection);
    30.     transform.Translate(moveDirection * speed * Time.deltaTime);
    31.  
    32.   }
    33.  
    34.   void LateUpdate()
    35.   {
    36.  
    37.     // Billboarding
    38.     transform.LookAt(camera_target.transform.position, Vector3.up);
    39.  
    40.     // Modifiy rotation in Euler space to lock certain dimensions
    41.     Vector3 rotation = transform.rotation.eulerAngles;
    42.     rotation.x = 0;
    43.     rotation.z = 0;
    44.     transform.rotation = Quaternion.Euler(rotation);
    45.  
    46.   }
    47. }
    Basically, I want to do two things with this.
    1.) Move the GameObject (called actor in this case) up and down and left and right on a grid. Along the x/z or "flat" plane. Which it currently is not doing.
    2.) Make sure that it stays billboarded towards the camera. Which it is doing.

    So while the actor sprite is looking to the camera and then getting it's x and z rotations reset, when I try to move the axes are somehow aligned perpendicularly around the camera. I.E. moving left or right results in an orbital movement, and moving forward or back results in getting closer or further away from the camera.

    Note: there is another script that locks the camera position to the player's and forces it to look at the player. Or GameObject rather, but this camera will only ever look at the GameObject the player is directly controlling. Found here:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerCamera : MonoBehaviour
    6. {
    7.  
    8.     public Transform player;
    9.     public Vector3 offset;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.        
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.  
    21.         //get the players position and add it with offset, then store it to transform.position aka the cameras position
    22.         transform.position = player.position + offset;
    23.         //keeps target, i.e. the player, focused
    24.         transform.LookAt(player);
    25.     }
    26. }
    I don't think this one is the problem, but I really don't know enough to say.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,561
    Any reason you're doing all this coding rather than just making a looping animation and being done with it??
     
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,495
    Transform.Translate has two arguments. First the vector you want to translate by and second the coordinate space it should move in. By default it would use "Space.Self" which is local space. Since you said you want to move along the x-z-plane it's not clear what that plane should be relevant to. However if you meant the world x-z- plane, you should use Space.World in your Translate call or not using Translate at all and just add your movement to the world position (transform.position) which as the same effect.
     
    orionsyndrome likes this.
  4. scoopz

    scoopz

    Joined:
    Feb 24, 2022
    Posts:
    4
    hope I got the gist of the question correctly: I'm not just doing an animation because this script is for player input so it needs to be dynamic.

    Ah thank you, I was wondering if it had something to do with local vs world spaces. I appreciate the answer! I'll update when I test things out.

    EDIT: Thanks for the assistance. This + some more debugging straightened things out! Thank you
     
    Last edited: Jun 11, 2023
    Bunny83 likes this.