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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Trying to simply divide a value from a vector

Discussion in 'Scripting' started by Voodin, Sep 24, 2018.

  1. Voodin

    Voodin

    Joined:
    Apr 19, 2018
    Posts:
    48
    I don't have internet right now so bear with me. I was reading a Reddit post by someone who replicated source engines 3d skyboxes. They said they "simply divided camera movement by a scale factor" to get a second camera to move and project what it sees. However when I try to do this I keep getting "operator / cannot be applied to operands of type 'void' and 'float'

    My code looks like this:


    Code (CSharp):
    1. Public Gameobject targetcamera;
    2. Public float ratio;
    3.  
    4. Void update(){
    5. Float a = targetcamera.transform.translate(vector3.left) / ratio;
    People keep saying that the vector3 returns floats, but that isn't enough info to help me
     
  2. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,772
    targetcamera.transform.translate(vector3.left) moves the transform by the given vector. it does not return a vector hence the 'void' in the error message.
     
  3. Voodin

    Voodin

    Joined:
    Apr 19, 2018
    Posts:
    48
    So how could I make one to one movement? I tried using transform.position instead but now my object doesn't stop moving
     
  4. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    899
    Create a temporary vector and do you maths in that...

    Code (CSharp):
    1.     public Gameobject targetcamera;
    2.     public float ratio;
    3.     Vector3 displacement;
    4.    
    5.     void Update(){
    6.         displacement = Vector3.left;
    7.         displacement.x /= ratio;
    8.         displacement.y /= ratio;
    9.         displacement.z /= ratio;
    10.  
    11.         targetcamera.transform.translate(displacement);
    12.     }
    13.  
    Although if you're just moving your camera left always, you can create a new vector to move by scaled by ratio.

    Code (CSharp):
    1.     public Gameobject targetcamera;
    2.     public float ratio;
    3.     Vector3 displacement;
    4.      
    5.     void Start(){
    6.         displacement = new Vector3(-1/ratio, 0, 0);
    7.     }
    8.     void Update(){
    9.         targetcamera.transform.translate(displacement);
    10.     }
    11.  
     
    Voodin likes this.
  5. Voodin

    Voodin

    Joined:
    Apr 19, 2018
    Posts:
    48
    thanks, ill try it out