Search Unity

Unity 2D player looks at mouse pointer and when you press w you move toward it

Discussion in '2D' started by LagoGames, Oct 22, 2019.

  1. LagoGames

    LagoGames

    Joined:
    May 19, 2019
    Posts:
    12
    Hey, I have been working on a script where your character can look at the mouse and will rotate with it and when you press w it will move towards it im having a lot of trouble what is the solution. it is for a Top-Down Shooter and this way I won't have to make a script for the gun aiming.
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,438
    Hi,

    can you show your script so far? (and what is the problem with it)
     
    vakabaka likes this.
  3. LagoGames

    LagoGames

    Joined:
    May 19, 2019
    Posts:
    12
    yes I can. I have school so I can send it later today
     
  4. LagoGames

    LagoGames

    Joined:
    May 19, 2019
    Posts:
    12
    {
    public float speed = 1.5f;
    private Rigidbody2D rb;
    private Vector3 target;
    private Vector2 moveAmount;
    void Start()
    {
    target = transform.position;
    rb = GetComponent<Rigidbody2D>();
    }
    void Update()
    {
    Vector3 mouseScreen = Input.mousePosition; Vector3 mouse = Camera.main.ScreenToWorldPoint(mouseScreen); transform.rotation = Quaternion.Euler(0, 0, Mathf.Atan2(mouse.y - transform.position.y, mouse.x - transform.position.x) * Mathf.Rad2Deg - 90);
    if (Input.GetKeyDown(KeyCode.W))
    {
    target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    target.z = transform.position.z;
    moveAmount = target.normalized * speed;
    transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);

    }

    }
    private void FixedUpdate()
    {
    rb.MovePosition(rb.position + moveAmount * Time.fixedDeltaTime);
    }
    }

    Here is the script the problem is you have to individually press w every time you cant hold it down to move
     
  5. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    try
    Code (CSharp):
    1. if (Input.GetKey(KeyCode.W))
     
  6. LagoGames

    LagoGames

    Joined:
    May 19, 2019
    Posts:
    12
    thanks I will
     
  7. LagoGames

    LagoGames

    Joined:
    May 19, 2019
    Posts:
    12
    I put in the code and it fixed my problem but now the character won't stop moving what do I do?
     
  8. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    Code (CSharp):
    1. if (Input.GetKeyDown(KeyCode.W))
    2. {
    3. target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    4. target.z = transform.position.z;
    5. moveAmount = target.normalized * speed;
    6. transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
    7. }
    8.  
    9. if (Input.GetKeyUp(KeyCode.W))
    10. {
    11. moveAmount = Vector2.zero;
    12. }
    13. {
    14.  
    still I would use Input.GetAxis for the movement
    https://docs.unity3d.com/ScriptReference/Input.GetAxis.html

    maybe something is wrong here, just as example
    Code (CSharp):
    1.  
    2. float move;
    3. //...
    4. move = Input.GetAxis ("Vertical");
    5. if (move >= 0)
    6. {
    7. target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    8. target.z = transform.position.z;
    9. moveAmount = target.normalized * speed;
    10. transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
    11.  
    12. }
    13.  
    14. }
    15. private void FixedUpdate()
    16. {
    17. rb.MovePosition(rb.position + move * moveAmount * Time.fixedDeltaTime);
    18. }
    19. }
     
    Last edited: Oct 24, 2019
  9. LagoGames

    LagoGames

    Joined:
    May 19, 2019
    Posts:
    12

    I put in the code but for some reason, it is doing the opposite when I press w and s the character stops and it automatically starts
    Code (CSharp):
    1. public class playermovement : MonoBehaviour
    2. {
    3.     public float speed = 1.5f;
    4.     private Rigidbody2D rb;
    5.     private Vector3 target;
    6.     float move;
    7.  
    8.     private Vector2 moveAmount;
    9.  
    10.     private void FixedUpdate()
    11.     {
    12.         rb.MovePosition(rb.position + move * moveAmount * Time.fixedDeltaTime);
    13.     }
    14.  
    15.     void Start()
    16.     {
    17.         target = transform.position;
    18.         rb = GetComponent<Rigidbody2D>();
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.         Vector3 mouseScreen = Input.mousePosition; Vector3 mouse = Camera.main.ScreenToWorldPoint(mouseScreen); transform.rotation = Quaternion.Euler(0, 0, Mathf.Atan2(mouse.y - transform.position.y, mouse.x - transform.position.x) * Mathf.Rad2Deg - 90);
    24.  
    25.         move = Input.GetAxis("Vertical");
    26.        
    27.         if (move >= 0)
    28.      
    29.         {
    30.             target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    31.             target.z = transform.position.z;
    32.             moveAmount = target.normalized * speed;
    33.             transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
    34.         }
    35.  
    36.  
    37.         else
    38.         {
    39.             moveAmount = Vector2.zero;
    40.         }
    41.  
    42.         }
    43.  
    44.  
    45.  
    46.    
    47.  
    48.  
    49.  
    50.    
    51.     }
    52.  
    53.  
    54.  
    55.  
    56.  
    57.  
     
  10. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    sorry :) I have just copypasted your script, without to look at the other things. There are two different moves in the script.
    1.
    Code (CSharp):
    1. transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
    2.
    Code (CSharp):
    1. rb.MovePosition(rb.position + move * moveAmount * Time.fixedDeltaTime);
    you should disable one of them
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class playermovement : MonoBehaviour
    6. {
    7.     public float speed = 1.5f;
    8.     private Rigidbody2D rb;
    9.     private Vector3 target;
    10.     float move;
    11.  
    12.     private Vector2 moveAmount;
    13.  
    14.     void Start()
    15.     {
    16.         target = transform.position;
    17.         rb = GetComponent<Rigidbody2D>();
    18.     }
    19.  
    20.     void Update()
    21.     {
    22.         Vector3 mouseScreen = Input.mousePosition; Vector3 mouse = Camera.main.ScreenToWorldPoint(mouseScreen);
    23.         transform.rotation = Quaternion.Euler(0, 0, Mathf.Atan2(mouse.y - transform.position.y, mouse.x - transform.position.x) * Mathf.Rad2Deg - 90);
    24.  
    25.         move = Input.GetAxis("Vertical");
    26.  
    27.         if (move >= 0)
    28.  
    29.         {
    30.             target = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    31.             target.z = transform.position.z;
    32.             moveAmount = target.normalized * speed;
    33.         }
    34.         else
    35.         {
    36.             moveAmount = Vector2.zero;
    37.         }
    38.     }
    39.  
    40.     private void FixedUpdate()
    41.     {
    42.         rb.MovePosition(rb.position + move * moveAmount * Time.fixedDeltaTime);
    43.     }
    44. }
     
    Last edited: Oct 24, 2019
  11. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    I would do:
    if without physics (W button as example)
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MoveToMouse : MonoBehaviour
    6. {
    7.     public float speed;
    8.  
    9.     private void Update()
    10.     {
    11.         Rotate();
    12.  
    13.         if (Input.GetKey(KeyCode.W))
    14.         {
    15.             Move();
    16.         }
    17.     }
    18.  
    19.     void Rotate ()
    20.     {
    21.         Vector2 directionToMouse = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    22.         transform.up = directionToMouse.normalized;
    23.     }
    24.  
    25.     void Move()
    26.     {
    27.         transform.Translate(transform.up * speed * Time.deltaTime, Space.World);
    28.     }
    29. }
    with rigidbody2D and MovePosition (not sure about it, I have never used moveposition)
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class OtherMoveToMouse : MonoBehaviour
    6. {
    7.     public float speed;
    8.     Rigidbody2D rb;
    9.     float move;
    10.     Vector2 directionToMouse;
    11.  
    12.     private void Start()
    13.     {
    14.         rb = GetComponent<Rigidbody2D>();
    15.     }
    16.     private void Update()
    17.     {
    18.         Rotate();
    19.         move = Input.GetAxis("Vertical");
    20.     }
    21.  
    22.     private void FixedUpdate()
    23.     {
    24.         //if the back moving isn't needed
    25.         if (move >= 0)
    26.         {
    27.             rb.MovePosition(rb.position + directionToMouse.normalized * move * speed * Time.fixedDeltaTime);
    28.         }
    29.     }
    30.  
    31.     void Rotate()
    32.     {
    33.         directionToMouse = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    34.         transform.up = directionToMouse.normalized;
    35.     }
    36. }
    37.  
    with velocity
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class OtherMoveToMouseWithVelocity : MonoBehaviour
    6. {
    7.     public float speed;
    8.     Rigidbody2D rb;
    9.     float move;
    10.     Vector2 directionToMouse;
    11.  
    12.     private void Start()
    13.     {
    14.         rb = GetComponent<Rigidbody2D>();
    15.     }
    16.     private void Update()
    17.     {
    18.         Rotate();
    19.         move = Input.GetAxis("Vertical");
    20.     }
    21.  
    22.     private void FixedUpdate()
    23.     {
    24.         //if the back moving isn't needed
    25.         if (move >= 0)
    26.         {
    27.             rb.velocity = (Vector2) directionToMouse.normalized * move * speed;
    28.         }
    29.     }
    30.  
    31.     void Rotate()
    32.     {
    33.         directionToMouse = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    34.         transform.up = directionToMouse.normalized;
    35.     }
    36. }
    37.  
    Code (CSharp):
    1.  transform.up = directionToMouse.normalized;
    this line is depended on the sprite look direction (maybe you will use transform.right, -transform.up, -transform.right)
     
    Last edited: Oct 24, 2019