Problem With The Shooting Script
Sorry Everyone But I'm new in Unity. I want to build a mobile game. But I Have some Problems with my shooting script: The Script is Below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
#region Singleton class: GameManager
public static GameManager instance;
void Awake()
{
if (instance == null)
{
instance = this;
}
}
#endregion
Camera cam;
public PlayerController ball;
public TrajectoryScript trajectory;
[SerializeField] float pushForce = 4f;
bool isDragging = false;
int canMove = 0;
Vector2 startPoint;
Vector2 endPoint;
Vector2 direction;
Vector2 force;
float distance;
//---------------------------------------
void Start()
{
cam = Camera.main;
ball.DesactivateRb();
}
void Update()
{
Debug.Log(ball.rb.velocity.y);
if (ball.rb.velocity.y == 0 )
{
canMove = 1;
if (Input.GetMouseButtonDown(0) && canMove ==1 )
{
isDragging = true;
OnDragStart();
}
if (Input.GetMouseButtonUp(0) )
{
isDragging = false;
OnDragEnd();
canMove = 2;
force = Vector2.zero;
}
if (isDragging )
{
OnDrag();
}
}
}
//-Drag
void OnDragStart()
{
ball.ActivateRb();
startPoint = cam.ScreenToWorldPoint(Input.mousePosition);
trajectory.Show();
}
void OnDrag()
{
endPoint = cam.ScreenToWorldPoint(Input.mousePosition);
distance = Vector2.Distance(startPoint, endPoint);
if (distance >= 1.2f)
{
distance = 1.2f;
}
direction = (startPoint - endPoint).normalized;
force = direction * distance * pushForce;
//just for debug
Debug.DrawLine(startPoint, endPoint);
trajectory.UpdateDots(ball.ballPos, force);
}
void OnDragEnd()
{
distance = 0;
//push the ball
ball.ActivateRb();
ball.Push(force);
trajectory.Hide();
}
}
My problem is when i get my player into the elevator . The Elevator will auto move up and down. So The Player And Camera Move Up and down too .Futhermore, Because the cam move up and down automatically so the end point will not in the position i want . So i hope everybody suggest me some solutions to solve this problem :((. AH ! another question that :( when the elevator goes down i see that the ball auto jump up :( i don't know why ?
Image1: https://forum.unity.com/attachments/upload_2021-8-7_15-59-52-png.903464/
Image2: https://forum.unity.com/attachments/upload_2021-8-7_16-1-45-png.903470/
Image3: https://forum.unity.com/attachments/upload_2021-8-7_16-1-21-png.903467/
[preview of external content removed for GDPR compliance as it was including 3rd party cookies]
I think the first problem arises from the fact that the start position is update only once when you start the dragging, im not experienced with trajectory and other stuff, but it may help to somehow update the start position in update....i know the existing code won't work....but as i said before i have little experience with this....so that's all i can help for now
thank you but i have fixed it ! just change detect input method !