Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by gnirts · May 16, 2014 at 01:34 PM · c#rotateangleflip

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 :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges