How to move and rotate game object in the direction of movement,How to rotate and move gameObject in the direction of movement - 2d

I'm doing a top-down shooter in Unity 2D and I'm currently trying to make a fire point, but when a character changes direction of movement, the fire point stays in the same position, but I want it to move and rotate. Sadly i have no idea how to do it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5f;
public Rigidbody2D rb;
Vector2 movement;
public Animator animator;
void Update()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
if(movement != Vector2.zero)
{
animator.SetFloat("Horizontal", movement.x);
}
animator.SetFloat("Speed", movement.sqrMagnitude);
}
void FixedUpdate()
{
rb.MovePosition(rb.position + movement * moveSpeed * Time.deltaTime);
}
}
enter image description here
enter image description here
I want it to be like the first picture, but on the opposite side.
enter image description here
Something like this (I rotated it in photoshop)

Related

Rotate cube on button click in Unity3D

I have created one cube. I want to rotate that cube on button click.
I created script for rotating cube and another script for button click.
Now how can I make that cube rotate on this button click.
Code for Button:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ButtonInput1 : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
Button button=null;
void Rotate()
{
a++;
button.onClick.AddListener (delegate() { PlayerController.CubeRotate ();});
}
}
Code for RotateCube:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
// Use this for initialization
// Update is called once per frame
int a=1;
public void CubeRotate () {
if (Input.GetButton("Button1")) {
a++;
//transform.Rotate ( new Vector3(15,30,45) * Time.deltaTime);
}
if (a % 2 == 0) {
transform.Rotate ( new Vector3(15,30,45) * Time.deltaTime);
}
}
}
I just need to know, how to call cube's script on this button click. Is there any other way to achieve this?
Thats funny, i actually just answered this question worded exactly like yours asked by another person. Rotate object continuously on button click Unity3D

TranslateTransition in JavaFX does nothing

I'm trying to use a TranslateTransition object in JavaFX to move an onscreen object in a LOGO program I am building. I have an onscreen TurtleDisplay object, which extends ImageView, and this is what I'm trying to move. The code to move it is here:
public void drawTurtle(TurtleData currentData) {
TurtleImage inList = getTurtleImage(currentData);
if (inList==null) {
TurtleImage temp = new TurtleImage(currentData.getX(),
currentData.getY(), currentData.getHeading(), turtleImage);
myTurtlesGroup.getChildren().add(temp);
myTurtlesList.add(temp);
}
else {
TranslateTransition tt = new TranslateTransition(Duration.seconds(3),inList);
tt.setFromX(inList.getX());
tt.setFromY(inList.getY());
tt.setToX(inList.getX()+currentData.getX());
tt.setToY(inList.getY()+currentData.getY());
tt.setCycleCount(Timeline.INDEFINITE);
tt.play();
}
}
This code, which is part of the front end, is called from the back end via a Listener on an ObservableList. The backend contains this ObservableList of TurtleData objects that contain the information necessary to move a turtle on screen -- which turtle to move, the coordinate to move to, and the rotation of the turtle. The code to call this is here:
ObservableList<TurtleData> myTurtles = FXCollections
.observableArrayList();
myTurtles.addListener(new ListChangeListener<TurtleData>() {
#Override
public void onChanged(Change<? extends TurtleData> c) {
myDisplay.getSelectedWorkspace().getTV().clearTurtles();
while (c.next()) {
for (TurtleData addItem : c.getAddedSubList()) {
myDisplay.getSelectedWorkspace().getTV().drawTurtle(addItem);
}
}
}
});
I have stepped through with a debugger and ensured that this code is called -- specifically, the tt.play() line is run. Nothing moves on screen. Does anyone have any idea what is wrong? Do I need to setup an Animation Timeline? Thank you for any help!

XNA: Conditional Draw

