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

How can i display the object on/off in the same distance from player when pressing on F ?

Discussion in 'Scripting' started by Chocolade, Dec 22, 2017.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    916
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class DroidPosition : MonoBehaviour
    7. {
    8.     public GameObject droid;
    9.  
    10.     private float distance;
    11.  
    12.     private void Start()
    13.     {
    14.         distance = Vector3.Distance(transform.position, droid.transform.position);
    15.         droid.SetActive(false);
    16.     }
    17.  
    18.     private void Update()
    19.     {
    20.         if (Input.GetKeyDown(KeyCode.F))
    21.         {
    22.             droid.transform.position = new Vector3(transform.position.x - distance, transform.position.y, transform.position.z);
    23.             droid.SetActive(!droid.activeInHierarchy);
    24.         }
    25.     }
    26. }
    27.  
    I want that each time i display the Droid by pressing on F it will be at the same distance from the player like in the Start.

    transform is the player (FirstPersoncharacter with a Camera).

    But when i press F the droid is nit in front of the player like in the Start.
    This is where the droid position at before running the game.



    When running the game and pressing F the droid is not in front of the player but seems like on the player or somewhere else:

     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, when you set it, you are subtracting the distance from x.
    Is distance in front of you all that you care about? That would be +z I would imagine.
    If you want the exact offset, that would be a Vector3. It sounds more like you just want +z if it's always straight in front of you. Something like transform.forward * distance. I assume you could have the droid keep the same y and x as the player. Unless it's up higher or to the side....
     
    Chocolade likes this.
  3. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    916

    When i'm doing:

    Code (csharp):
    1.  
    2. droid.transform.position = transform.forward * distance;
    3.  
    I don't see the droid at all.

    If i'm doing:

    Code (csharp):
    1.  
    2. droid.transform.position = Vector3.MoveTowards(transform.position, droid.transform.position, distance);
    3.  
    The droid is in the right height but it's all the time display in it's original position and not the position from the player camera.

    The player is FirstPersonCharacter and i want that if i moved the player camera not the player position only camera to some direction and pressing F display the droid on this camera direction it's pointing and with the same distance original distance.

    And then when the droid is displaying and i move the player camera around the droid should be following the player camera(FirstPersonCharacter camera). Like in the first screenshot no matter what direction the player is facing display the droid in this direction with the same distance and when moving around the player camera move also the droid with it.
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    Can you make the droid a child of the camera or the player?
     
    Chocolade likes this.
  5. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    916
    Right this is working i made the droid child of the camera.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, totally cool.. :) lol
    While that easy solution came up, I was off making this... didn't polish but will post .. b/c :)
    Code (csharp):
    1.  
    2. public class DroidTest : MonoBehaviour {
    3.  
    4.     [SerializeField]
    5.     Transform droid;
    6.     [SerializeField]
    7.     float distance;
    8.     [SerializeField]
    9.     bool droidActive = false;
    10.     [SerializeField]
    11.     Vector3 xyOffset;
    12.     private void Start()
    13.     {
    14.         Vector3 tmp = new Vector3(droid.position.x, droid.position.y, transform.position.z);
    15.         xyOffset = droid.position - transform.position;
    16.         xyOffset.z = 0;
    17.         distance = Vector3.Distance(droid.position, tmp);
    18.         print("Distance = " + distance);
    19.     }
    20.     private void Update()
    21.     {
    22.         if (Input.GetKeyDown(KeyCode.F))
    23.         {
    24.             droid.gameObject.SetActive(!droid.gameObject.activeInHierarchy);
    25.             if (droid.gameObject.activeInHierarchy)
    26.             {
    27.                 droidActive = true;
    28.             }
    29.             else droidActive = false;
    30.         }
    31.         // testing updates without spam! :)
    32.         if(Input.GetKeyDown(KeyCode.N))
    33.         {
    34.             if (droidActive)
    35.             {
    36.                 Vector3 newpos = new Vector3(xyOffset.x, xyOffset.y, transform.position.z);
    37.                 newpos += transform.forward * distance;
    38.                 print("new pos = " + newpos);
    39.                 droid.position = newpos;
    40.             }
    41.         }
    42.     }
    43. }
    44.  
     
    LiterallyJeff and Chocolade like this.
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Regardless, glad it's working for ya ;) lol
     
  8. keith_p1

    keith_p1

    Joined:
    Aug 18, 2017
    Posts:
    2
    Code (csharp):
    1. droid.transform.position = transform.forward * distance;
    should be
    Code (csharp):
    1. droid.transform.position = transform.position + transform.forward * distance;
    transform.forward is a direction, so you're only moving the droid around the origin as the player spins about.
     
    Chocolade likes this.