Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to add a speed cap to an object that follows another object on the x axis? (C#)

Discussion in 'Scripting' started by Deleted User, Oct 18, 2017.

  1. Deleted User

    Deleted User

    Guest

    I have a code that makes a paddle follow a ball along the X-axis simply by knowing the ball's position. I planned to use this as a sort of "practice mode" for a breakout-style game but all it does is act like a wall, naturally. By that I mean it, no matter what, will be in the exact position of the ball, thus doing absolutely nothing and making it impossible to win.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AI_Paddle : MonoBehaviour {
    6.  
    7.     public bool autoPlay = true;
    8.  
    9.     private Ball ball;
    10.  
    11.     void Start()
    12.     {
    13.         ball = GameObject.FindObjectOfType<Ball>();
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update ()
    18.     {
    19.         AutoPlay();
    20.     }
    21.  
    22.     void AutoPlay()
    23.     {
    24.         Vector3 paddlePos = new Vector3(0.5f, this.transform.position.y, -1f);
    25.         Vector3 ballPos = ball.transform.position;
    26.         paddlePos.x = Mathf.Clamp(ballPos.x, 0.5f, 15.5f);
    27.         this.transform.position = paddlePos;
    28.     }
    29. }
    I would like to make it possible to beat the practice mode using something along the lines of a speed cap so the "practice paddle" can't be on the ball's exact position at higher speeds (I am open to suggestions of other ways) but, being a complete beginner, I have no clue how. I'm assuming this would require the entire AutoPlay() function?
     
  2. ZukleGugle

    ZukleGugle

    Joined:
    Oct 18, 2017
    Posts:
    1
    Last edited: Oct 18, 2017
  3. Deleted User

    Deleted User

    Guest

  4. McDev02

    McDev02

    Joined:
    Nov 22, 2010
    Posts:
    664
    You can do it like so:
    Code (CSharp):
    1. public class AI_Paddle : MonoBehaviour
    2. {
    3.     public bool autoPlay = true;
    4.  
    5.     public float MaxSpeed = 5;
    6.     private Ball ball;
    7.  
    8.     Vector3 targetPos;
    9.  
    10.     void Start()
    11.     {
    12.         ball = FindObjectOfType<Ball>();
    13.         targetPos = new Vector3(0.5f, 0, -1f);
    14.         transform.position = targetPos;
    15.     }
    16.    
    17.     void Update()
    18.     {
    19.         AutoPlay();
    20.     }
    21.  
    22.     void AutoPlay()
    23.     {
    24.         Vector3 ballPos = ball.transform.position;
    25.         targetPos.x = Mathf.Clamp(ball.transform.position.x, 0.5f, 15.5f);
    26.         var step = MaxSpeed * Time.deltaTime;
    27.         transform.position = Vector3.MoveTowards(transform.position, targetPos, step);
    28.     }
    29. }
     
  5. Deleted User

    Deleted User

    Guest

    Well, this is an interesting problem. This is the result of my script:
    1.PNG 2.PNG
    And this is the result of that one:
    3.PNG
    What the heck?
     
  6. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Something's gone wrong somewhere along the way. You can just use your original script in its entirety and change these lines:
    Code (csharp):
    1.  
    2. paddlePos.x = Mathf.Clamp(ballPos.x, 0.5f, 15.5f);
    3.  
    to:
    Code (csharp):
    1.  
    2. paddlePos.x = Mathf.Clamp(Mathf.MoveTowards(paddlePos.x, ballPos.x, 5.0f * Time.deltaTime), 0.5f, 15.5f);
    3.  
    Change 5.0f to whatever speed you want the paddle to move by.

    EDIT: You'll also need to set the x position of the paddle position instead of just the Y:
    Code (csharp):
    1.  
    2. Vector3 paddlePos = new Vector3(this.transform.position.x, this.transform.position.y, -1f);
    3.  
     
    Deleted User likes this.
  7. Deleted User

    Deleted User

    Guest

    Finally, it worked! Thanks for the help!