• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by areFranz · May 22, 2014 at 01:14 PM · aimspaceship

Add an aim to a space ship simulator

Hi All! I have a big problem: i need to add an aim that really point to objects in a 3d space attached to my spaceship. The scene is composed by:

  • GameObject "SpaceShip" with attached script "SchipController";

  • Its child "SphipMesh;

  • another child "Shooter" with the script "Shooting" attached;

And the main camera with the script "SmoothFollow" attached with target set to SpaceShip.

Here are the scripts: "ShipController" (JS):

 var turnspeed = 5.0;
 var speed = 5.0;
 private var trueSpeed = 0.0;
 var strafeSpeed = 5.0;
 
 function Update () {
 
     var roll = Input.GetAxis ("Roll");
     var pitch = Input.GetAxis ("Pitch");
     var yaw = Input.GetAxis ("Yaw");
     var strafe = Vector3 (Input.GetAxis ("Horizontal") * strafeSpeed * Time.deltaTime, Input.GetAxis ("Vertical") * strafeSpeed * Time.deltaTime, 0);
     
     var power = Input.GetAxis ("Power");
     
     //Truespeed controls
     
     if (trueSpeed < 10 && trueSpeed > -3) {
         trueSpeed += power;
     }
 
     if (trueSpeed > 10) { 
         trueSpeed = 9.99;    
     }
 
     if (trueSpeed < -3) {
         trueSpeed = -2.99;    
     }
 
     if (Input.GetKey ("backspace")) {    
         trueSpeed = 0;
     }
     
     rigidbody.AddRelativeTorque (pitch * turnspeed * Time.deltaTime, yaw * turnspeed * Time.deltaTime, roll * turnspeed * Time.deltaTime);
     rigidbody.AddRelativeForce (0, 0, trueSpeed * speed * Time.deltaTime);
     rigidbody.AddRelativeForce (strafe);
 }

