Advertisement
jemongolfin98

TestPlayerMovement02 (updated 9-29-24 1455)

Sep 28th, 2024 (edited)
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.01 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 SidewaysMovementTest02 : MonoBehaviour
  9. {
  10.     private Rigidbody2D rb2d;
  11.     public GameObject testPlayer;
  12.  
  13.     public float switchSpeed = 5;
  14.     private float acc = 0.005f;
  15.  
  16.  
  17.     public Transform[] points;
  18.  
  19.     int currentPosition;
  20.     int destinationPosition;
  21.  
  22.    
  23.    
  24.    
  25.     // Start is called before the first frame update
  26.     void Start()
  27.     {
  28.         rb2d = transform.GetComponent<Rigidbody2D>();
  29.         currentPosition = 1;
  30.         destinationPosition = 1;
  31.     }
  32.  
  33.     // Update is called once per frame
  34.     void Update()
  35.     {
  36.        
  37.     }
  38.  
  39.     void FixedUpdate()
  40.     {
  41.         Debug.Log($"currentPosition: {currentPosition}, destinationPosition: {destinationPosition}");
  42.         //Debug.Log(dir);
  43.        
  44.         // Test 02
  45.         if (currentPosition == destinationPosition)
  46.         {
  47.  
  48.         }
  49.         else if (currentPosition != destinationPosition)
  50.         {
  51.             // remember, 10 - 5 is 5, so target - position is always your direction.
  52.                 Vector3 dir = points[destinationPosition].position - transform.position;
  53.            
  54.                 // magnitude is the total length of a vector.
  55.                 // getting the magnitude of the direction gives us the amount left to move
  56.                 float dist = dir.magnitude;
  57.            
  58.                 // this makes the length of dir 1 so that you can multiply by it.
  59.                 dir = dir.normalized;
  60.            
  61.                 // the amount we can move this frame
  62.                 float move = switchSpeed * Time.deltaTime;
  63.            
  64.                 // limit our move to what we can travel.
  65.                 if (move > dist)
  66.                 {
  67.                     move = dist;
  68.                     currentPosition = destinationPosition;
  69.                 }
  70.            
  71.                 // apply the movement to the object.
  72.                 transform.Translate( dir * move);
  73.  
  74.                 Debug.Log(dir);
  75.         }
  76.  
  77.         //Debug.Log($"currentPosition: {currentPosition}, destinationPosition: {destinationPosition}, directionNormalized: {dir.normalized}");
  78.     }
  79.  
  80.     public void SwitchLeft()
  81.     {
  82.         if (destinationPosition > 0 && destinationPosition <= 2)
  83.         {
  84.             destinationPosition--;
  85.         }
  86.     }
  87.  
  88.     public void SwitchRight()
  89.     {
  90.         if (destinationPosition >= 0 && destinationPosition < 2)
  91.         {
  92.             destinationPosition++;
  93.         }
  94.     }
  95.  
  96.     // private void OnTriggerEnter2D(Collider2D other)
  97.     // {
  98.     //     if (other.CompareTag("Point01"))
  99.     //     {
  100.     //         currentPosition = 0;
  101.     //     }
  102.     //     else if (other.CompareTag("Point02"))
  103.     //     {
  104.     //         currentPosition = 1;
  105.     //     }
  106.     //     else if (other.CompareTag("Point03"))
  107.     //     {
  108.     //         currentPosition = 2;
  109.     //     }
  110.     // }
  111. }
  112.  
  113. }
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement