Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Issues with moving towards an object?

Discussion in 'Scripting' started by androidac67, Jul 13, 2023.

  1. androidac67

    androidac67

    Joined:
    Jan 3, 2023
    Posts:
    75
    I have a Raycast that detects an object directly in front of me and if there is a hit and my player is in the trigger area, I can press a key to have my player rotate towards a target object.

    However, because the Raycast distance is extremely short, my player is basically standing right next to the object which is good. So is there a way that I can somehow "glue" or stick my player to that target object either before or at the same time that I am rotating with the target object?

    I thought that my trigger below would "glue" the player to the target object, but that is not the case. The reason that I have been trying to implement it is because there is a big issue where if I run past the trigger at the same time as I press my input key, I will still rotate despite being no where near the target object or the Raycast hit distance.

    Code (CSharp):
    1. Transform target;
    2. float speed;
    3.  
    4. void OnTriggerEnter(Collider col)
    5. {  
    6.     if (Physics.Raycast(transform.position, transform.forward, 1.5f))
    7.     {
    8.         canRotate = true;
    9.         Debug.Log("Raycast forward");      
    10.     }
    11. }
    12.  
    13. void Update()
    14. {        
    15.     if (canRotate == true)
    16.     {
    17.         if (Input.GetKeyDown(KeyCode.F))
    18.         {    
    19.             var step = speed * Time.deltaTime;
    20.             transform.rotation = Quaternion.RotateTowards(transform.rotation, target.rotation, step);
    21.             // Glue player to object
    22.         }  
    23.            
    24.     }
    25. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    What do you mean by "glue?" Do you mean kinda like a Dark Souls enemy lock-on so once it is activated you always face it?

    If so there's tons of tutorials for that kinda setup... it's sorta what you have above but with a little bit of extra programming to decide if you are locked on or not.
     
  3. androidac67

    androidac67

    Joined:
    Jan 3, 2023
    Posts:
    75
    Like for example in games when there is an interact option for a ladder such as "Press F to climb" in a small radius around the ladder and you press the specific key, your camera will rotate towards the ladder and the player itself will also move towards that ladder (to close the gap between the player and ladder if there is one), and you cannot do any other movements (no WASD movement, etc) until you press a different key to get out of the climbing action.

    I just don't know how they achieve the "move towards" part. I know there is Vector3.MoveTowards but I'm not sure if that is applicable here because if I have multiple target objects, I'm not sure how I would get all the vectors of each object.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Oh yeah, gotcha... that's sort of just a stateful controller situation:

    one controller mode for normal

    another controller mode for on ladder

    And then the requisite accoutrement to switch states between those two, including any logic or animations that would be involved in switching states. Just go checkout tutorials for ladder climbing.
     
  5. androidac67

    androidac67

    Joined:
    Jan 3, 2023
    Posts:
    75
    Yeah thanks for the info. I've looked at many ladder climbing tutorials especially on youtube but from what I've seen unfortunately they don't cover that area. Could I just disable my main movement script and keep the script in my post enabled, then once rotation is complete, do the opposite?
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Wouldn't you just be on the ladder but with the walking script???

    Ideally the code you have above would be the thing that handles transitioning from walk controller to ladder controller and back... sort of like a "player movement mode manager."
     
  7. androidac67

    androidac67

    Joined:
    Jan 3, 2023
    Posts:
    75
    It's ok, I managed to solve the issue. I had to mess around with the raycasts, remove a specific raycast and now it actually works, detects properly and moves the way I want.