Jump to content
  • Advertisement
Sign in to follow this  
hackersk

Unity How can I move an object with keyboard and the camera follows it

This topic is 2128 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello. I'm new to the forum and would like your help on something. Basically I want to say I am beginner in programming in language C++, I read a whole book on Java and learned enough. I've also learned from video on Youtube and also know how to work as a novice in Unity.

 

I would like to build a little game in Unity in which there will be an item (a bike) that will take command of the keyboard with the right arrow to move forward. The game is basically 2D. I just need to figure out the logic. And also the camera is fixed on the object and follow it and the background is a painting that changes accordingly. How can it happen? Playing it as reminiscent of the ones with motorbikes just the buttons you have to get to the end of the track, but it is 2D. Like similar games in miniclip.com

Edited by hackersk

Share this post


Link to post
Share on other sites
Advertisement

This example is in b4a and psuedocode but the logic is the same.

 

I scroll by tracking the x,y for the character and the mx,my for the map like so:

sub maintimer
If (x-mx)*scale>w*.75 Then px=speed                                Scroll map when needed
If (x-mx)*scale<w*.25 Then px=-speed
If (x-mx)*scale>w*.29 AND (x-mx)*scale<w*.71 Then px=0
If (y-my)*scale>h*.90 Then py=speed
If (y-my)*scale<h*.10 Then py=-speed
If (y-my)*scale>h*.35 AND (y-my)*scale<h*.75 Then py=0
mx=mx+px
my=my+py
end sub

Sub Surface_Draw(ac As AcceleratedCanvas)
ac.MatrixsetScale(scale*1.01,scale*1.01)
ac.DrawBitmapWithMatrixAt(crate,(c*128-mx)*scale, (l*128-my)*scale, True)

drawcharactor(x-mx,y-my)

end sub

Basically the map or background gets drawn on mx,my and the character gets drawn at x-mx,y-my, If the character gets near the edge of the screen, the map coordinates move until the character is back in the center.

 

This is the game I'm talking about and its a WIP:

http://flatank.blogspot.com/2013/06/super-metroid.html

You can download the latest build if you have an Android device.

Edited by latch

Share this post


Link to post
Share on other sites

Hi. Thanks for your reply. I can't understand about this code. I have to use it in Unity and enter it at the camera in a script? Sorry but I do not know too much about programming, I'm a newbie. I want the camera to follow the object in Unity. And how the object will move by pressing the right arrow on the keyboard?

 

Thanks again :)

Share this post


Link to post
Share on other sites

For my game I did it like this (i did mine in C++ but the concept should be the same):

You have two rectangles: a viewport, that's the area that's supposed to be visible, basically the whole area of the window/screen, and the boundaries of your level.

The center of the viewport is the spot the camera is "looking at". So if you set the cameras centerpoint to the players centerpoint in every frame, it will follow the player.

All you need to do is keep the viewport in the boundaries of the level and then when drawing the player draw him at his current position minus the distance of the viewports left side to the left side of your level boundary.

Like in the attached picture tongue.png

xOffset = cameraCenter.x - (viewportWidth / 2) - bounds.left;

yOffset = cameraCenter.y - (viewportHeight / 2) - bounds.top

Something like that. I'm not sure if this is 100% correct but it should get you started.

 

Share this post


Link to post
Share on other sites


Hi. Thanks for your reply. I can't understand about this code. I have to use it in Unity and enter it at the camera in a script?

 

sounds like you need help with unitiy.   post a new question asking how to move the camera in unity.

Share this post


Link to post
Share on other sites

Okay, thank you very much everyone! I did not expect an immediate response so quickly :) I'm pretty happy. If I need something will post it here.

Thanks again

Share this post


Link to post
Share on other sites

For my game I did it like this (i did mine in C++ but the concept should be the same):

You have two rectangles: a viewport, that's the area that's supposed to be visible, basically the whole area of the window/screen, and the boundaries of your level.

The center of the viewport is the spot the camera is "looking at". So if you set the cameras centerpoint to the players centerpoint in every frame, it will follow the player.

All you need to do is keep the viewport in the boundaries of the level and then when drawing the player draw him at his current position minus the distance of the viewports left side to the left side of your level boundary.

Like in the attached picture tongue.png

xOffset = cameraCenter.x - (viewportWidth / 2) - bounds.left;

yOffset = cameraCenter.y - (viewportHeight / 2) - bounds.top

Something like that. I'm not sure if this is 100% correct but it should get you started.

This is the same thing I did in a roundabout way. Even the formula is the same sorta!

Share this post


Link to post
Share on other sites

Hello again. This is the final view of the game I want to make. Check the photos (links) to see what I have create and what was that I talked about.

 

Check here:

 

http://i40.tinypic.com/2d8hxnd.png

 

http://i44.tinypic.com/2whfh4j.png

 

http://i39.tinypic.com/30icgma.png

 

In Main Camera the script is Smooth Follow.js that is included in Unity.

 

Thank you all again ;)

 

If there is any point of view, I need immediately smile.png

 

Now the Object (cube) moves right and left.

 

PS: Sorry for my English rolleyes.gif

Edited by hackersk

Share this post


Link to post
Share on other sites

Hi, I don't think you need the Smooth Follow.js. Have you tried moving the camera object to be a child of the cube object and removing the smooth follow script? If you do that the camera should just follow the cube movement exactly if that is what you want.

Share this post


Link to post
Share on other sites

Yes Rorakin, this is exactly what I want and more ok for my game. But also the Smooth Follow is ok. No problem. I have understand now the whole situation :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!