Advertisement
jemongolfin98

TestPlayerMovement

Sep 24th, 2024
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.63 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using UnityEngine.SocialPlatforms.Impl;
  6. using UnityEngine.UI;
  7.  
  8. public class SidewaysMovementTest01 : MonoBehaviour
  9. {
  10.     private Rigidbody2D rb2d;
  11.     public GameObject testPlayer;
  12.  
  13.     public float switchSpeed = 5;
  14.     private float acc = 0.005f;
  15.    
  16.     private bool switchLeft = false;
  17.     private bool switchRight = false;
  18.  
  19.     public Transform point01;
  20.     public Transform point02;
  21.     public Transform point03;
  22.  
  23.     public bool atPoint01 = false;
  24.     public bool atPoint02 = false;
  25.     public bool atPoint03 = false;
  26.  
  27.    
  28.    
  29.    
  30.     // Start is called before the first frame update
  31.     void Start()
  32.     {
  33.         rb2d = transform.GetComponent<Rigidbody2D>();
  34.     }
  35.  
  36.     // Update is called once per frame
  37.     void Update()
  38.     {
  39.        
  40.     }
  41.  
  42.     void FixedUpdate()
  43.     {
  44.         // Test 01
  45.         if (atPoint01 == true)
  46.         {
  47.            if (switchLeft == false && switchRight == true)
  48.             {
  49.                 // remember, 10 - 5 is 5, so target - position is always your direction.
  50.                 Vector3 dir = point02.position - transform.position;
  51.            
  52.                 // magnitude is the total length of a vector.
  53.                 // getting the magnitude of the direction gives us the amount left to move
  54.                 float dist = dir.magnitude;
  55.            
  56.                 // this makes the length of dir 1 so that you can multiply by it.
  57.                 dir = dir.normalized;
  58.            
  59.                 // the amount we can move this frame
  60.                 float move = switchSpeed * Time.deltaTime;
  61.            
  62.                 // limit our move to what we can travel.
  63.                 if(move > dist) move = dist;
  64.            
  65.                 // apply the movement to the object.
  66.                 transform.Translate( dir * move);
  67.             }
  68.         }
  69.         else if (atPoint02 == true)
  70.         {
  71.             if (switchLeft == true && switchRight == false)
  72.             {
  73.                 // remember, 10 - 5 is 5, so target - position is always your direction.
  74.                 Vector3 dir = point01.position - transform.position;
  75.            
  76.                 // magnitude is the total length of a vector.
  77.                 // getting the magnitude of the direction gives us the amount left to move
  78.                 float dist = dir.magnitude;
  79.            
  80.                 // this makes the length of dir 1 so that you can multiply by it.
  81.                 dir = dir.normalized;
  82.            
  83.                 // the amount we can move this frame
  84.                 float move = switchSpeed * Time.deltaTime;
  85.            
  86.                 // limit our move to what we can travel.
  87.                 if(move > dist) move = dist;
  88.            
  89.                 // apply the movement to the object.
  90.                 transform.Translate( dir * move);
  91.             }
  92.             else if (switchLeft == false && switchRight == true)
  93.             {
  94.                 // remember, 10 - 5 is 5, so target - position is always your direction.
  95.                 Vector3 dir = point03.position - transform.position;
  96.            
  97.                 // magnitude is the total length of a vector.
  98.                 // getting the magnitude of the direction gives us the amount left to move
  99.                 float dist = dir.magnitude;
  100.            
  101.                 // this makes the length of dir 1 so that you can multiply by it.
  102.                 dir = dir.normalized;
  103.            
  104.                 // the amount we can move this frame
  105.                 float move = switchSpeed * Time.deltaTime;
  106.            
  107.                 // limit our move to what we can travel.
  108.                 if(move > dist) move = dist;
  109.            
  110.                 // apply the movement to the object.
  111.                 transform.Translate( dir * move);
  112.             }
  113.         }
  114.         else if (atPoint03 == true)
  115.         {
  116.             if (switchLeft == true && switchRight == false)
  117.             {
  118.                 // remember, 10 - 5 is 5, so target - position is always your direction.
  119.                 Vector3 dir = point02.position - transform.position;
  120.            
  121.                 // magnitude is the total length of a vector.
  122.                 // getting the magnitude of the direction gives us the amount left to move
  123.                 float dist = dir.magnitude;
  124.            
  125.                 // this makes the length of dir 1 so that you can multiply by it.
  126.                 dir = dir.normalized;
  127.            
  128.                 // the amount we can move this frame
  129.                 float move = switchSpeed * Time.deltaTime;
  130.            
  131.                 // limit our move to what we can travel.
  132.                 if(move > dist) move = dist;
  133.            
  134.                 // apply the movement to the object.
  135.                 transform.Translate( dir * move);
  136.             }
  137.         }
  138.     }
  139.  
  140.     public void SwitchLeft()
  141.     {
  142.         switchLeft = true;
  143.         switchRight = false;
  144.     }
  145.  
  146.     public void SwitchRight()
  147.     {
  148.         switchLeft = false;
  149.         switchRight = true;
  150.     }
  151.  
  152.     private void OnTriggerEnter2D(Collider2D other)
  153.     {
  154.         if (other.CompareTag("Point01"))
  155.         {
  156.             atPoint01 = true;
  157.             atPoint02 = false;
  158.             atPoint03 = false;
  159.         }
  160.         else if (other.CompareTag("Point02"))
  161.         {
  162.             atPoint01 = false;
  163.             atPoint02 = true;
  164.             atPoint03 = false;
  165.         }
  166.         else if (other.CompareTag("Point03"))
  167.         {
  168.             atPoint01 = false;
  169.             atPoint02 = false;
  170.             atPoint03 = true;
  171.         }
  172.     }
  173. }
  174.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement