Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Ramlethal · Sep 04, 2019 at 03:22 PM · positionspawnscene-change

Companion spawns in default place instead of behind Player

I made the script which moves chosen companion to the place behind Player at the start of the Scene. It works well when I start the game, but when I change Scene (moving character in gameplay) to another and then go back, Player character is in right place, but companion spaws in default place (Inspector values). I have no idea why is this happening...

Companion is on the Scene already as normal GameObject (not Static) and as I said it works after I hit play, but not after changing Scene and coming back.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 
 public class KompanFollow : MonoBehaviour {
 
     System.Random losuj;
     private CharacterController character_Controller;
     public GameObject Player;
     public Animator ani;
     int timer = 0;
     bool destination = false;
     int IdleAni = 0;
     public NavMeshAgent agent;
     GameObject player;
     bool ustawienie = false;
     // Use this for initialization
 
     void Awake()
     {
         character_Controller = GetComponent<CharacterController>();
     }
 
 
     public void Start () {
 
         player = GameObject.Find("PlayerCharacter");
 
         if (player.GetComponent<player>().Ilu_Towarzyszy() == 0)
         {
             Debug.Log("No companion");
             this.gameObject.SetActive(false);
         }
         else if (this.gameObject.name == "Stefka" && player.GetComponent<player>().Kto_Towarzyszy() != 0)
         {
             Debug.Log("No Stefka (companion 0)");
             this.gameObject.SetActive(false);
         }
         else if (this.gameObject.name == "Saifer" && player.GetComponent<player>().Kto_Towarzyszy() != 1)
         {
             Debug.Log("No Saifer (companion 1)");
             this.gameObject.SetActive(false);
         }
 
         if (this.gameObject.activeSelf)
         {
             ani = GetComponent<Animator>();
             losuj = player.GetComponent<PowerOfCreation>().radek;
             IdleAni = losuj.Next(1, 22);
            
            //set companion behind Player
             gameObject.transform.position = new Vector3(player.GetComponent<PowerOfCreation>().positionplayera[0], player.GetComponent<PowerOfCreation>().positionplayera[3], player.GetComponent<PowerOfCreation>().positionplayera[1]);
             gameObject.transform.eulerAngles = new Vector3(gameObject.transform.eulerAngles.x, player.GetComponent<PowerOfCreation>().positionplayera[2], gameObject.transform.eulerAngles.z);
             gameObject.transform.position -= (2 * gameObject.transform.forward);
             
             //player.GetComponent<PowerOfCreation>().positionplayera[0/1/2] is changed on one Scene (before transition to another) and Player character is set in Start function acording to those 3 values, so Player charcater appears on new map in the right place (not same as Inspector position) (and it works!)
         }
     }
 
     // Update is called once per frame
     void Update()
     {
 
         
         
 
 
             if (timer <= 12)
                 timer++;
             else
             {
                 timer = 0;
             }
 
             if (((Player.transform.position) - transform.position).sqrMagnitude > 13)
             {
                 if (timer == 12)
                 {
                     agent.SetDestination(Player.transform.position);
                     destination = true;
                 }
 
                 if (destination)
                 {
                     agent.isStopped = false;
                     ani.Play("RUN");
                 }
 
             }
             else
             {
                 agent.ResetPath();
                 agent.isStopped = true;
                 destination = false;
                 ani.Play("IDLE" + IdleAni);
             }
 
 
         
     }
 }
 



Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Ramlethal · Sep 05, 2019 at 12:06 AM

Problem solved. NavMeshAgent was setting different position sometimes, I can't understand why tho. If someone would have similar problem, I can tell you just to disable agent and enable in script. Use something like GetComponent.enabled=false in Start() or Awake() function and get it back to true in Update() using some bool to make it call once only. Strange thing, but apparently it's solved.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

202 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Spawning limited GameObjects at a specific position not working 1 Answer

Enemy Spawner Help 0 Answers

Wrong position on spawn via script on every object (2.973684 instead of 3) 0 Answers

Position not correct Game 2D 0 Answers

how to set position of multiple images instantiate and set images in canvas verticallly 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges