Please HELP!

Hi, I’ve been posting threads for days asking for help to do this but no one has been able to help so could someone please write me a C# script for this as I would really appreciate it :slight_smile:

When the gameobjects x position gets to 20 I want it to stop moving, rotate 180 degrees and then start moving again. Can anyone help me write a script for this as it’s urgent!

Thanks
Jana1108 :slight_smile:

Sounds more like contract work to me…

I’d be very interested to see why this is “urgent”

using UnityEngine;

public class MoveRightThenBackAgain : MonoBehaviour {

    [SerializeField, Range(5f,200f)] float distance = 20f;
    [SerializeField, Range(1f,20f)] float speed = 10f;
   
    bool pointingRight;

    void Update ()
    {
        float delta = Time.deltaTime * speed;

        float x = pointingRight ? distance : 0f;
        var target = Vector3.right * x;

        transform.position = Vector3.MoveTowards (transform.position, target, delta);

        float r = pointingRight ? 0f : 180f;
        transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.Euler(Vector3.forward * r), delta);

        if (pointingRight && transform.position.x >= distance)
            pointingRight = false;

        else if (!pointingRight && transform.position.x <= 0f)
            pointingRight = true;
    }
}

Check post histories, there is often a need for a collection of whole scripts rather than indicators on how to do it followed by an announcement that the latest game is released on an App Store.

2 Likes

your Avatar paired with your reply just made my day

3 Likes

Thanks everyone :slight_smile:

So true. That’s why I didn’t bother to reply either.