# [UNITY] Item bobbing

## Recommended Posts

I would like to know how I can make held items bob while the player is walking/jumping. I am using unity and c#. Thanks!

##### Share on other sites
Posted (edited)
41 minutes ago, Timothy Sharp said:

bob while the player is walking/jumping

If you just want it stuck on the hand you can parrent it to a bone.
If you want an actual laging kind of bob to emulate "follow through" and "overlapping" you can add a extra bone ot the rig that does this.

It can also be done by code. Using linear algebra you can find the offset, then you know the point your object has to move to. From there you can lerp the space between the last position and the position it has to be.

Making games requires at least Grade 12 linear algebra. Vector math is most of what you will be using. (edit: I realize this reads incorrectly, I mean you will learn the math as you make games. I am not good with words.)

Code:

Spoiler


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FollowTarget : MonoBehaviour {
//This Mono class allows a unity game object to follow a other
public GameObject Target;
public float Delay = 1f;//Time in +/- seconds

Vector3 StartOffset;

void MoveTowardsTarget(){

this.transform.position = Vector3.Lerp (this.transform.position,
this.transform.position = Target.transform.position + StartOffset, Time.deltaTime/Delay);
}

// Use this for initialization
void Start () {
//When the game starts it is decided how far the object will be from the other object
StartOffset = this.transform.position - Target.transform.position;
}

// Update is called once per frame
void Update () {
MoveTowardsTarget ();
}
}

As you can see this is a general utility type code. Can be used for any kind of object tracking. I like to use it for my camera. By using the lag you can easily get "overlapping" animations.

If you want me to explain what I did here I will be happy to. This math is used for everything from moving to calculating complex paths and looking at other objects.

Edited by Scouting Ninja

##### Share on other sites
2 hours ago, Scouting Ninja said:

If you just want it stuck on the hand you can parrent it to a bone.
If you want an actual laging kind of bob to emulate "follow through" and "overlapping" you can add a extra bone ot the rig that does this.

It can also be done by code. Using linear algebra you can find the offset, then you know the point your object has to move to. From there you can lerp the space between the last position and the position it has to be.

Making games requires at least Grade 12 linear algebra. Vector math is most of what you will be using. (edit: I realize this reads incorrectly, I mean you will learn the math as you make games. I am not good with words.)

Code:

Hide contents



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FollowTarget : MonoBehaviour {
//This Mono class allows a unity game object to follow a other
public GameObject Target;
public float Delay = 1f;//Time in +/- seconds

Vector3 StartOffset;

void MoveTowardsTarget(){

this.transform.position = Vector3.Lerp (this.transform.position,
this.transform.position = Target.transform.position + StartOffset, Time.deltaTime/Delay);
}

// Use this for initialization
void Start () {
//When the game starts it is decided how far the object will be from the other object
StartOffset = this.transform.position - Target.transform.position;
}

// Update is called once per frame
void Update () {
MoveTowardsTarget ();
}
}

As you can see this is a general utility type code. Can be used for any kind of object tracking. I like to use it for my camera. By using the lag you can easily get "overlapping" animations.

If you want me to explain what I did here I will be happy to. This math is used for everything from moving to calculating complex paths and looking at other objects.

Please explain and I can not parent it to a bone because i'm using a fps controller

##### Share on other sites
9 hours ago, Timothy Sharp said:

I can not parent it to a bone because i'm using a fps controller

So you don't have arms?

If you don't have arms then just make a empty GameObject then animate that using the Unity animator. Once you have a animation you can parent all the weapons to the empty object.

If you do have arms:

Here is a good tutorial on parenting to a bone: http://blog.huthaifa-abd.com/gamedevelopment/attach-weapon-to-character.html But all you do is find the bone and parent it, very simple stuff.

Can you show me with a screenshot what you are trying to do, I will be of more help.

## Create an account

Register a new account

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 12
• 14
• 9
• 9
• ### Forum Statistics

• Total Topics
633346
• Total Posts
3011451
• ### Who's Online (See full list)

There are no registered users currently online

×