- Home /
Question by
OwnDemise · Apr 14, 2020 at 02:19 PM ·
c#transform.positiontransform.translatetime.time
noobNeedsHelp with writing script for movement
Hi all, visually here is what i'm trying to do:
1: From the ' X ' my Object moves horizontally to the wall on the left. It collides with the wall that I've tagged 'SheetEnd'.
I want to have the object (the dark blue piece) reset its transform.position back to X, where it repeats motion #1. I can do this, but having it continue the movement will not work if I use time.Time.
I'll add the code...I feel i'm close...and am over-complicating this....any hints... or right directions would help.
void Start()
{
initialPosition = transform.position;
}
// Update is called once per frame
void Update()
{
//make a method that moves the MusicGuide from left to right
if (state == State.Moving)
{
ScanLeftRight();
}
if (state == State.FinishedMoving)
{
print("DING!");
// Reset position and timer (if that's possible)
// Change state back to Moving.
}
}
public void ScanLeftRight()
{
float timer = 0f;
timer += Time.time;
Vector3 musicGuidePosition = initialPosition + (vectorMagnitude * multiplier * (timer));
transform.position = musicGuidePosition;
// Ignore this code, was playing around with Sin() //
//take an initial transform position and add an offset with respect to time.
//float cycle = Time.time / period;
//float tau = 2 * Mathf.PI;
//float oscillatingVector = Mathf.Sin(cycle * tau) / 2 + 0.5f;
//print(oscillatingVector);
//Vector3 musicGuidePosition = initialPosition - (vectorMagnitude * oscillatingVector);
// update object's position so that it will move when game is running.
//transform.position = musicGuidePosition;
}
private void ResetPosition()
{
if (state == State.FinishedMoving)
{
transform.position = new Vector3(8f, 10f, 4f);
print("Reset Position " + transform.position);
}
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "SheetEnd") // I have used the same tag that you attached to your enemy character
{
Debug.Log("Code made it this far... tagged ->");
state = State.FinishedMoving;
//reset position back to initial starting point once it reaches set distance
ResetPosition();
}
} }
question.jpg
(51.7 kB)
Comment
Your answer
Follow this Question
Related Questions
changing x multiplies x,y and z position for no reason 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Rotate Parent using Child as Pivot 0 Answers