- Home /
Flip an object by 180° (C#)
Hi, I'm very new to Unity and I didn't find a working answer to a 180 degrees rotation problem I have.
I refer to a part of game similar to a function in transport tycoon, simutrans etc,
I need to have a behaviour like this: the object starts from point A, goes to point B, then to point C and goes there and back again between B and C in infinite loop - a vehicle that exits from its depot, goes to the load point, then to the delivery point, then the load point and so on.
This is my code:
using UnityEngine;
using System;
using System.Collections;
public class VehicleBehaviour : MonoBehaviour
{
public Vector3 StartDestination;
public Vector3 EndDestination;
public Vector3 StartingPosition;
public bool StraightDirection;
public Vehicle v;
bool rotating = false;
Vector3 angle;
IEnumerator Start()
{
if (StraightDirection)
{
StartingPosition = (StartingPosition.x != 0.0f && StartingPosition.y != 0.0f && StartingPosition.z != 0.0f) ? StartingPosition : StartDestination;
yield return StartCoroutine(Move (transform, StartingPosition, EndDestination, v.Speed, -1));
}
else
{
StartingPosition = (StartingPosition.x != 0.0f && StartingPosition.y != 0.0f && StartingPosition.z != 0.0f) ? StartingPosition : EndDestination;
yield return StartCoroutine(Move (transform, StartingPosition, StartDestination, v.Speed, 1));
}
StraightDirection = !StraightDirection;
while (true) {
yield return StartCoroutine(Move(transform, EndDestination, StartDestination, v.Speed, -1));
yield return StartCoroutine(Move(transform, StartDestination, EndDestination, v.Speed, 1));
}
}
IEnumerator Move(Transform thisTransform, Vector3 startPos, Vector3 endPos, float time, int d)
{
float i= 0.0f;
float rate= 1.0f/time;
float ang1 = Vector3.Angle (d*(startPos - endPos), -d * thisTransform.forward);
thisTransform.eulerAngles = new Vector3 (0, ang1, 0);
angle = thisTransform.eulerAngles;
while (i < 1.0f) {
i += Time.deltaTime * rate;
thisTransform.position = Vector3.Lerp(startPos, endPos, i);
thisTransform.position = thisTransform.position + new Vector3(0f, Terrain.activeTerrain.SampleHeight (transform.position) + renderer.bounds.size.y/2 + v.Distance, 0f);
yield return null;
}
float ang2 = Vector3.Angle (d*(endPos - startPos), -d * thisTransform.forward);
thisTransform.eulerAngles = new Vector3 (thisTransform.eulerAngles.x, ang2, thisTransform.eulerAngles.z);
}
}
(I've omitted some {if..then} here and there).
With this code I've obtained my vehicle to go correctly forward and "shift to reverse" instead of turning by 180°.
Among all my attempts I tried to modify this part "thisTransform.eulerAngles = new Vector3 (thisTransform.eulerAngles.x, ang2, thisTransform.eulerAngles.z);" in a thousand differnt ways and it seems that this should be the line to change (I've tried from the most obvious "-ang2" to a lot of code taken here and there in unityAnswers in many answers -as in example: how to get the positive or negative angle and How can I rotate an object 180 degrees? (C#)-).
Thnks in advice for help :)
Your answer
Follow this Question
Related Questions
rotate to specific coordinate c# 1 Answer
Distribute terrain in zones 3 Answers
Rotating Joystick on Touch 0 Answers
Flip over an object (smooth transition) 3 Answers
Rotating without gimbal lock 1 Answer