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 SidewaysMovementTest01 : MonoBehaviour
- {
- private Rigidbody2D rb2d;
- public GameObject testPlayer;
- public float switchSpeed = 5;
- private float acc = 0.005f;
- private bool switchLeft = false;
- private bool switchRight = false;
- public Transform point01;
- public Transform point02;
- public Transform point03;
- public bool atPoint01 = false;
- public bool atPoint02 = false;
- public bool atPoint03 = false;
- // Start is called before the first frame update
- void Start()
- {
- rb2d = transform.GetComponent<Rigidbody2D>();
- }
- // Update is called once per frame
- void Update()
- {
- }
- void FixedUpdate()
- {
- // Test 01
- if (atPoint01 == true)
- {
- if (switchLeft == false && switchRight == true)
- {
- // remember, 10 - 5 is 5, so target - position is always your direction.
- Vector3 dir = point02.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;
- // apply the movement to the object.
- transform.Translate( dir * move);
- }
- }
- else if (atPoint02 == true)
- {
- if (switchLeft == true && switchRight == false)
- {
- // remember, 10 - 5 is 5, so target - position is always your direction.
- Vector3 dir = point01.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;
- // apply the movement to the object.
- transform.Translate( dir * move);
- }
- else if (switchLeft == false && switchRight == true)
- {
- // remember, 10 - 5 is 5, so target - position is always your direction.
- Vector3 dir = point03.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;
- // apply the movement to the object.
- transform.Translate( dir * move);
- }
- }
- else if (atPoint03 == true)
- {
- if (switchLeft == true && switchRight == false)
- {
- // remember, 10 - 5 is 5, so target - position is always your direction.
- Vector3 dir = point02.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;
- // apply the movement to the object.
- transform.Translate( dir * move);
- }
- }
- }
- public void SwitchLeft()
- {
- switchLeft = true;
- switchRight = false;
- }
- public void SwitchRight()
- {
- switchLeft = false;
- switchRight = true;
- }
- private void OnTriggerEnter2D(Collider2D other)
- {
- if (other.CompareTag("Point01"))
- {
- atPoint01 = true;
- atPoint02 = false;
- atPoint03 = false;
- }
- else if (other.CompareTag("Point02"))
- {
- atPoint01 = false;
- atPoint02 = true;
- atPoint03 = false;
- }
- else if (other.CompareTag("Point03"))
- {
- atPoint01 = false;
- atPoint02 = false;
- atPoint03 = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement