I can't get my character to move backward or forwards- but left and right work perfectly fine! The inputs in the project are fine, there's nothing crazy I did with the physics on the rigidbody, I just can't seem to figure out the issue.
Portion of code pertaining to movement:
if (Input.GetAxis("Horizontal") > 0 || Input.GetAxis("Horizontal") < 0) {
gameObject.transform.GetComponent<Rigidbody>().MovePosition(gameObject.transform.position + (gameObject.transform.right * Input.GetAxis("Horizontal") * 10 * hSpeedMult * Time.deltaTime));
posAt = gameObject.transform.position;
}
else {
gameObject.transform.position = new Vector3(posAt.x, gameObject.transform.position.y, posAt.z);
}
if (Input.GetAxis("Vertical") > 0 || Input.GetAxis("Vertical") < 0) {
gameObject.transform.GetComponent<Rigidbody>().MovePosition(gameObject.transform.position + (gameObject.transform.forward * Input.GetAxis("Vertical") * 10 * vSpeedMult * Time.deltaTime));
posAt = gameObject.transform.position;
}
else {
gameObject.transform.position = new Vector3(posAt.x, gameObject.transform.position.y, posAt.z);
}
The "posAt" is just used to make sure the player cannot move when no controls are pressed.
Once I removed the "position freezer", it did the trick. My "PosAt" lines were the problem.
Related
I need some help understanding why a bug is happening in some code using a GtkTreeView, and how to fix it.
In the program, the user opens an image and runs a function to detect stars in the image. This is all reliable and works fine. Data about each star is populated into a GtkTreeView.
The user then clicks on a star in the display which triggers a callback:
gboolean on_drawingarea_button_release_event(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
that in turn calls the function shown below:
void set_iter_of_clicked_psf(double x, double y) {
GtkTreeSelection *selection = GTK_TREE_SELECTION(gtk_builder_get_object(gui.builder, "treeview-selection"));
GtkTreeView *treeview = GTK_TREE_VIEW(lookup_widget("Stars_stored"));
GtkTreeModel *model = gtk_tree_view_get_model(treeview);
GtkTreeIter iter;
gboolean valid;
gboolean is_as;
const double radian_conversion = ((3600.0 * 180.0) / M_PI) / 1.0E3;
double invpixscalex = 1.0;
double bin_X = gfit.unbinned ? (double) gfit.binning_x : 1.0;
if (com.stars && com.stars[0]) {// If the first star has units of arcsec, all should have
is_as = (strcmp(com.stars[0]->units,"px"));
} else {
return; // If com.stars is empty there is no point carrying on
}
if (is_as) {
invpixscalex = 1.0 / (radian_conversion * (double) gfit.pixel_size_x / gfit.focal_length) * bin_X;
}
valid = gtk_tree_model_get_iter_first(model, &iter);
while (valid) {
gdouble xpos, ypos, fwhmx;
gtk_tree_model_get(model, &iter, COLUMN_X0, &xpos, -1);
gtk_tree_model_get(model, &iter, COLUMN_Y0, &ypos, -1);
gtk_tree_model_get(model, &iter, COLUMN_FWHMX, &fwhmx, -1);
fwhmx *= invpixscalex;
gdouble distsq = (xpos - x) * (xpos - x) + (ypos - y) * (ypos - y);
gdouble psflimsq = 6. * fwhmx * fwhmx;
if (distsq < psflimsq) {
GtkTreePath *path = gtk_tree_model_get_path(model, &iter);
if (!path) return;
gtk_tree_selection_select_path(selection, path);
gtk_tree_view_scroll_to_cell(treeview, path, NULL, TRUE, 0.5, 0.0);
gtk_tree_path_free(path);
gui.selected_star = get_index_of_selected_star(xpos, ypos);
display_status();
redraw(REDRAW_OVERLAY);
return;
}
valid = gtk_tree_model_iter_next(model, &iter);
}
siril_debug_print("Point clicked does not correspond to a known star\n");
return;
}
The code steps through each iter in the GtkTreeView and checks whether the location clicked is close enough to the centre of a star: the key bit of the code then follows inside the conditional towards the end of the function. If the location matches a star, a GtkTreePath is declared and initialised based on the iter, changes the selection to that path and calls gtk_tree_view_scroll_to_cell() which is supposed to scroll the GtkTreeView so that the selected star is shown.
For some users the code works every time and causes the tree view to scroll to the selected star. For other users sometimes it works, sometimes it does not work. When it doesn't work I can tell the correct code path is followed because gui.selected_star is updated correctly (it triggers a drawing event in the subsequent redraw(REDRAW_OVERLAY) call. It is only gtk_tree_view_scroll_to_cell() that appears to function erractically.
Neither I nor one of my fellow developers can see why it works sometimes but not always, nor can we see why some users don't see the bug at all. So if any GTK gurus could enlighten me I'd be most grateful!
if (Input.GetKeyDown("z") == true)
{
moveSpeed = 10;
}
else if (Input.GetKeyDown("z") == false)
{
moveSpeed = 5;
}
that is the code.
When it runs it seems to detect that z is pressed and than set moveSpeed to 10. But than it fails to detect that z is not pressed and resets moveSpeed back to 5.
I have tried a few variations of this code, all seem to have the same result.
you must use Input.GetKeyUp("z")
I've been using the YouMax plugin which enables you to embed your YouTube channel on your website. However, I am having problems as it displays the uploaded date in months and years. I'd like it to display days, weeks, months and years.
You can view the source code here http://jsfiddle.net/wCKKU/
I believe that its this that needs adjusting to make it calculate in day, weeks, months and years.
function getDateDiff(timestamp) {
if (null == timestamp || timestamp == "" || timestamp == "undefined") return "?";
var splitDate = ((timestamp.toString().split('T'))[0]).split('-');
var d1 = new Date();
var d1Y = d1.getFullYear();
var d2Y = parseInt(splitDate[0], 10);
var d1M = d1.getMonth();
var d2M = parseInt(splitDate[1], 10);
var diffInMonths = (d1M + 12 * d1Y) - (d2M + 12 * d2Y);
if (diffInMonths <= 1) return "1 month";
else if (diffInMonths < 12) return diffInMonths + " months";
var diffInYears = Math.floor(diffInMonths / 12);
if (diffInYears <= 1) return "1 year";
else if (diffInYears < 12) return diffInYears + " years"
}
You could modify the plugin by a small block of code in the middle of the function:
var d2M = parseInt(splitDate[1], 10); // this line is already there
var d1D = d1.getDate();
var d2D = parseInt(splitDate[2],10);
var diffInDays = (d1D + 30 *d1M + 12 * d1Y) - (d2D + 30 *d2M + 12 *d2Y);
if (diffInDays < 2) return "1 day";
else if (diffInDays < 7) return diffInDays+" days";
else if (diffInDays > 7 && diffInDays < 14) return "1 week";
else if (diffInDays > 14 && diffInDays < 30) return Math.floor(diffInDays / 7) + " weeks";
var diffInMonths = (d1M + 12 * d1Y) - (d2M + 12 * d2Y); // this line is already there
Note that this isn't a particularly elegant way to handle the issue, but it matches the coding style the plugin is already using, and at least won't break anything else.
Also, as a side comment, if you're modifying the plugin code you'll want to fix a bug in it at the same time. Getting the current month should look like this:
var d1M = d1.getMonth() + 1;
This is because in Javascript, the getMonth() function returns the month on a zero-based index, and your math won't be reliable unless you switch it to a one-based index.
I am new to ActionScript-3 and I am attempting to make a game to learn more.
For every picture that is displayed I want there to be 4 choices (buttons) and only one of them to be the correct one. But how can I make it so that the text from the buttons will be random.
As you can see I've made it so the 4th button is always the correct answer. I don't want to make all this thing for every picture that is displayed...to much pointless code.
Can anybody help me? If you need extra information I will gladly provide it.
var k:int;
for(k=1;k<=3;k++)
{
GAME.variante.buttonMode=true;
GAME.variante.addEventListener(MouseEvent.MOUSE_OVER,mouse_over_variante);
GAME.variante.addEventListener(MouseEvent.MOUSE_OUT,mouse_out_variante);
GAME.variante.varianta_corecta.addEventListener(MouseEvent.CLICK,variante);
GAME.variante.varianta_gresita1.addEventListener(MouseEvent.CLICK,variante_gresiteunu);
GAME.variante.varianta_gresita2.addEventListener(MouseEvent.CLICK,variante_gresitedoi);
GAME.variante.varianta_gresita3.addEventListener(MouseEvent.CLICK,variante_gresitetrei);
GAME.varianta1.text = "Cameleon";
GAME.varianta2.text = "Snake";
GAME.varianta3.text = "Frog";
GAME.varianta4.text = "Snail";
function variante_gresiteunu(e:MouseEvent){
if (varianta_gresita_apasata1 == 1){
totalScore -= score_variante_gresite;
GAME.text1.text = totalScore;
varianta_gresita_apasata1 = 2;
}
}
function variante_gresitedoi(e:MouseEvent){
if (varianta_gresita_apasata2 == 1){
totalScore -= score_variante_gresite;
GAME.text1.text = totalScore;
varianta_gresita_apasata2 = 2;
}
}
function variante_gresitetrei(e:MouseEvent){
if (varianta_gresita_apasata3 == 1){
totalScore -= score_variante_gresite;
GAME.text1.text = totalScore;
varianta_gresita_apasata3 = 2;
}
}
}
GAME.extra_points.visible = false;
function variante (e:MouseEvent) {
if (GAME.stichere.sticker1.currentFrame == (1)){
GAME.extra_points.visible = true;
GAME.extra_points.plus_ten1.gotoAndPlay(1);
}
//go to great job screen
GAME.greatJob.stars.gotoAndPlay(1);
GAME.greatJob.visible = true;
}
function mouse_over_variante (e:MouseEvent) {
trace(e.target.name);
e.target.gotoAndPlay(1);
}
function mouse_out_variante (e:MouseEvent) {
e.target.gotoAndStop(1);
}
You like to have 4 images and they will be tested right?
The text below the images will be randomness. I saw your code and I
confess I was confused. I made a different one.
I undestand that this code is a little diferent of what you ask, but i think it will > give you some new ideas and help you on your app...
//start button added on the sceen named f3toc. I give a function name for him f3roll.
f3toc.addEventListener(MouseEvent.CLICK,f3roll);
function f3roll(e:MouseEvent):void{
//creating variables for the picture.
var bola:Number
var quadrado:Number
var pentagono:Number
//Here is just a randomization code, you can change it to the what you want to use after the =
bola = Math.ceil(Math.random() * 10);
pentagono = Math.ceil(Math.random() * 10);
f3res1_txt.text = String (bola + 8 + 8);
f3res2_txt.text = String(bola - 1 + bola);
f3res3_txt.text = String (pentagono + 10 - bola);
//converting number to string so we can put tem into the text fields.
var pentagonotring:String = pentagono.toString();
var bolastring:String = bola.toString();
//function to check wen the name is correct. each wrong do nothing and every correct add 1 to a variable, in the end wen this variable reach 3 it does something.
f3check_bnt.addEventListener (MouseEvent.CLICK, f3check);
function f3check (e:MouseEvent):void{
if (f3inp2_txt.text == pentagonotring){
f3ver_ext2.text = "Correct"
} else {f3ver_ext2.text = "Wrong";}
if (f3inp1_txt.text == bolastring){
f3ver_ext1.text = "Correct"
}else {f3ver_ext1.text = "Wrong";}
// function to check wen the variable pass reach 3
pass = 0;
if (f3ver_ext1.text == "Correct"){
pass++
}
if (f3ver_ext2.text == "Correct"){
pass++
if (pass == 3){
nextFrame();
}}}}
I have a segmented control with 3 defined segments. i am looking to capture the segment index so I can use it in an if statement to update variables accordingly as such:
-(IBAction)numPlayers:(id)sender;
{
numPlayersSegment = [(UISegmentedControl *)sender retain];
if (numPlayersSegment.selectedSegmentIndex == 0)
{
numOfPlayers = 2;
}
else if (numPlayersSegment.selectedSegmentIndex = 1)
{
numOfPlayers = 3;
}
else if (numPlayersSegment.selectedSegmentIndex = 2)
{
numOfPlayers = 4;
}
else if (numPlayersSegment.selectedSegmentIndex = -1)
{
numOfPlayers = 0;
}
NSLog(#"Players selected = %d", numPlayersSegment.selectedSegmentIndex);
However whenever I press the third segment (index 2) it returns the value of the second segment (index 1) and also highlights the 2nd segment. I can see nothing untoward in IB.
Has anybody seen this and have any suggestions.
I am using xcode 3.2.1 on snow leopard
thanks
== and = don't function the same. At a minimum you should fix that. You also don't want the added retain to sender.