- Home /
Problem is not reproducible or outdated
How do i make a sword that pushes other objects when it hits, using animations.
I have a character with attack animations and i want the other objects to be affected by it when i swing the sword. I tried collision boxes and what not and it seems the collision box doesn't follow the sword during the animation, it just stays in place. So i guess im trying to find a way to apply the animation to the collision box.
Answer by tw1st3d · May 21, 2014 at 08:29 PM
So, this is what I came up with.
using UnityEngine;
using System.Collections;
public class RotateOnHit : MonoBehavior
{
private bool rotate = false;
private float triggered = 0.0f, currentTime, timer;
public void Start()
{
this.timer = 10.0f;
}
public void Update()
{
if(this.rotate) {
if(this.triggered == 0.0f) {
this.triggered = Time.deltaTime;
}
this.currentTime = Time.deltaTime;
if((this.currentTime - this.triggered) < timer) {
// Should perform a relative spin
transform.Rotate(Vector3.right * Time.deltaTime, Space.World);
} else {
this.rotate = false;
}
}
}
// This may not be the correct method name pretty sure it is though
public void OnCollisionEnter(Collider e)
{
if(e typeof GameObject) {
// I haven't worked with Unity in a while,
// So it may be a different object path
if(e.gameObject.tag == "sword") {
this.rotate = true;
}
}
}
}
sorry, i don't know C# very well, can you explain what it's suppose to do? also, i figured out that the collider doesn't move along because the sword itself doesn't change place, the mesh renderer is what makes it look like it does.. any alternative ideas to replicate the push of the sword?
Create an animation that moved the characters arm(or the sword), causing the sword to swing and hit the target. On contact, OnCollisionEnter() will fire. Perhaps create a velocity algorithm to create a faux jump system?
Also, what this script does, is listens for a collider hit, and then moves the object for 10 seconds by Time.deltaTime on the Vector3.Right plane.
the problem is that i already have an animation. i dont think i can apply an animation to the mesh collider itself unless the sword is moving with some kind of physics based animation
You are incorrect. Give the sword a thin box collider and listen for the event.