- Home /
Move object forward then backwards
Hi, I've been trying to do this for hours and I just can't get it right, the sword is always going to the same direction, I want it to go forward (local space)on the x axis (in 2d) and then back off to the original position. I really don't know how to describe this problem any better, here's my code though:
using UnityEngine;
using System.Collections;
public class MeleeWeaponScript : MonoBehaviour {
public float rotationTime = 15f;
bool attacking = false;
Vector2 targetPosition;
public StabForwardVariables stabForwardVariables;
[System.Serializable]
public class StabForwardVariables {
public float forwardAmount;
public float velocity;
}
void Update () {
if (Input.GetButtonDown("Fire1") && (Vector2) transform.localPosition == Vector2.zero) {
attacking = true;
targetPosition = new Vector2(transform.localPosition.x + stabForwardVariables.forwardAmount , transform.localPosition.y);
}
}
void FixedUpdate () {
if (!attacking) {
transform.localPosition = Vector2.Lerp(transform.localPosition, Vector2.zero, stabForwardVariables.velocity * Time.deltaTime);
var h = Input.GetAxis("HorizontalRight");
var v = Input.GetAxis("VerticalRight");
if (Mathf.Abs(h) > 0.05 || Mathf.Abs(v) > 0.05) {
var angle = Mathf.Atan2(v, h) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.AngleAxis(angle, Vector3.forward), rotationTime * Time.deltaTime);
}
}
else {
transform.localPosition = Vector2.Lerp(transform.localPosition, targetPosition, stabForwardVariables.velocity * Time.deltaTime);
if ((Vector2) transform.localPosition == targetPosition)
attacking = false;
}
}
}
Thanks in advance.
So you are basically trying to make a stabbing animation?
Yup, the reason I don't want to use animations is because I'll have two attacks, and the second will probably be impossible to be done with animations
Answer by AndyMartin458 · Jun 13, 2014 at 05:54 PM
You are always setting attacking to false, so the if(!attacking) variable is going to be true immediately after you try to move the sword in a direction. You should have an extra boolean for determining when to move the sword backward.
using UnityEngine;
using System.Collections;
public class MeleeWeaponScript : MonoBehaviour {
public float rotationTime = 15f;
bool attacking = false;
Vector2 targetPosition;
bool movingBackward = false;
public StabForwardVariables stabForwardVariables;
[System.Serializable]
public class StabForwardVariables {
public float forwardAmount;
public float velocity;
}
void Update () {
if (Input.GetButtonDown("Fire1") && (Vector2) transform.localPosition == Vector2.zero) {
attacking = true;
movingBackward = false;
targetPosition = new Vector2(transform.localPosition.x + stabForwardVariables.forwardAmount , transform.localPosition.y);
}
}
void FixedUpdate () {
if (!attacking && movingBackward) {
transform.localPosition = Vector2.Lerp(transform.localPosition, Vector2.zero, stabForwardVariables.velocity * Time.deltaTime);
var h = Input.GetAxis("HorizontalRight");
var v = Input.GetAxis("VerticalRight");
if (Mathf.Abs(h) > 0.05 || Mathf.Abs(v) > 0.05) {
var angle = Mathf.Atan2(v, h) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.AngleAxis(angle, Vector3.forward), rotationTime * Time.deltaTime);
}
movingBackward = false;
}
else {
transform.localPosition = Vector2.Lerp(transform.localPosition, targetPosition, stabForwardVariables.velocity * Time.deltaTime);
if ((Vector2) transform.localPosition == targetPosition)
attacking = false;
movingBackward = true;
}
}
}