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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Character goes to wrong direction after being instructed to go to xyz

Discussion in 'Scripting' started by xNihel, Oct 9, 2017.

  1. xNihel

    xNihel

    Joined:
    Oct 9, 2017
    Posts:
    8
    So here's a thing. We are doing a 2d game where we make a flag and character has to move to flag(ignore second target). But no matter where i click character goes only into one direction.
    Anyone here know how to fix this? I'm beginner in unity and i spent 2 days trying to do this properly.
    Here are codes:
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Flags{
    7.  
    8.     public float x;
    9.     public float y;
    10.     public float moveSpeed = 0.004f;
    11.     public Flags flag;
    12.  
    13.     public float X
    14.     {
    15.         get
    16.         {
    17.             return x;
    18.         }
    19.  
    20.         set
    21.         {
    22.             x = value;
    23.         }
    24.     }
    25.  
    26.     public float Y
    27.     {
    28.         get
    29.         {
    30.             return y;
    31.         }
    32.  
    33.         set
    34.         {
    35.             y = value;
    36.         }
    37.     }
    38.  
    39.     public Flags()
    40.     {
    41.  
    42.     }
    43.     public Flags(float x, float y)
    44.     {
    45.         this.x = X;
    46.         this.y = Y;
    47.         Debug.Log("Stworzono flage");
    48.     }
    49.  
    50.  }
    51.  
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class CharacterMovment : MonoBehaviour {
    7.  
    8.     public float speed = 1.5f;
    9.     private Vector3 target1;
    10.     private Vector3 target2;
    11.     Flags flag = new Flags(0,0);
    12.  
    13.     void Start()
    14.     {
    15.         target1 = transform.position;
    16.         target2 = transform.position;
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.  
    22.         if (Input.GetMouseButtonDown(0))
    23.         {
    24.             float x = Input.mousePosition.x;
    25.             float y = Input.mousePosition.y;
    26.             Flags flag = new Flags(x, y);
    27.             target1.x = flag.x;
    28.             target1.y = flag.y;
    29.             target1.z = transform.position.z;
    30.         }
    31.      
    32.         if (Input.GetMouseButtonDown(1))
    33.         {
    34.             float x = Input.mousePosition.x;
    35.             float y = Input.mousePosition.y;
    36.             Flags flag = new Flags(x, y);
    37.             target2.x = flag.x;
    38.             target2.y = flag.y;
    39.             target2.z = transform.position.z;
    40.         }
    41.  
    42.      
    43.  
    44.         transform.position = Vector3.MoveTowards(transform.position, target1, speed * Time.deltaTime);
    45.         //GameObject.Find("character (1)").transform.position szuka obiektu o nazwie character (1) i pobiera jego pozycje na osi xyz, jeśli jest takie samo jak target, czyli jeśli osiągnął pierwszą flage to idzie do drugiej.
    46.     }
    47. }
    48.  
    game is in 2d btw
     
    Last edited: Oct 9, 2017
  2. MathiasDG

    MathiasDG

    Joined:
    Jul 1, 2014
    Posts:
    114
    Input.mousePosition is in screen space.
    Transform.position is in world space

    Try using camera.ScreenToWorldPoint to convert your input.mousePosition into a world space coordinate.
     
    xNihel likes this.
  3. xNihel

    xNihel

    Joined:
    Oct 9, 2017
    Posts:
    8
    Thanks for help.
    We solved this in this way:
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class CharacterMovment : MonoBehaviour {
    7.    
    8.     public float speed = 5f;
    9.     private Vector3 target;
    10.     private bool collided;
    11.     private bool condition;
    12.  
    13.     void Start()
    14.     {
    15.         target = transform.position;
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.  
    21.         if (Input.GetMouseButtonDown(0))
    22.         {
    23.             target = Input.mousePosition;
    24.             target.z = 22.1f;
    25.             target = Camera.main.ScreenToWorldPoint(target);
    26.             Debug.Log(target);
    27.         }
    28.  
    29.         if (Vector3.Distance(target, transform.position) > 1)
    30.         {
    31.             transform.position = Vector3.Lerp(transform.position, target, speed * Time.deltaTime);
    32.         }
    33.     }
    34.  
    35. }
    36.  
     
    MathiasDG likes this.