Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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)"...

Show more comments

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.

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

spaceship aim 1 Answer

How to import the object from server to unity 2 Answers

Material doesn't have a color property '_Color' 4 Answers

Can someone help me fix my Javascript for Flickering Light? 6 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