Questions about how to use Unity C# to input coordinates into a conditional expression and other questions.

Asked 1 years ago, Updated 1 years ago, 81 views

I am a beginner in software development and programming.
I am writing a script using unity ver 5.1.1 f1, C#, but I have a question because it has boiled down.

I'm trying to write a script that says Object(maincamera) at point A moves in the X direction every time I receive a key input such as space under certain coordinate conditions such as x<100 and then returns to point A (e.g., x>100), but it doesn't work well.

using UnityEngine;
using System.Collections;

public class mvcam:MonoBehavior {

    // Use this for initialization
    void start() {
    }

        void update() {
//      intx;
        transform.localPosition=new Vector3(-11,0,0);
        Vector3pos = transform.position;
        pos.x+=2;
        transform.position=pos;

//      Vector3pos = transform.position;
//      transform.position=pos;
        if (Input.GetKey(KeyCode.Space)&pos.x<100)
//          pos.x+ = new Vector 3(1,0,0);
        print(transform.localPosition.x);
    }
}

I intend to write a conditional expression for if(Input.GetKey(KeyCode.Space)&pos.x<100), but the key has not responded and is not working at all.

If possible, I would like you to explain the script as well.Thank you for your cooperation.

c# unity3d

2022-09-30 21:13

1 Answers

if(Input.GetKey(KeyCode.Space)&pos.x<100)
Regarding the conditional expression in
Every time I get a key, if that's the case,

instead of Input.GetKey() returning true while pressing the key You should use Input.GetKeyDown() to return true only to the frames you press.

void update(){
//      intx;
        transform.localPosition=new Vector3(-11,0,0);
        Vector3pos = transform.position;
        ...
        }

First, the initials of the update function are lowercase.
void Update() to be called every frame.
The start function used for initialization is also empty, but the initials are also lowercase.

This is a concern, but if every frame is called,
transform.localPosition initializes every frame Vector3(-11,0,0) Was it intended as an action?

It is also worrisome that transform.localPosition and transform.position are mixed.


2022-09-30 21:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.