- Home /
How to increment value of axis when something happens
I have a player and I want something to spawn beneath the player, but the spawned items will stack up beneath it, in order to not mess up the physics system due to the object spawning inside the player, I make the player jump using jumpforce, but this sometimes messes up the system and the objects merge with each other only to separate with a large amount of force causing player to fly away. Now I want to make it such that whenever the Mouse is clicked or the screen is tapped, the players position automatically increases and then an object spawn beneath it. So lets say the object is of X height. Now I want the player to move up by X whenever the screen is tapped and then the object must spawn below it. However as you know, we can not add float values to the axis, I realized it the hard way by doing:
player.transform.position = (player.transform.position + (0, 10, 0));
So is there a way to accomplish this? I've searched some forums but haven't gotten the desired results and have been stuck with this issue for a while(More than a Week). It would be awesome if anyone could help me out or just guide me in the right direction :-)
Really, you shouldn't have taken a week on this. You could just, you know, look at any example code, such as on the documentation. It's understandable you might have trouble if you're a beginner with typed program$$anonymous$$g, but a week is a long time to be stuck on something trivial like this. Try to use all the resources available to you, even if they seem boring: sometimes documentation, manuals, or blogs can be very enlightening.
Answer by CardboardComputers · Aug 13, 2020 at 11:54 AM
To expand on qIsUnDead (1132710)'s answer, remember that transform.position
is a Vector3
struct, and if you want to modify one of its components, you have to do it:
1) by creating a new Vector3
:
player.transform.position = new Vector3(
player.transform.position.x,
player.transform.position.y + 10f,
player.transform.position.z
);
2) by using some kind of function or operator on it:
player.transform.position += new Vector3(0f, 10f, 0f);
For this case there isn't much of a difference, and the latter is much cleaner. I'd recommend using that, but both will achieve the same goal.
You may have noticed that I added f
after each number; that lets the compiler know it's meant to be a float literal, but it's fine for you to omit it here, as the compiler will know to cast an int to a float.
Answer by qIsUnDead · Aug 13, 2020 at 10:42 AM
You can not add simple float like this . make sure to convert them to vector first.
player.transform.position = player.transform.position + new Vector3(0, 10, 0);
Thank You very much, it was a very dumb mistake of me, I am actually a beginner in unity and C# so these are kind of the mistakes I make.
Your answer
Follow this Question
Related Questions
How can i place subjects in different positions? 0 Answers
Distribute terrain in zones 3 Answers
Why doesent my code get executed? 1 Answer
Change position of a text prefab added to a world space canvas via scripting 0 Answers
NullReferenceException: Object reference not set to an instance of an object Line 19 1 Answer