- Home /
Animating a weapon into/out of player view
What I want to do is basically animate the weapon from the bottom and slide it up like he pulled it out of his sling or something. But I have never really done animation before. All I have is the actual weapon model and I attached it to the FPS player main camera. Can someone help guide me in the correct direction :] Please and thank you!
Please either accept an appropriate answer, let us know if this issue has or has not been resolved, or close your quesion.
Answer by clunk47 · May 27, 2013 at 09:38 PM
You could make the animation in your 3D program(Blender, Maya, etc...), and code it to Play the animations whenever... OR you could use a simple script in JS or C# using Vector3.MoveTowards like so. This is a simple C# EXAMPLE.
using UnityEngine;
using System.Collections;
public class EXAMPLE : MonoBehaviour
{
Vector3 showPos;
Vector3 hidePos;
bool hide = false;
void Start()
{
showPos = transform.position;
hidePos = showPos + Vector3.down;
}
void Update()
{
if(hide)
{
transform.position = Vector3.MoveTowards(transform.position, hidePos, 1.0f);
}
else if(!hide)
{
transform.position = Vector3.MoveTowards(transform.position, showPos, 1.0f);
}
}
}
This is just an example to get you pointed in the right direction.
GetComponent<EXA$$anonymous$$PLE>().hide = true;
GetComponent Reference.
Thank you :] Now its just the matter of making the hand/item follow the player and move it up and down ^_^
Just make the player the parent of your "hand object". $$anonymous$$ake the hand object the parent of your weapon. Then you'd want to attach your movement script the the hand object, not the weapon. Since the weapon is a child of the hand object, it will move where then hand object moves. Since the hand object is child of your player, it will do the same in respect to the player. Parent Ref
If you want to do a "bob" up and down when walking, you could use the same principle as above. But since it's continuous you would want to check when the object meets a certain position, so it can move back the other direction. Here's an example of that:
Vector3 startPos;
Vector3 stopPos;
Vector3 targetPos;
bool isWalking = true;
void Start()
{
startPos = transform.position;
stopPos = startPos + Vector3.down;
targetPos = stopPos;
}
void Update()
{
if(isWalking)
{
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, targetPos, 1.0f);
}
if(transform.position == stopPos)
{
targetPos = startPos;
}
if(transform.position == startPos)
{
targetPos = stopPos;
}
}
I don't see how you could be getting a nullref ex in this specific code. Everything is defined. Post the FULL error. It will tell us what script and what lines of the script are throwing the error.
Your answer
Follow this Question
Related Questions
Animating different Weapons 2 Answers
How to reset gameObject position when switched back to? 0 Answers
Best weapon system for multiplayer FPS? 0 Answers
Vector3.Lerp and Animations 0 Answers
How to create a stationary weapon FPS controller ? 2 Answers