Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by PlayouGames · Sep 27, 2021 at 10:20 AM · rotationmovementcharacter movementtransform.rotation

Flipping MeshRenderer upon direction change

Hello,

I have a 3D mesh Renderer that I'm trying to move from A to B. Currently the movement is fine and works well. However what im trying to do right now is flip the rotation in Y. So its a spike, moves from A to B, then id like it to flip when its going from B to A

Adding this GIF of what it does, Hopefully you can see the spike coming down needs to have flipped, it could be a Scale to-1, but please advise. https://gfycat.com/waterloggedconfusedcrayfish

Here is my code for everything other than the flipping. It is also a 2.5 D game with 3D models.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ShootingSpike : MonoBehaviour
 {
 
     public float moveSpeed = 5f;
     public Transform pointA, pointB;
 
     private bool movingUp;
 
     private Rigidbody2D theRB;


 // A Public variable to add my 3D model. 
     public MeshRenderer myModel;
 
 
 
     // Start is called before the first frame update
     void Start()
     {
         theRB = GetComponent<Rigidbody2D>();
         pointA.parent = null;
         pointB.parent = null;
 
         movingUp = true;
 
     }
 
     // Update is called once per frame
     void Update()
     {
       if (movingUp)
         {
 
             theRB.velocity = new Vector2(theRB.velocity.x, moveSpeed);
 

             if (transform.position.y > pointA.position.y)
             {
                 movingUp = false;
             }
         } else
         {
             theRB.velocity = new Vector2(theRB.velocity.x, -moveSpeed);
             if (transform.position.y < pointB.position.y)
             {
                 movingUp = true;
             }
         }
     }
 }
  








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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by rage_co · Sep 28, 2021 at 11:01 AM

maybe this?

 using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class ShootingSpike : MonoBehaviour
  {
  
      public float moveSpeed = 5f;
      public Transform pointA, pointB;
      Vector3 baseRotation;
  
      private bool movingUp;
  
      private Rigidbody2D theRB;
      // A Public variable to add my 3D model. 
      public MeshRenderer myModel;
  
  
  
      // Start is called before the first frame update
      void Start()
      {
          theRB = GetComponent<Rigidbody2D>();
          baseRotation = transform.rotation;
          
          pointA.parent = null;
          pointB.parent = null;
  
          movingUp = true;
  
      }
  
      // Update is called once per frame
      void Update()
      {
        if (movingUp)
       {
         MoveUp();
       } 
       else
       {
         FallDown();
       }
      }
 
      void FallDown()
      {
        theRB.velocity = new Vector2(theRB.velocity.x, -moveSpeed);
        transform.eulerAngles.z = -baseRotation.z;
        
        if (transform.position.y < pointB.position.y)
        {
          movingUp = true;
        }
      }
 
      void MoveUp()
      {   
        theRB.velocity = new Vector2(theRB.velocity.x, moveSpeed);
        transform.eulerAngles.z = -baseRotation.z;
      
        if (transform.position.y > pointA.position.y)
        {
          movingUp = false;
        }
      }
  }

just rotating the spike on the z axis should do the trick i think

Comment
Add comment · Show 4 · Share
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
avatar image PlayouGames · Sep 28, 2021 at 08:49 PM 0
Share

Thank you for your reply, i seem to get these 2 errors. Would you $$anonymous$$d sharing you thoughts? The Transform.rotation, does the T need to be capital?

alt text alt text

1.png (15.0 kB)
2.png (22.1 kB)
avatar image rage_co PlayouGames · Sep 29, 2021 at 12:58 AM 0
Share

the transform is supposed to be lowercase....i think you just can't use eulerAngles directly

avatar image PlayouGames rage_co · Sep 29, 2021 at 01:24 AM 0
Share

