Jump to content
  • Advertisement
Timothy Sharp

[UNITY] Item bobbing

Recommended Posts

Advertisement
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!