hii iam new learner i need help i have been done the project succesfully but i have a problem i make to mountins for the player in the first mountin the boost speed dosent work when i press the up arrow key but in the second mountin after the player passing the first mountin (in the second mountin of the same scene and the same project ) the boost speed work like i wish it to work can same one give me an advice to make it work in the first and the second mountin???
You’ve posted in the wrong place. This is Unreal. I’ve edited the tag so hopefully you will get an answer soon.
ooo sorry for that:)
The reason it’s only working for one mountain is that PlayerController
uses FindObjectOfType<SurfaceEffector2D>()
to find a single game object in the scene. Rick mentions in the lecture that it won’t work if you have more than one of that object type in the same scene. The script is looking for the first Surface Effector 2D
that it can find, in your case that’s the second mountain. There are a number of ways to work around this (and FindObjectOfType
is being phased out in later versions of Unity anyway), the simplest (but not the best) is probably to make serialized fields for every mountain in your game scene. At the top of PlayerController
add this:
[SerializeField] SurfaceEffector2D mountain1;
[SerializeField] SurfaceEffector2D mountain2;
In the Inspector, drag one mountain into the field for mountain1
and the other into the field for mountain2
Then in RespondToBoost()
you can change the speed of every mountain like this:
if(Input.GetKey(KeyCode.UpArrow))
{
mountain1.speed = boostSpeed;
mountain2.speed = boostSpeed;
}
else
{
mountain1.speed = baseSpeed;
mountain2.speed = baseSpeed;
}
If you add more mountains, you can add more serialized fields and put them into the script. This isn’t the best way to do things because you have to go into your code and add more references to mountains every time you add one to your scene; it’s easy to forget to add a mountain in the inspector which will give you an error, and if you make more than one scene you’ll have to address every mountain in every new scene. But it’s a simple way using what you’ve learned at this point in the course, if you’re mostly interested in getting these two mountains to work right now.
A better solution would involve making a List<SurfaceEffector2D>
using FindObjectsByType
(note the plural in Objects) and using a foreach loop. These are concepts which are taught in the next section of the course, Quiz Master.
Really Thank You for your detailed response, smile:
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.