it is done, thank you for the help

 > using System.Collections; using
 > System.Collections.Generic; using
 > UnityEngine;
 > 
 > public class ShootingSpike :
 > MonoBehaviour {
 > 
 > 
 >     [Header("Controller")]
 >     public float moveSpeed = 5f;
 > 
 > 
 > 
 > 
 >     [Header("Targets")]
 >     public Transform pointA;
 >     public Transform pointB;
 > 
 >     private Vector3 baseRotation;
 > 
 >     private bool movingUp;
 > 
 >     private Rigidbody2D theRB;
 > 
 >     private Quaternion myRotation;
 > 
 > 
 > 
 > 
 >     // Start is called before the first frame update
 >     void Start()
 >     {
 >         theRB = GetComponent();
 >         myRotation = transform.rotation;
 > 
 >         pointA.parent = null;
 >         pointB.parent = null;
 > 
 >         movingUp = true;
 > 
 >     }
 > 
 >     // Update is called once per frame
 >     void Update()
 >     {
 >         if (movingUp)
 >         {
 >             MoveUp();
 >         }
 >         else
 >         {
 >             FallDown();
 >         }
 >     }
 > 
 >     void FallDown()
 >     {
 >         theRB.velocity = new Vector2(theRB.velocity.x, -moveSpeed);
 >         transform.rotation = Quaternion.Euler(0f, 0f, 180f);
 >         if (transform.position.y < pointB.position.y)
 >         {
 >             movingUp = true;
 >         }
 > 
 >        
 >     }
 > 
 >     void MoveUp()
 >     {
 >         theRB.velocity = new Vector2(theRB.velocity.x, moveSpeed);
 >         transform.rotation = Quaternion.Euler(0f, 0f, 0f);
 >         if (transform.position.y > pointA.position.y)
 >         {
 >             movingUp = false;
 >         }
 >     } }



Show more comments
avatar image
0

Answer by PlayouGames · Sep 28, 2021 at 09:49 PM

@rage_co

Thanks again,

the rotate Z should work. I have three errors as i mentioned above, thanks for your input.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ShootingSpike : MonoBehaviour
 {
 
 
     [Header("Controller")]
     public float moveSpeed = 5f;
 
 
 
 
     [Header("Targets")]
     public Transform pointA;
     public Transform pointB;
 
     private Vector3 baseRotation;
 
     private bool movingUp;
 
     private Rigidbody2D theRB;
 
 
 
 
     // Start is called before the first frame update
     void Start()
     {
         theRB = GetComponent<Rigidbody2D>();
         baseRotation = transform.rotation;
 
         pointA.parent = null;
         pointB.parent = null;
 
         movingUp = true;
 
     }
 
     // Update is called once per frame
     void Update()
     {
         if (movingUp)
         {
             MoveUp();
         }
         else
         {
             FallDown();
         }
     }
 
     void FallDown()
     {
         theRB.velocity = new Vector2(theRB.velocity.x, -moveSpeed);
         transform.eulerAngles.z = -baseRotation.z;
 
         if (transform.position.y < pointB.position.y)
         {
             movingUp = true;
         }
     }
 
     void MoveUp()
     {
         theRB.velocity = new Vector2(theRB.velocity.x, moveSpeed);
         transform.eulerAngles.z = -baseRotation.z;
 
         if (transform.position.y > pointA.position.y)
         {
             movingUp = false;
         }
     }
 }



Comment
Add comment · Show 1 · Share
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
avatar image rage_co · Sep 29, 2021 at 01:26 AM 1
Share

First things first im stupid, i tried to save in rotation, which is a quaternion into vector3...so change the line baseRotation = transform.rotation to

 baseRotation = GetComponent<Transform>().eulerAngles;

Just making sure that the transform is indeed correct using get component.......

And then to change the direction

 void FallDown()
      {
          theRB.velocity = new Vector2(theRB.velocity.x, -moveSpeed);
          GetComponent<Transform>().rotation= Quaternion.Euler(baseRoation.x, baseRoation.y, 
  baseRoation.z + 180);
          if (transform.position.y < pointB.position.y)
          {
              movingUp = true;
          }
      }

And similarly,

 void MoveUp()
      {
          theRB.velocity = new Vector2(theRB.velocity.x, moveSpeed);
          GetComponent<Transform>().rotation = Quaternion.Euler(baseRotation.x, baseRotation.y, baseRotation.z);
  
          if (transform.position.y > pointA.position.y)
          {
              movingUp = false;
          }
      }

And just to be sure...what 3 lines does it show an error in?

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

227 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

i'm using a code to make my cube walk with character controller but when i play the cube spins. 0 Answers

GameObject not looking at me.. 1 Answer

player movement not using rotation 0 Answers

i have a spin wheel and a button when i click the button i need to rotate the wheel for a certain period,each time the rotation speed should be random 0 Answers

Touch to move and drag to rotate object 2 Answers


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