"Shooting" (C#):

 using UnityEngine;
 using System.Collections;
 
 public class Shooting : MonoBehaviour {
 
     public Rigidbody plasma;
     public Transform plasmaFlash;
     public AudioClip plasmaShootFx;
 
     void Update () {
 
         if (Input.GetKeyDown (KeyCode.Space)) {    
 
             Rigidbody clone;
             clone = Instantiate (plasma, transform.position, Quaternion.Euler (0, 90, 90)) as Rigidbody;
             clone.velocity = transform.TransformDirection (Vector3.forward * 800);
             
             Instantiate (plasmaFlash, transform.position, transform.rotation);
             
             audio.PlayOneShot (plasmaShootFx);
             
             Destroy (clone.transform.gameObject, 2);
         }    
     }
 }

And the SmoothFollow (JS):

 var target : Transform;
 var distance = 3.0;
 var height = 3.0;
 var damping = 5.0;
 var smoothRotation = true;
 var rotationDamping = 10.0;
 
 function FixedUpdate () {
     var wantedPosition = target.TransformPoint(0, height, -distance);
     transform.position = Vector3.Lerp (transform.position, wantedPosition, Time.deltaTime * damping);
 
     if (smoothRotation) {
         var wantedRotation = Quaternion.LookRotation(target.position - transform.position, target.up);
         transform.rotation = Quaternion.Slerp (transform.rotation, wantedRotation, Time.deltaTime * rotationDamping);
     }
 
     else transform.LookAt (target, target.up);
 }

Now, how i can position the aim plane in the way of real point to asteroids (directly forward the ship an looking at the asteroids)? Please help!

Comment
Add comment · Show 16
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · May 22, 2014 at 03:24 PM 0
Share

I understand how you have thing setup, but I don't understand what you are asking in this line:

Now, how i can position the aim plane in the way of real point to asteroids (directly forward the ship an looking at the asteroids)?

avatar image areFranz · May 22, 2014 at 04:22 PM 0
Share

I'm asking that I need that the pointer must be aligned to invisible line that goes through the shooter and the target (the asteroids) so i can shoot to them and destroy them. If I make a plane child of the GameObject Ship, at position Z far from the ship$$anonymous$$esh, I only obtain a fixed aim that is in front of the ship$$anonymous$$esh but not in the traiectory of the target. I hope that you understand now...

avatar image ozone · May 23, 2014 at 12:28 AM 1
Share

You could try drawing the reticle on the GUI ins$$anonymous$$d of rendering a plane. These might help: http://docs.unity3d.com/Documentation/ScriptReference/Camera.WorldToScreenPoint.html http://docs.unity3d.com/Documentation/ScriptReference/GUI.DrawTexture.html

avatar image areFranz · May 23, 2014 at 11:24 AM 0
Share

Ok, I've tried to do the aim in DrawTexture, but, what must be the target of Camera.WorldToScreenPoint() ? I've tried with "Vector3.Forward" and i obtain an aim in the center of the screen and not in front of the ship... and also its moving in y axis in inverted position...

avatar image areFranz · May 23, 2014 at 11:27 AM 0
Share

Ok I inverted the y pos by subtracrting it by the Screen.height... but it conflicts with the SmoothFollow on the camera.. I used the target "new Vector3 (0, 0, 20)"...

avatar image areFranz · May 23, 2014 at 11:29 AM 0
Share

i need something like this http://www.youtube.com/watch?v=bg$$anonymous$$aBzNukRI

avatar image areFranz · May 23, 2014 at 11:35 AM 0
Share

after some tryings i have always that problem: the aim is no in front of the ship but is behind it and conflicts with the camera smooth follow...

avatar image ozone · May 24, 2014 at 08:45 PM 0
Share

Sorry for not replying sooner. Try using Transform.forward and multiplying it by whatever distance you want to get a point in front of the spaceship, then convert that to a screen point.

avatar image areFranz · May 27, 2014 at 02:32 PM 0
Share

It is not clear for me: i have to do this? Vector3 pos = camera.WorldToScreenPoint (transform.forward * 20);

         GUI.DrawTexture (new Rect (pos.x - 32, Screen.height - pos.y - 32, 64, 64), aim);
avatar image frogsbo · May 27, 2014 at 03:09 PM 0
Share

the video you posted doesnt lock on to targets.

You can use a draw texture, i would use a transparent crosshairs object, move it with mouse/keys, make spaceship slerp towards a lookat aligned with target, make camera follow spaceship...

if you want to aim with mouse / touchpad, ai$$anonymous$$g should be relative to camera position, and camera turns when aimer is towards edge of screen.

are you yousing mouse or keys?

avatar image areFranz · May 27, 2014 at 05:13 PM 0
Share

I'm using keys (as the script above)... Can you give me a code snippet to do what you'r saying? EDIT: I don't understand completely... could you tell me in detail what I have to do? Expeclially reguarding my scripts...

avatar image areFranz · May 28, 2014 at 12:54 PM 0
Share

I've tried what frogsbo said me, even not at all, i don't understand how make spaceship slerp towards a lookat aligned with target. But the aim is always behind of the ship, not in front of it... Now I'm testing if a textured EmptyObject in front of the spaceship could work for me, adjusting the scripts values. Could this be a solution?

avatar image areFranz · May 28, 2014 at 02:16 PM 0
Share

The try is almost fine with changing the rotation velocity of my script and the values of the camera follow, but the aim points only near objects and not the far ones... How about a raycast?

avatar image AlucardJay · May 28, 2014 at 05:52 PM 0
Share

Why don't you just make the reticule a child of the ship, then put a LookAt script on the reticule so it always faces the camera?

avatar image areFranz · May 28, 2014 at 07:34 PM 0
Share

Tried, it doesn't work...

avatar image areFranz · May 30, 2014 at 02:13 PM 0
Share

So now I have a plane child of the ship and at position Vector3 (0, 0, 80); i see the aim and its behaviour is correct BUT it don't aim always at the asteroids: the closer ones (from Z 80 and Z 0) are correctily pointed, and the far asteroids are out of axis..the way of DrawTexture does't give results anyway...

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Object "Look at" Mouse Z Axis 1 Answer

Raycast not rotating with object it casts from 0 Answers

Problem with Z rotation when rotating X and Y and the same time. 2 Answers

Pistol Aim prolbem style = Tomb Raider??? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges