- Home /
(URGENT) I have a swinging axe I can get to rotate once in 1 direction but i need it to loop
I put urgent in the title because I have a science fair project being presented on Saturday and I have to get this mechanic implemented by the end of Friday or else I have to leave it out.
So back to the swinging axe. I am really new with working with quaternions and I have a modified version of a script I found on unity awnsers that I am trying to use to get my axe to swing in a 180 degree arc in a loop. As I said in the title I have it rotating in 1 direction but I need it to rotate back and loop.
using UnityEngine;
using System.Collections;
public class physics : MonoBehaviour
{
Vector3 Swing1Condition = new Vector3(0, 0, 499);
public Vector3 StartingPosition;
public Vector3 targetAngle = new Vector3(0f, 0f, 90f);
public float speed;
void Start()
{
StartingPosition = transform.eulerAngles;
Swing1();
}
// Update is called once per frame
void Swing1()
{
if (StartingPosition.z <= Swing1Condition.z)
{
StartingPosition = new Vector3(
Mathf.LerpAngle(StartingPosition.x, targetAngle.x, Time.deltaTime * speed),
Mathf.LerpAngle(StartingPosition.y, targetAngle.y, Time.deltaTime * speed),
Mathf.LerpAngle(StartingPosition.z, targetAngle.z, Time.deltaTime * speed));
transform.eulerAngles = StartingPosition;
}
}
}
By loop you mean what? Back and forth? Or A -> B, A -> B
you should only need to change on axis if that's all you need. you don't really need lerps and things either unless you're trying to smooth the movement on purpose?
just do
transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, transform.eulerAngles.z + (Time.deltaTime * speed));
That won't look back and fourth though. I am trying to get my aex to swing in a 180 degree arc
Couldn't you just animate it and make it ping-pong? Or does it have to be coded?
Answer by Bill9009 · Feb 03, 2017 at 07:51 AM
When you first used my script I forgot to set an amount to resetRate. Any way good luck.
public class physics : MonoBehaviour {
Vector3 Swing1Condition = new Vector3(0, 0, 499);
public Vector3 StartingPosition;
private int reset = 1;
public float resetRate;
//This is the time before the float reset is changed to -1
public Vector3 targetAngle = new Vector3(0f, 0f, 90f);
public float speed;
private void Start()
{
InvokeRepeating("restart", 0f, resetRate);
}
//InvokeRepeating constantly actives a method at a given rate of time(resetRate)
private void Update()
{
StartingPosition = transform.eulerAngles;
Swing1();
}
void Swing1()
{
if (StartingPosition.z <= Swing1Condition.z)
{
StartingPosition = new Vector3(
Mathf.LerpAngle(StartingPosition.x, targetAngle.x, Time.deltaTime * speed),
Mathf.LerpAngle(StartingPosition.y, targetAngle.y, Time.deltaTime * speed),
Mathf.LerpAngle(StartingPosition.z, targetAngle.z * reset, Time.deltaTime * speed));
//When I multiply targetAngle.z by -1 it becomes -90
//This makes the axe go backward
transform.eulerAngles = StartingPosition;
}
}
void restart()
{
reset = reset * -1;
}
//I activate this using InvokeRepeating which constantly changes the axe swing
}
I tried your script and the axe just stays in place
Huzzah! I guess I can include my swinging axe in my game in-time for the science fair <3 . Could you do me a favor though and explain to me how the script works? you can tell me in the comments or put comments in your script :P
Answer by goutham12 · Feb 03, 2017 at 07:51 AM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class test : MonoBehaviour {
Vector3 StartingPosition,targetAngle;
float speed = 5;
void Start(){
StartingPosition = transform.eulerAngles;
targetAngle = new Vector3 (0, 0, 90);
StartCoroutine (Swing1 ());
}
IEnumerator Swing1()
{
while (true)
{
float moveProgress = 0;
Vector3 target = Vector3.zero;
while(moveProgress < 1)
{
moveProgress += Time.deltaTime*speed;
transform.eulerAngles = Vector3.Lerp(StartingPosition,targetAngle,moveProgress);
yield return null;
}
targetAngle = StartingPosition;
StartingPosition = transform.eulerAngles;
yield return null;
}
}
}
Answer by malkere · Feb 03, 2017 at 09:04 AM
float speed = 5f;
bool goingForward = true;
void Update () {
if (goingForward) {
Vector3 r = transform.eulerAngles;
transform.eulerAngles = new Vector3(r.x, r.y, r.z + (Time.deltaTime * speed));
if (transform.eulerAngles.z >= 90) {
goingForward = false;
}
}
else {
Vector3 r = transform.eulerAngles;
transform.eulerAngles = new Vector3(r.x, r.y, r.z - (Time.deltaTime * speed));
if (transform.eulerAngles.z <= 0) {
goingForward = true;
}
}
}
plain mechanics to expand off of
tried a variation of your script and the axe spins in 360s ins$$anonymous$$d of looping back and fourth in a 180 degree arc
forgot to mention your script and how i modified it
using UnityEngine;
using System.Collections;
public class physics : $$anonymous$$onoBehaviour
{
public Vector3 StartingPosition;
public Vector3 targetAngle = new Vector3(0f, 0f, 90f);
public float speed;
bool goingForward = true;
void Start()
{
StartingPosition = transform.eulerAngles;
}
// Update is called once per frame
void Update()
{
if(goingForward)
{
Vector3 r = transform.eulerAngles;
transform.eulerAngles = new Vector3(r.x, r.y, r.z + (Time.deltaTime * speed));
if (transform.eulerAngles.z >= -90)
{
goingForward = true;
}
else
{
Vector3 v = transform.eulerAngles;
transform.eulerAngles = new Vector3(v.x, v.y, v.z - (Time.deltaTime * speed));
if (transform.eulerAngles.z <= 90)
{
goingForward = false;
}
}
}
}
}
Sometime the way unity handles the floats of rotations is unusual, quite a lot of people post problems about it. To be sure I always add if (rotation >= 360) rotation -= 360; else if (rotation < 0) rotation += 360;
that way it's always within 0-360. Hard to gaurentee otherwise
the way you rewrote the code however nestles the second if inside the first. So you ALWAYS are adding to rotation. after that the if statements are being called. I seperated everything into one if and one else
Your answer
Follow this Question
Related Questions
How would I make a ListChangedEvent? 1 Answer
Unity 3D: How to make the gameobject speed increase continuously on tapping quickly? 1 Answer
Spawning 3D Objects according to Data at JSON Matrix 0 Answers
How to move the object to where the object is already pointing to? 1 Answer
"Follow Target" script with a prefab (C#) Help me please! 1 Answer