x


Script for Circular Motion always reset object position

I written up a script that allowed any one of my game objects to rotate in a circular motion however it always seems to reset the object to the center of the screen when I play the game and never to the position I started it from. I got no clue about how to go around this but I've been trying to look into it. If anyone can help me out thanks in advance.

using UnityEngine;
using System.Collections;

// Circular Motion Script
public class CircularMotionScript : MonoBehaviour
{
    public float mPosition = 0F;
    public float mRadius = 2.5F;

    void Update ()
    {
        // Move game object in a circular motion
        mPosition += .02F;
        transform.position = new Vector3(Mathf.Sin(mPosition)*mRadius, Mathf.Cos(mPosition)*mRadius, 0);
    }
}
more ▼

asked Jul 28 '12 at 02:17 AM

TakMarche gravatar image

TakMarche
12 6 9 14

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Well, what you describe is exactly what you do there. Sin() and Cos() returns a value between -1 and 1. You multiply this value by your radius so the value is in between -radius and radius. Then you use it as absolute position. This way you will always rotate around 0,0,0

What you need is an offset. This can be saved at start like this:

using UnityEngine;
using System.Collections;

public class CircularMotionScript : MonoBehaviour
{
    public float mPosition = 0F;
    public float mRadius = 2.5F;
    private Vector3 m_InitialPosition;

    void Start()
    {
        m_InitialPosition = transform.position;
    }

    void Update ()
    {
        mPosition += Time.deltaTime * 1.0f; // 1.0f is the rotation speed in radians per sec.
        Vector3 pos = new Vector3(Mathf.Sin(mPosition), Mathf.Cos(mPosition), 0);
        transform.position = m_InitialPosition + pos * mRadius;
    }
}
more ▼

answered Jul 28 '12 at 02:28 AM

Bunny83 gravatar image

Bunny83
45.4k 11 49 207

Ah I see it now. Thanks for the explanation!

Jul 28 '12 at 04:29 AM TakMarche

I found myself stuck once again. What if my initial position is always changing (from another script) once the object is destroyed?

Jul 30 '12 at 02:51 AM TakMarche

I don't get what you mean. When the object is destroyed, it's no longer there...

When you instantiate a new object with that script, it will set it's initial position the first frame after it has been instantiated.

You can make m_InitialPosition a public variable so you can access it from another script. The other script can set the center point to what ever you want.

Jul 30 '12 at 10:15 AM Bunny83

Thanks for the explanation!I was finally able to figure it out.

Aug 01 '12 at 10:45 PM TakMarche
(comments are locked)
10|3000 characters needed characters left

You don't need to do all of that.. just make the object you want to move circularly a child of a stick (cube game object) , put your object at the tip of the stick... then attach a script to the stick to make it rotate function Update(){ transform.Rotate(0,0,Time.deltaTime); } make sure not to render the stick in camera..

the stick length is the radius of the circle.......

more ▼

answered Sep 04 '12 at 05:57 AM

daytripper1989 gravatar image

daytripper1989
1 1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4162
x97

asked: Jul 28 '12 at 02:17 AM

Seen: 563 times

Last Updated: Sep 04 '12 at 05:57 AM