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;
}
}
}
}
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
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
the transform is supposed to be lowercase....i think you just can't use eulerAngles directly
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;
> }
> } }
Answer by PlayouGames · Sep 28, 2021 at 09:49 PM
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;
}
}
}
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
![](https://koobas.hobune.stream/wayback/20220613043416im_/https://answers.unity.com/themes/thub/images/avi.jpg)