Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Events;
- using UnityEngine.SocialPlatforms.Impl;
- using UnityEngine.UI;
- public class SidewaysMovementTest02 : MonoBehaviour
- {
- private Rigidbody2D rb2d;
- public GameObject testPlayer;
- public float switchSpeed = 5;
- private float acc = 0.005f;
- public Transform[] points;
- int currentPosition;
- int destinationPosition;
- // Start is called before the first frame update
- void Start()
- {
- rb2d = transform.GetComponent<Rigidbody2D>();
- currentPosition = 1;
- destinationPosition = 1;
- }
- // Update is called once per frame
- void Update()
- {
- }
- void FixedUpdate()
- {
- Debug.Log($"currentPosition: {currentPosition}, destinationPosition: {destinationPosition}");
- //Debug.Log(dir);
- // Test 02
- if (currentPosition == destinationPosition)
- {
- }
- else if (currentPosition != destinationPosition)
- {
- // remember, 10 - 5 is 5, so target - position is always your direction.
- Vector3 dir = points[destinationPosition].position - transform.position;
- // magnitude is the total length of a vector.
- // getting the magnitude of the direction gives us the amount left to move
- float dist = dir.magnitude;
- // this makes the length of dir 1 so that you can multiply by it.
- dir = dir.normalized;
- // the amount we can move this frame
- float move = switchSpeed * Time.deltaTime;
- // limit our move to what we can travel.
- if (move > dist)
- {
- move = dist;
- currentPosition = destinationPosition;
- }
- // apply the movement to the object.
- transform.Translate( dir * move);
- Debug.Log(dir);
- }
- //Debug.Log($"currentPosition: {currentPosition}, destinationPosition: {destinationPosition}, directionNormalized: {dir.normalized}");
- }
- public void SwitchLeft()
- {
- if (destinationPosition > 0 && destinationPosition <= 2)
- {
- destinationPosition--;
- }
- }
- public void SwitchRight()
- {
- if (destinationPosition >= 0 && destinationPosition < 2)
- {
- destinationPosition++;
- }
- }
- // private void OnTriggerEnter2D(Collider2D other)
- // {
- // if (other.CompareTag("Point01"))
- // {
- // currentPosition = 0;
- // }
- // else if (other.CompareTag("Point02"))
- // {
- // currentPosition = 1;
- // }
- // else if (other.CompareTag("Point03"))
- // {
- // currentPosition = 2;
- // }
- // }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement