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

I need help

Discussion in '2D' started by Zamki, Jan 27, 2020.

  1. Zamki

    Zamki

    Joined:
    Jan 27, 2020
    Posts:
    4
    Hello i am Programming a simple 2D game for mobile devices with 2 Joysticks one for the rotation for the char and the other one for the movement i made a simple script for the rotation. But every time i start the game there is some wierd looking black line on the Screen.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Movement : MonoBehaviour
    {
    public float moveSpeed = 5f;
    public Joystick Joystickmovement;
    public Rigidbody2D rb;
    public Joystick Looking;
    Vector2 Rotation;
    Vector2 movement;
    public Rigidbody2D Kamera;

    // Update is called once per frame
    void Update()
    {
    //Input
    movement.x = Joystickmovement.Horizontal;
    movement.y = Joystickmovement.Vertical;
    Rotation.y = Looking.Horizontal;
    Rotation.x = Looking.Vertical * -1;

    }

    void FixedUpdate()
    {
    //movement
    rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);

    //Rotation
    Vector2 VirtualMouse = rb.position + Rotation;
    Vector2 lookdir = VirtualMouse - rb.position;
    float angle = Mathf.Atan2(lookdir.y, lookdir.x) * Mathf.Rad2Deg - 90f;
    rb.rotation = angle;
    Kamera.position = rb.position;
    }
    }
    Unbenannt.png
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Please use code-tags when posting code otherwise it's just a wall of text that's hard to read.

    Posting physics and movement code won't help as you're describing a rendering issue. Maybe there's a reason you think it's related?

    Anyway, looks like you're drawing a line somewhere or maybe this is a super thin sprite but it's impossible to tell so hard to help further.
     
  3. Zamki

    Zamki

    Joined:
    Jan 27, 2020
    Posts:
    4
    i think it is related because the render issue started when i implemented the Rotaiton
     
  4. Wezai

    Wezai

    Joined:
    Dec 30, 2016
    Posts:
    74
    Have you tried disabling this script to see if it's really the culprit?
     
    MelvMay likes this.
  5. Zamki

    Zamki

    Joined:
    Jan 27, 2020
    Posts:
    4
    Yes the little line is not there if i disable the script
    movement.x = Joystickmovement.Horizontal;
    movement.y = Joystickmovement.Vertical;
    Rotation.y = Looking.Horizontal;
    Rotation.x = Looking.Vertical * -1;
    this is the part where i get the input from the joysticks

    //Rotation
    Vector2 VirtualMouse = rb.position + Rotation;
    Vector2 lookdir = VirtualMouse - rb.position;
    float angle = Mathf.Atan2(lookdir.y, lookdir.x) * Mathf.Rad2Deg - 90f;
    rb.rotation = angle;
    this is the part where i first calculate a virtual mous to get the location from the joystick then i calculate the angel and then set the angel as the Rigidbody2D rotaiton -90°
     
  6. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,353
    Use code tags please:
    upload_2020-1-27_15-44-10.png
     
  7. Zamki

    Zamki

    Joined:
    Jan 27, 2020
    Posts:
    4
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Movement : MonoBehaviour
    6. {
    7.     public float moveSpeed = 5f;
    8.     public Joystick Joystickmovement;
    9.     public Rigidbody2D rb;
    10.     public Joystick Looking;
    11.     Vector2 Rotation;
    12.     Vector2 movement;
    13.     public Rigidbody2D Kamera;
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         //Input
    19.         movement.x = Joystickmovement.Horizontal;
    20.         movement.y = Joystickmovement.Vertical;
    21.         Rotation.y = Looking.Horizontal;
    22.         Rotation.x = Looking.Vertical * -1;
    23.  
    24.     }
    25.  
    26.     void FixedUpdate()
    27.     {
    28.         //movement
    29.         rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
    30.  
    31.         //Rotation
    32.         Vector2 VirtualMouse = rb.position + Rotation;
    33.         Vector2 lookdir = VirtualMouse - rb.position;
    34.         float angle = Mathf.Atan2(lookdir.y, lookdir.x) * Mathf.Rad2Deg - 90f;
    35.         rb.rotation = angle;
    36.         Kamera.position = rb.position;
    37.     }
    38. }
    39.  
    Code (CSharp):
    1.  
     
    Cornysam likes this.
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Sorry but I still have no idea. Physics is just that, physics. It has nothing to do with rendering so even if its the movement showing the line, it's still something to do with your rendering.

    Probably best to show a video or describe what's being rendered; maybe some clearer images.

    NOTE: You're setting rb.rotation directly. You should be using "rb.MoveRotation" same as you do "rb.MovePosition".