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

[SOLVED]Move the player to the location

Discussion in 'Scripting' started by VisualTech48, Jul 4, 2014.

  1. VisualTech48

    VisualTech48

    Joined:
    Aug 23, 2013
    Posts:
    247
    Hi, I want to make my player, let's say 'glide' to some location. Not just appear. I want him to move his transform.position to a coliders transform.position, but with a transition.

    Current code: [It's probably very bad or wrong]
    Code (javascript):
    1.  
    2.  function MovePlayer(dur : float, from : Vector3, to : Vector3)
    3. {
    4.     count += Time.deltaTime;
    5.     var lerp = count / dur;
    6.     PlayControler.transform.position = Vector3.Lerp(from, to, lerp);
    7.     if (PlayControler.transform.position == to) {
    8.         count = 0.0;
    9.   }
    10.  
     
  2. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Make MovePlayer a coroutine and yield until it reaches its destination.
     
  3. MischaWeerwag

    MischaWeerwag

    Joined:
    Jul 4, 2014
    Posts:
    9
    Try something like this:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class playercontroller : MonoBehaviour
    6. {
    7.     public float movementSpeed;
    8.     public float turningSpeed;
    9.  
    10.     public GameObject player;
    11.  
    12.     public Vector3 targetPosition;
    13.     public bool move = false;
    14.  
    15.     public GameObject pointer;
    16.  
    17.     void Start ()
    18.     {
    19.  
    20.     }
    21.  
    22.     void Update ()
    23.     {
    24.         //float horizontal = Input.GetAxis("Mouse X") * turningSpeed;
    25.         //if (Input.GetMouseButton(0))
    26.         //{
    27.         //    transform.Translate(Vector3.forward * Time.deltaTime * movementSpeed);
    28.         //    Screen.lockCursor = true;
    29.         //}
    30.         //else
    31.         //{
    32.         //    Screen.lockCursor = false;
    33.         //}
    34.         //transform.Rotate(0, horizontal, 0);
    35.  
    36.         //float horizontal = Input.GetAxis("Mouse X") * turningSpeed;
    37.         if (Input.GetMouseButtonUp(0))
    38.         {
    39.             var ground = new Plane(Vector3.up, new Vector3(transform.position.x, 0, transform.position.z));
    40.             var hitdist = 0.0f;
    41.  
    42.             var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    43.  
    44.             if (ground.Raycast(ray, out hitdist))
    45.             {
    46.                 targetPosition = ray.GetPoint(hitdist);
    47.                 targetPosition = new Vector3(targetPosition.x, 5.5f, targetPosition.z);
    48.  
    49.                 GameObject pntr = (GameObject)GameObject.Instantiate(pointer);
    50.                 pntr.transform.position = targetPosition;
    51.  
    52.                 move = true;
    53.             }
    54.         }      
    55.  
    56.         if (move)
    57.         {
    58.             var targetRotation = Quaternion.LookRotation(targetPosition - transform.position);
    59.             transform.rotation = targetRotation;
    60.  
    61.             transform.Translate(Vector3.forward * Time.deltaTime * movementSpeed);
    62.             //transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * movementSpeed);
    63.  
    64.             if (Vector3.Distance(transform.position, targetPosition) < 1.0f)
    65.             {
    66.                 move = false;
    67.             }
    68.         }
    69.     }
    70.  
    71. }
    72.  
    73.  
     
  4. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483

    What's the point in separating the actual moving from the input? It's completely superfluous. If you want separation, move it to another function and instead of setting move=true, call that function.

    Also, what on earth is
    Code (CSharp):
    1. GameObject pntr = (GameObject)GameObject.Instantiate(pointer);
    2.                 pntr.transform.position = targetPosition;
    even doing?
     
  5. VisualTech48

    VisualTech48

    Joined:
    Aug 23, 2013
    Posts:
    247
    Thank you all, but the problem isn't moveing the player. What I want to is a smooth teleport for the player. The player movment is locked when its doing it, but I want him to move from 1 point (Wherever he is at the time) to the point (center) of the Colider gameobject.
     
  6. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    What's a "smooth" teleport? Do you mean some sort of alpha fading of the renderer so it doesn't just pop into existence?
     
  7. MischaWeerwag

    MischaWeerwag

    Joined:
    Jul 4, 2014
    Posts:
    9
    Gets the job done though. I dont like coroutines for that kind of stuff.
     
  8. VisualTech48

    VisualTech48

    Joined:
    Aug 23, 2013
    Posts:
    247
    What I mean, is that it does't insta teleport to the location, it just like smoothly goes to that location.
     
  9. VisualTech48

    VisualTech48

    Joined:
    Aug 23, 2013
    Posts:
    247
    This is what I mean
     

    Attached Files:

  10. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    But then that IS a problem of moving the player
     
  11. VisualTech48

    VisualTech48

    Joined:
    Aug 23, 2013
    Posts:
    247
    You are right, I wrongly said it. I just need that move. The Location of the player must smoothly match the location of the other box.
     
  12. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    MischaWeerang's code will work fine for what you need, all the code is there, you just need to choose which parts you do not want.
     
  13. MischaWeerwag

    MischaWeerwag

    Joined:
    Jul 4, 2014
    Posts:
    9
    1. Code (csharp):
      1.  
      2. using UnityEngine;
      3. using System.Collections;
      4.  
      5. public class playercontroller : MonoBehaviour
      6. {
      7.     public float movementSpeed;
      8.     public GameObject player;
      9.     public Vector3 targetPosition;
      10.  
      11.     private bool move = false;
      12.  
      13.     void Start ()
      14.     {
      15.  
      16.     }
      17.  
      18.     void Update ()
      19.     {
      20.         if (Input.GetMouseButtonUp(0))
      21.         {
      22.             var ground = new Plane(Vector3.up, new Vector3(transform.position.x, 0, transform.position.z));
      23.             var hitdist = 0.0f;
      24.  
      25.             var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
      26.  
      27.             if (ground.Raycast(ray, out hitdist))
      28.             {
      29.                 targetPosition = ray.GetPoint(hitdist);
      30.                 targetPosition = new Vector3(targetPosition.x, 5.5f, targetPosition.z);
      31.  
      32.                 move = true;
      33.             }
      34.         }      
      35.  
      36.         if (move)
      37.         {
      38.             var targetRotation = Quaternion.LookRotation(targetPosition - transform.position);
      39.             transform.rotation = targetRotation;
      40.  
      41.             transform.Translate(Vector3.forward * Time.deltaTime * movementSpeed);
      42.  
      43.             if (Vector3.Distance(transform.position, targetPosition) < 1.0f)
      44.             {
      45.                 move = false;
      46.             }
      47.         }
      48.     }
      49.  
      50. }
      51.  
      This will work out of the box. add the script to your 'player'. in the inspector set a movementspeed, add the object to move (itself probably), and click on the screen to see your box move to the position you clicked.
      Notthe prettiest code though...
     
    VisualTech48 likes this.
  14. VisualTech48

    VisualTech48

    Joined:
    Aug 23, 2013
    Posts:
    247
    I'll work it around, thank you for the code. Thank you all for help. Cheers !
     
  15. VisualTech48

    VisualTech48

    Joined:
    Aug 23, 2013
    Posts:
    247
    Still can't get it to work properly, and it's writen in csharp, which is hard for me to translate to Java. I don't know how this works on the player, when he even isn't declared in this script.