x


gun problems

how can i make an object move from one point to another... like a bullet in a gun

more ▼

asked Jul 30 '12 at 08:33 AM

Alix98 gravatar image

Alix98
-35 1 4

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

1 answer: sort voted first

There are couple of ways to approach this. If you simply want to fire a bullet then the easiest would be to do the following.

void Update()
{
    Vector3 directionOfFire = Vector3.forward;
    float bulletSpeed = 10f;
    gameObject.transform.Translate(directionOfFire * Time.deltaTime * bulletSpeed);
}

But if you want to move a bullet from one fixed point to another (which is not how it normally happens, but still might be a requirement in your game) the following might help you.

 private float m_DeltaTime = 0f;
 private Vector3   m_StartPos;
 private Vector3   m_EndPos;

 void Start()
 {
    m_StartPos = Vector3.zero;
    m_EndPos = Vector3.left * 100f;
 }

 void Update()
 {
    if(m_DeltaTime < 1f)
    {
       m_DeltaTime += Time.deltaTime;
       gameObject.transform.position = Vector3.Lerp(m_StartPos, m_EndPos, m_DeltaTime);
    }
 }
more ▼

answered Aug 06 '12 at 12:44 PM

hdsenevi gravatar image

hdsenevi
314 1 3 6

(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:

x496
x445
x305

asked: Jul 30 '12 at 08:33 AM

Seen: 243 times

Last Updated: Aug 06 '12 at 12:45 PM