Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Noob made mistake and needs help

Discussion in 'Scripting' started by Bernardbear, May 16, 2024.

  1. Bernardbear

    Bernardbear

    Joined:
    Apr 19, 2020
    Posts:
    31
    Good day, this script is attached to a baby that follows the player. Everything worked perfectly untill... I changed the pivot point of the baby in blender and re-exported it. I also change some rotation values.When I tested the game the baby wont follow the player anymore, but if I add this script to a new cube object it follows the player??
    What did I do wrong?




    Code (CSharp):
    1. using System.Collections.Specialized;
    2. using System.Security.Cryptography;
    3. using System.Threading;
    4. using UnityEngine;
    5.  
    6. public class WalkingScript : MonoBehaviour
    7.  
    8. {
    9.     private Transform target;
    10.     public float speed;
    11.  
    12.  
    13.    
    14.  
    15.  
    16.     void Start()
    17.     {
    18.         target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
    19.     }
    20.  
    21.  
    22.  
    23.         // Update is called once per frame
    24.         void Update()
    25.         {
    26.  
    27.             transform.position = Vector3.MoveTowards(transform.position, target.transform.position, speed * Time.deltaTime);
    28.             Vector3 direction = target.position - transform.position;
    29.             Quaternion rotation = Quaternion.LookRotation(direction);
    30.             transform.rotation = rotation;
    31.         }
    32.  
    33.  
    34.      
    35.     }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,777
    The issue isn't going to be with the script, but with the model. Since you changed the pivot, that will affect a lot of logic. You could fix the pivot in Blender and export again, or, you can use the tried and true Unity technique known as, "sticking it in a child object under the parent and using the parent to control everything". That way you can move the Blender model around freely and the logic of the script will work the way it wants to.
     
  3. Bernardbear

    Bernardbear

    Joined:
    Apr 19, 2020
    Posts:
    31
    Thank you for the reply. That is exactly what I did, this script is on a parent and the child is the object that a re-exported?
     
  4. Bernardbear

    Bernardbear

    Joined:
    Apr 19, 2020
    Posts:
    31
    Okay, I figured out something, by method of elimination. If I disable the gameobject Animator, the script works? How does that works??
     
  5. Bernardbear

    Bernardbear

    Joined:
    Apr 19, 2020
    Posts:
    31
    Okay I fixed it by redoing the animation.