Advertisement

[C#] Some doubts with regards to an "if statement" and "instantiate" statement

Started by December 02, 2016 06:12 PM
6 comments, last by Zesi 8 years ago

Hi all. I need help with some C# scripts I am trying to solve.

(1) I want to move an object left and right on a 2D plane, restricting it within the parameters I will be setting on the inspector.

This is what I've written.


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class movingObject : MonoBehaviour {
 
	public Vector3 rightEdge;
	public Vector3 leftEdge;
	public Vector3 currentPosition;
	public float speed;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {

		if (Vector3.Distance (currentPosition, rightEdge) <= 0) {
			transform.position += Vector3.left * speed * Time.deltaTime;
		} else if (Vector3.Distance (currentPosition, leftEdge) >= 0) {
			transform.position += Vector3.right * speed * Time.deltaTime;
		}
	}
}

I understand that there is some logic problem here. What if movingObject is at 0?

How can I rectify this problem?

(2) Why does "transform.position" not work on instantiate?

For example:


if (Input.GetKey (KeyCode.Space)) 
		{
			GameObject beam = Instantiate (projectilesPrefab, new Vector3 (transform.position.x, -3.5f, transform.position.y), Quaternion.identity) as GameObject;
			transform,position += Vector3.up * speed * Time.deltaTime;
		}

This code does not work at all. After some googling, I realised I need to use rigidbody2d to get what I want.

I am just curious why does "transform position" not work on an Instantiate statement.

(3) Also with regards to instantiate, similar to script on (2) above. How do I script the rate of instantiate? is there such a thing?

What I mean is, for example, I want instantiate to return every 5 seconds, instead of every frame. How do I go about doing that?

Thank you so much everyone!


Vector3.Distance (currentPosition, rightEdge) <= 0


Vector3.Distance will never return a value less than zero. Maybe you want to be comparing the X coordinate instead of the entire vector?


new Vector3 (transform.position.x, -3.5f, transform.position.y)


Why are you putting a Y coordinate in the Z parameter?

I want instantiate to return every 5 seconds, instead of every frame.


I would make a variable that stores the next point in time where instantiation is allowed. When you try to instantiate, check that (now >= next allowed time). When that is true, instantiate and set the variable to (current time + 5 seconds).
Advertisement


Vector3.Distance (currentPosition, rightEdge) <= 0

Vector3.Distance will never return a value less than zero. Maybe you want to be comparing the X coordinate instead of the entire vector?


new Vector3 (transform.position.x, -3.5f, transform.position.y)

Why are you putting a Y coordinate in the Z parameter?

I want instantiate to return every 5 seconds, instead of every frame.


I would make a variable that stores the next point in time where instantiation is allowed. When you try to instantiate, check that (now >= next allowed time). When that is true, instantiate and set the variable to (current time + 5 seconds).

Hi Nypyren. Thank you.

Yes, if I am trying to compare just the x-coordinates, how do I do it? or is there a more "elegant" way of making an object move left and right without moving them out of the game space.

Ops sorry.yes the 2nd one was a typo. I meant to type transform.position.z

I am sorry, for your last part, how do I set the next allowed time? Can you provide an example?

thank you so much for your help.

  1. GameObject beam = Instantiate (projectilesPrefab, new Vector3 (transform.position.x, -3.5f, transform.position.y), Quaternion.identity) as GameObject;
  2. transform,position += Vector3.up * speed * Time.deltaTime;

You're not setting the beam's transform's position in that second line, is that what you're referring to?

Hi ferrous! Again. thank you so much for your reply

  1. GameObject beam = Instantiate (projectilesPrefab, new Vector3 (transform.position.x, -3.5f, transform.position.y), Quaternion.identity) as GameObject;
  2. transform,position += Vector3.up * speed * Time.deltaTime;

You're not setting the beam's transform's position in that second line, is that what you're referring to?

Sorry i forgot to add it in when I typed here.

I did


beam.transform,position += Vector3.up * speed * Time.deltaTime;

but the projectile doesn't move up at all.

I had to change it to a rigidbody2d.velocity. and it solve my problem.

I am just very curious why can't I do a "transform" on a "instantiate"-ed object, (if that make sense)

thank you!

Yes, if I am trying to compare just the x-coordinates, how do I do it? or is there a more "elegant" way of making an object move left and right without moving them out of the game space.

It's the same fundamental problem as a 2d game, you want to prevent something from moving out of bounds. The only two logical ways to do that are to move them out of bounds and then snap them back in, or determine how far you are going to move and then only move them by that amount.

The problem with using distance is that distance is magnitude. If your "right game bound" is right of your character, positive on the x axis, then as you approach it the vector will shrink until you move past it. Then it will be a vector pointing from your character to the bound still, except the bound is in the opposite direction, so the distance will start growing again. He suggested you compare the x coordinates of the vectors instead. if(vec1.x > rightVec.x) kind of thing.

I am sorry, for your last part, how do I set the next allowed time? Can you provide an example?

You could either pass a delta time between frames or have some code that takes delta times and applies it to a "running time" variable that you can compare against. Once enough time has elapsed you run the code. Just don't use something like system time since that usually can be changed.
Advertisement

Hi ferrous! Again. thank you so much for your reply

  1. GameObject beam = Instantiate (projectilesPrefab, new Vector3 (transform.position.x, -3.5f, transform.position.y), Quaternion.identity) as GameObject;
  2. transform,position += Vector3.up * speed * Time.deltaTime;

You're not setting the beam's transform's position in that second line, is that what you're referring to?

Sorry i forgot to add it in when I typed here.

I did


beam.transform,position += Vector3.up * speed * Time.deltaTime;

but the projectile doesn't move up at all.

I had to change it to a rigidbody2d.velocity. and it solve my problem.

I am just very curious why can't I do a "transform" on a "instantiate"-ed object, (if that make sense)

thank you!

Yeah.... I think the problem is that your assuming the transform will be applied every frame? That's why changing that to a rigidbody and changing the velocity worked for you. Velocity makes the 'beam' object move every frame. That's probably what you want, but if for some reason you wanted the object to move by hand, you'd be best off creating a new script that is on the beam object, lets say, "BeamMoverScript", and in it's Update() method, it would do that this.transform.position+= Vector3.up * speed * Time.deltaTime;

A transform is not re-applied every frame, it's more like a map of how that thing is laid out on the screen / in the hierarchy of objects.

Yes, if I am trying to compare just the x-coordinates, how do I do it? or is there a more "elegant" way of making an object move left and right without moving them out of the game space.

It's the same fundamental problem as a 2d game, you want to prevent something from moving out of bounds. The only two logical ways to do that are to move them out of bounds and then snap them back in, or determine how far you are going to move and then only move them by that amount.

The problem with using distance is that distance is magnitude. If your "right game bound" is right of your character, positive on the x axis, then as you approach it the vector will shrink until you move past it. Then it will be a vector pointing from your character to the bound still, except the bound is in the opposite direction, so the distance will start growing again. He suggested you compare the x coordinates of the vectors instead. if(vec1.x > rightVec.x) kind of thing.

I am sorry, for your last part, how do I set the next allowed time? Can you provide an example?

You could either pass a delta time between frames or have some code that takes delta times and applies it to a "running time" variable that you can compare against. Once enough time has elapsed you run the code. Just don't use something like system time since that usually can be changed.

Thank you so much for the reply.

Understood. I manage to use Mathf.Clamp to do the job.

Actually, I have no codes related to time on my scripts. I probably will have to work on that. The only "Time" that I use is Time.deltaTime.

This topic is closed to new replies.

Advertisement