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

Question Help With Trajectory Prediction

Discussion in 'Scripting' started by morian35, May 1, 2022.

  1. morian35

    morian35

    Joined:
    May 21, 2020
    Posts:
    3
    Hi I'm new in coding so I want your help... I have a cannon with limit rotation so I want to make
    trajectory prediction for it but I don't know how I try some tutorials but nothing work

    here is my code

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CannonManager : MonoBehaviour
    6. {
    7.  
    8.     public GameObject ball;
    9.     public float lunchForce;
    10.     public Transform shotPoint;
    11.  
    12.  
    13.  
    14.  
    15.     void Update()
    16.     {
    17.         Vector3 mouse_pos = Input.mousePosition;
    18.         Vector3 player_pos = Camera.main.WorldToScreenPoint(this.transform.position);
    19.  
    20.         mouse_pos.x = mouse_pos.x - player_pos.x;
    21.         mouse_pos.y = mouse_pos.y - player_pos.y;
    22.  
    23.         float angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
    24.         Debug.Log(angle);
    25.         //if(angle )
    26.         this.transform.rotation = Quaternion.Euler(new Vector3(0, 0, Mathf.Min(Mathf.Max(angle, -20), 65)));
    27.  
    28.         if (Input.GetMouseButtonDown(0))
    29.         {
    30.             Shoot();
    31.         }
    32.  
    33.     }
    34.  
    35.     void Shoot()
    36.     {
    37.         GameObject newBall = Instantiate(ball, shotPoint.position, shotPoint.rotation);
    38.         newBall.GetComponent<Rigidbody2D>().velocity = transform.right * lunchForce;
    39.     }
    40.  
    41.  
    42. }
    43.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    There is certainly no possible way that one of us typing only text here could help you grasp something that you are unable to get from video tutorials.

    If you cannot work through tutorials, I suggest going back to the basics of classical physics kinematics and using those as a guideline for developing your own solution. Those rules have not changed since they were identified and anything you implement WILL have to correctly implement that math, no other way around it.

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Tutorials are a GREAT idea. Tutorials should be used this way:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!

    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!
     
    morian35 likes this.
  3. morian35

    morian35

    Joined:
    May 21, 2020
    Posts:
    3
    the problem that in the tutorials its show how to do it with full rotation in all the directions because it calculate the trajectory prediction with mouse direction and follow the mouse not the cannon (indeed the cannon follow the mouse but in a specific area) so is there a way to make the trajectory prediction to follow the cannon and appear only in the specific area that cannon shoot on it
     
  4. morian35

    morian35

    Joined:
    May 21, 2020
    Posts:
    3
    Can someone help me please