I’m fairly new to game creation, but thought I’d give it a try.
http://s29.postimg.org/tg048n2xz/Question1.png
I thought it would be better to draw a quick picture, with my question. I’m not too good at explaining in words.
Thanks,
Ann
I’m fairly new to game creation, but thought I’d give it a try.
http://s29.postimg.org/tg048n2xz/Question1.png
I thought it would be better to draw a quick picture, with my question. I’m not too good at explaining in words.
Thanks,
Ann
Which aspect are you struggling with? What code have you written?
We’ll be happy to help guide you but we’re not interested in writing whole scripts - you’ll never learn that way.
float timer;
Vector3 startPos;
Vector3 endPos;
void Start()
{
RandomPosition();
}
void RandomPosition()
{
timer = Time.time;
startPos = transform.position;
endPos = new Vector3(0, (startPos.y + 2.0f), 0);
}
void Update()
{
if (Input.GetKeyUp("space")) {
if (Time.time - timer > 1)
{
RandomPosition();
}
transform.position = Vector3.Lerp(startPos, endPos, Time.time - timer);
}
}
But this doesn’t move the object at a constant speed, 2.0f units up. And it’ll only move the object if 1 second is elapsed.
Instead of using Lerp, use Vector3.MoveTowards. Then you can set a constant speed.
You also probably want to move that line outside of the if statement (to the very bottom of Update), so it runs all the time instead of only when you release the spacebar.
you’re move code is within the scope controlled by the input check…i.e the object only moves in the frame the space bar is released.
move line 24 below the brace on line 25 for it to always move towards the end pos. This will allow the space to change the end position, you might want to add a check that the transform is within a small distance of the end pos if you want to make sure it makes it is close to the point before allowing another one to be set.
oh and adding an or clause (&& = and, || = or) of “or if there is no end pos” will allow it to work within the first second, be careful with the brackets on that though ![]()
I took your script and sorta changed it around a bit. I think this is closer to what you want. I put a sphere in my scene instead of a sprite but the point is the same. Here is the changed script, but the entire scene is attached too.
using UnityEngine;
using System.Collections;
public class RandomMover : MonoBehaviour
{
public float speed = 5.0f;
Vector3 goalPos;
void Start()
{
goalPos = transform.position;
}
void RandomPosition()
{
goalPos = new Vector3(0, (transform.position.y + 2.0f), 0);
}
void Update()
{
if (Input.GetKeyUp("space"))
{
RandomPosition();
}
Vector3 delta = goalPos - transform.position;
float step = speed * Time.deltaTime;
if (delta.magnitude >= step)
{
transform.position += delta.normalized * step;
}
}
}
The undefined behavior here is if you are already in transit, then press/release SPACE, it moves you to y = +2 from the precise place you were at that moment.
2226148–148268–Forum344214.unitypackage (4.07 KB)
Thanks, everyone for the help ![]()
This script works perfectly, but is there anyway to disable the spacebar while the object is moving?
Given that:
a) the object moves until it reaches its destination
b) we already have a check that checks if it still needs to keep moving
Then we can trivially make an else block that only ALLOWS the spacebar when you are “close enough” to the goal position… kinda like so:
void Update()
{
Vector3 delta = goalPos - transform.position;
float step = speed * Time.deltaTime;
if (delta.magnitude >= step)
{
transform.position += delta.normalized * step;
}
else
{
if (Input.GetKeyUp("space"))
{
RandomPosition();
}
}
}
thank you!!!