Im making a game on WP7. I have added a button in it. what i want to do is that whenever i touch the button, the button sprite should change its frame from 0 to 1 (0 being the button-up frame and 1 being the button-pressed frame). i have added the sprite sheet for the button which contains two frames only. the size is also okay.
to summarize, i want to do this:
1) when touch the button
2) button-pressed frame should be drawn and come back to original frame after 1 or 2 sec. (just like it happens in every game with buttons).
Sounds more like a design issue then an implementation issue. Basically, you need an idea getting you started. I'm not too familiar with WP7 specifically, but I'll show you the basic design you could use; with some sudo code you can fill in later.
class Button
{
private Point CellSize;
public Point Position {get; set;}
private Texture2D buttonSheet;
public Button (Texture2D texture, Point CellSize)
{
this.buttonSheet = texture;
this.CellSize = CellSize;
}
public bool isPressed {get; private set;}
public void Update (bool isPressed)
{
this.isPressed = isPressed;
}
public void Draw (SpriteBatch sb)
{
Rectangle Source = new Rectangle (0,0,CellSize.X,CellSize.Y);
if (this.isPressed)
Source = new Rectangle (CellSize.X * 1, CellSize.Y * 1, CellSize.X, CellSize.Y);
sb.Draw (this.buttonSheet, new Rectangle(this.Position.X,this.Position.Y,CellSize.X,CellSize.Y), Source, Color.White);
}
}
And then to use it:
class Game1
{
Button Start = new Button(new Texture2D() /*load and include your texture here*/, new Point (100, 50) /* 100 wide, 50 tall cells */);
public Update (GameTime gt)
{
bool clicked = false /* Put code here to check if the button is pressed */;
Start.Update(clicked);
}
public Draw (SpriteBatch spriteBatch)
{
Start.Draw(spriteBatch);
}
}
(note haven't tested this code)
Hope that helps you get started, feel free to elaborate if you want more info on a specific part.
Max

How can I animate a circle with PlayN?

This is a follow up to my last question:
How can I draw a circle to the screen with PlayN?
For my simple case, I want to programmatically create a single colored circle and move it across a 2-D plain (doesn't need to use box2d lib).
A real-world example would likely involve animating several circles. Two real-world examples for this case (sorry, I had to remove the links -- not enough karma!):
Browsmos for Chrome
Ants AI Challenge
It was suggested in response to my last question that I would want to use the ImmediateLayer class, so I am looking to understand how to properly incorporate this into my game loop.
Here's is my code sample:
public class SimpleCircleAnimation implements Game {
// Surface
private GroupLayer rootLayer;
private ImmediateLayer surface;
private Canvas canvas;
private Circle circle;
private CanvasImage circleImage;
#Override
public void init() {
// create root layer
rootLayer = graphics().rootLayer();
// a simple circle object
int circleX = 0; int circleY = 0;
int circleRadius = 20;
circle = new Circle(circleX, circleY, circleRadius);
// create an immediate layer and add to root layer
ImmediateLayer circleLayer = graphics().createImmediateLayer(new ImmediateLayer.Renderer() {
public void render (Surface surf) {
circleImage = graphics().createImage(circle.radius*2, circle.radius*2);
canvas = circleImage.canvas();
canvas.setFillColor(0xff0000eb);
canvas.fillCircle(circle.radius, circle.radius, circle.radius);
surf.drawImage(circleImage, circle.x, circle.y);
}
});
rootLayer.add(circleLayer);
}
#Override
public void paint(float alpha) {
}
#Override
public void update(float delta) {
// move circle
int newX = circle.x + 4; int newY = circle.y + 4;
circle.setPoint(newX, newY);
}
#Override
public int updateRate() {
return 25;
}
}
This successfully moves the circle diagonally down the screen from left to right. A couple questions:
Is this implemented properly?
In the case of multiple animated circles, is the idea with ImmediateLayer that you would create a circle image for each circle within the Renderer callback? Or would you perhaps create an Immediate Layer for each circle and add those to the root layer?
I would not use ImmediateLayer wiht render (Surface surf) adapter. Here u have, inside the render method creation of an image
circleImage = graphics().createImage(circle.radius*2, circle.radius*2);
just put this in the paint method
surf.drawImage(circleImage, circle.x, circle.y);
using the normal layer and u should be fine
Painting is done in paint method, and do not put calculations there
Update is for calculations, and physics oriented stuff
I discovered a detailed practical example of ImmediateLayer usage in the Cute Game source within the PlayN Samples:
CuteGame.java (code.google.com)

Qt painted content goes lost

I am writing an info-screen program. I created a full-screen widget and draw contents onto it.
In order to extend the life cycle of the TFT-display device, I want to implement a pixel-shifting feature. With other words, in every X minutes, I shift the screen to left/right/top/down for Y pixels.
My approach is as follows:
I use two layers (two QWidget).
I paint contents on the top layer.
When a pixel-shifting is performed, I just move the top layer for specified offset.
And then fill a background color to the bottom layer.
However, I found a problem:
If I move up the top layer for 10 pixels, the 10-pixel-content goes out of the screen. But when I move this layer down for 10 pixels. The 10-pixel-content will not be updated, it is gone.
How can I keep these 10-pixel-content? Is there any magic widget flag to solve this problem?
UPDATE 1:
The code is written in language D, but it is easy to understand:
class Canvas: QWidget
{
private QPixmap content;
this(QWidget parent)
{
super(parent);
setAttribute(Qt.WA_OpaquePaintEvent, true);
}
public void requestForPaint(QPixmap content, QRegion region)
{
this.content = content;
update(region);
}
protected override void paintEvent(QPaintEvent event)
{
if (this.content !is null)
{
QPainter painter = new QPainter(this);
painter.setClipping(event.region);
painter.fillRect(event.region.boundingRect, new QColor(0, 0, 0));
painter.drawPixmap(event.region.rect, this.content);
this.content = null;
painter.setClipping(false);
}
}
}
class Screen: QWidget
{
private Canvas canvas;
this()
{
super(); // Top-Level widget
setAutoFillBackground(True);
this.canvas = new Canvas(this);
showFullScreen();
}
public void requestForPaint(QPixmap content, QRegion region)
{
this.canvas.requestForPaint(content, region);
}
private updateBackgroundColor(QColor backgroundColor)
{
QPalette newPalette = palette();
newPalette.setColor(backgroundRole(), backgroundColor);
setPalette(newPalette);
}
public shiftPixels(int dx, int dy)
{
this.canvas.move(dx, dy);
updateBackgroundColor(new QColor(0, 0, 0)); // Just a demo background color
}
}
Screen screen = new Screen;
screen.requestForPaint(some_content, some_region);
screen.shiftPixels(0, -10);
screen.shiftPixels(0, 10);
Looking at the code, my first guess is that your region might be wrong. Try repainting the whole widget each time, and see if that solves the missing 10 pixel problem. If it does, then try working out why your region isn't covering the newly exposed portion.
One possibility along those lines: I notice in your Screen::requestForPaint method that you directly call the Canvas::requestForPaint without doing anything with the region. In Qt, the coordinates for anything like that are often assumed to be local, so if you don't account for the current position of the canvas widget, you might get an incorrect region.
Why not setting the position of the widget directly...? Another options might be using QPainter::translate(-1,-1) or something similar.

Resources