Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Bug when i instantiate, my spawned object appears exactly at 1/5 in Y position of the spawn position

Discussion in 'Editor & General Support' started by westergard, Feb 3, 2023.

  1. westergard

    westergard

    Joined:
    May 4, 2015
    Posts:
    101
    Hi there,

    i have a small bug that i'm unable to solve, and it usually works but not this time.
    I have a spawnpoint. when i instantiate from that spawn point, the spawn appears exactly at 1/5 of the y position of the spawnpoint. For example, if the position of the spawnpoint is 0,15,0, my spawn object will appear at 0,3,0.

    Any idea why it will do this?

    Here is my code to instantiate (it's in the Update function)

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. [System.Serializable]
    5.  
    6. public class Frontier
    7. {
    8.     public float xMin, xMax, zMin, zMax;
    9. }
    10.  
    11. public class movement : MonoBehaviour
    12. {
    13.     public float shipspeed;
    14.     public float shipinclinaison;
    15.     public Frontier movementlimit;
    16.     Rigidbody tonrb;
    17.  
    18.     public GameObject lasers;
    19.     public Transform laserstart;
    20.     public Transform laserstart2;
    21.     public float shoottime;
    22.     public float shoottime2;
    23.     private float nextshoot;
    24.     private float nextshoot2;
    25.  
    26.     void Update()
    27.     {
    28.         if (Input.GetButton("Fire1") && Time.time > nextshoot)
    29.         {
    30.             nextshoot = Time.time + shoottime;
    31.             Instantiate(lasers, laserstart.position, laserstart.rotation);
    32.         }
    33.  
    34.         if (Input.GetButton("Fire2") && Time.time > nextshoot2)
    35.         {
    36.             nextshoot2 = Time.time + shoottime2;
    37.             Instantiate(lasers, laserstart2.position, laserstart2.rotation);
    38.         }
    39.     }
    40.  
    41.     void Start()
    42.     {
    43.         tonrb = GetComponent<Rigidbody>();
    44.     }
    45.  
    46.     void FixedUpdate()
    47.     {
    48.         float horizontalmovement = Input.GetAxis("Horizontal");
    49.         float verticalmovement = Input.GetAxis("Vertical");
    50.  
    51.         Vector3 globalmovement = new Vector3(horizontalmovement, 0.0f, verticalmovement);
    52.         tonrb.velocity = globalmovement * shipspeed;
    53.        
    54.         tonrb.position = new Vector3
    55.         (
    56.             Mathf.Clamp(tonrb.position.x, movementlimit.xMin, movementlimit.xMax),
    57.             0.0f,
    58.             Mathf.Clamp(tonrb.position.z, movementlimit.zMin, movementlimit.zMax)
    59.         );
    60.  
    61.         tonrb.rotation = Quaternion.Euler(0.0f, 0.0f, tonrb.velocity.x * -shipinclinaison);
    62.     }
    63. }
    64.  
    Thanks for your help

    Sebastien
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,647
    Do the lasers have anything that assign their own position?
     
  3. westergard

    westergard

    Joined:
    May 4, 2015
    Posts:
    101
    No nothing, only velocity forward