0

So im making this Unity game where i have a gameobject that im moving and rotation during the game, the thing is the pivot point doesnt stick to the object. It rotates just fine on the spot when the game begins but when it has been moved the the pivotpoint didnt follow and its still rotation around its start location. Im using an "extra" gameobject to get the pivotpoint in the right spot in the first place, this extra object is attached to the first object tho so it SHOULD follow it around.

Any ideas what could be the cause/solution to this? Im pulling out my hair here been moving stuff around all night >.<

Here is the very basic code for the rotating, doubt thats it though.

public float speed = 1f; 

// Use this for initialization
void Start () 
{

}

// Update is called once per frame
void Update () 
{

    if (Input.GetKey(KeyCode.W))                                                // Gå fremad    
    {
        transform.position -= new Vector3 (speed * Time.deltaTime, 0.0f, 0.0f);  
    }

    if (Input.GetKey(KeyCode.S))                                                // Gå baglæns       
    {
        transform.position += new Vector3 (speed * Time.deltaTime, 0.0f, 0.0f); 
    }

    if (Input.GetKey(KeyCode.Q))                                                // Strafe mod venstre
    {
        transform.position -= new Vector3 (0.0f, 0.0f, speed * Time.deltaTime);  
    }

    if (Input.GetKey(KeyCode.E))                                                // Strafe mod højre
    {
        transform.position += new Vector3 (0.0f, 0.0f, speed * Time.deltaTime);  
    }

    if (Input.GetKey(KeyCode.D))                                                // Drej mod venstre     
    {
        transform.RotateAround (Vector3.zero, Vector3.up, 20 * Time.deltaTime);
    }

    if (Input.GetKey(KeyCode.A))                                                // Drej mod højre
    {
        transform.RotateAround (Vector3.zero, Vector3.up, -20 * Time.deltaTime);
    }
Steven
  • 166,672
  • 24
  • 332
  • 435
  • is the pivot point he child or the parent object ? – Uri Popov Jun 01 '15 at 14:13
  • The pivot point is the parent, the models & colliders for the object is children, should i have done it differently? –  Jun 01 '15 at 16:13
  • if you are a moving a object in unity that has a parent the parent will not move. Children follow the transform of the parent not the other way around. Try making the pivot point to be child and try to move the parent. Then the pivot point WILL move with the object. – Uri Popov Jun 01 '15 at 17:31
  • THe script is on the parent(the pivot point), the child(the model) is moving just fine, it just doesnt make sense to me. But ill try ur suggenstion to see if it works! –  Jun 01 '15 at 18:11
  • Thinking about it, how do i make the child the pivot point? Isnt the parent allways the pivot point of all the children objects? –  Jun 01 '15 at 18:23
  • Don't move the child at first place move the parent – Milad Qasemi Jun 02 '15 at 03:33
  • Im not sure what you mean by that, my script is on the parent, so i am moving the parent around –  Jun 02 '15 at 03:54
  • Nvm i got it to work changing the script a bit! Still appreciate your input friends^^ Kudos! –  Jun 02 '15 at 04:12

1 Answers1

0

don't use:

transform.RotateAround(Vector3.zero, Vector3.up, 20 * Time.deltaTime);

if you use this, you will always rotate around vector zero - that is (0,0,0) - in the WORLD coordinate system (not LOCAL).

if you want to rotate your object around its own center use:

transform.Rotate( xAngle, yAngle, zAngle, relativeTo );

if you want to rotate your object relative to another object (i don't think you want that), but anyway:

transform.RotateAround(otherGameObject.transform.position, Vector3.up, 20 * Time.deltaTime);
Jinjinov
  • 2,554
  • 4
  • 26
  • 45