Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Bugs & Fixes 2 years ago
Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
Turn Your Knowledge into Earnings.
I'm developing a 2D game in Unity, and I am stuck with a problem. I am trying to move a sprite from point A to point B when the user clicks. I have a Vector3 for both the starting point and the target.
I was able to get the desired change in position but only in one frame. I want the sprite to actually slide across, but I have been unable to. I tried using Coroutines but have not been successful.
IEnumerator CubeSlider(Vector3 start, Vector3 target) { while(start.x != target.x) { start.x += 0.1f; yield return null; } }
I'm only taking x into account because there is no movement on the y axis. I also tried using:
start = Vector3.MoveTowards(parameters here)
but was not successfull.
I started and called this Coroutine on my "onMouseDown()" function of the script like so:
StartCoroutine(CubeSlider(square1.transform.position, p3));
I would expect that when the user makes a click, the sprite would smoothly move across from start to target. I'm not getting any compiler errors, it's simply not moving at all.
Your first approach is incorrect. You are just adding to that float value another float value. Instead, you need to use Vector2 or Vector3. You can use your approach and that is not going to have any errors, but that won't affect your gameObject. If you wanted to move an object like that, you needed to use Vector2 or Vector3 in your loop. Also, you will need a helper method because values between those 2 Vectors will never be exactly equal. Below is one example of your first approach.
start.position += new Vector3(2f, 0f, 0f) * Time.deltaTime;
Since I see that you are passing arguments to your Coroutine, if you want to move a gameObject that doesn't have this script attached, pass its Transform Component. Of course, because this is an example I have placed everything in 1 Script. Below is an example of Unity Method.
public class Example : MonoBehaviour { public Vector3 myTarget; public Transform myStart; void Start() { StartCoroutine(CubeSlider(myStart, myTarget)); } IEnumerator CubeSlider(Transform start, Vector3 target) { while (start.position.x != target.x) { start.position = Vector3.MoveTowards(start.position, target, 2f * Time.deltaTime); yield return null; } Debug.Log("I reached my target. Done!"); } }
No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
General Tech 9 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.