• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by yakawa · Mar 11, 2015 at 05:12 PM · script error

tp system problem csharp

hi i'm trying to make my player shot a bullet when "q" is pressed and when he press "q" again he will teleport to the bullet position . Im using this code : (the problem is on the update function )

public class RobotController : MonoBehaviour { public float nextFire; public float fireRate; public bool tp= false ; public GameObject projectile; public Transform spawn; public float maxSpeed = 10f ; bool facingRight = true; public float lifeTime = 3f; public Transform proj; Animator anim ; // Use this for initialization void Start () {

     anim = GetComponent<Animator>();
 }
 
 // Update is called once per frame
 void FixedUpdate () {
 
     float move = Input.GetAxis ("Horizontal");

     anim.SetFloat ("speed", Mathf.Abs (move));
     rigidbody2D.velocity = new Vector3 (move * maxSpeed, rigidbody2D.velocity.y,0);

     if (move < 0 && facingRight)
         Flip ();
     else if (move > 0 && !facingRight)
         Flip ();

 }

 void Update(){

     if (Input.GetKey ("mo") && tp == false && Time.time > nextFire ) {

         Spawn ();
         nextFire = Time.time + fireRate;
         tp = true;

     
     }

     if (Input.GetKey ("q") && tp == true) {

         gameObject.transform.position = new Vector2 ( proj.transform.position.x,proj.transform.position.y);
     
         tp = false;

     }

 }
 void Flip (){

     facingRight = !facingRight;

     Vector3 theScale = transform.localScale;

     theScale.x *= -1;

     transform.localScale = theScale;



 }

 void Spawn (){
     GameObject clone;
     clone =  (GameObject) Instantiate (projectile, spawn.position, spawn.rotation);
     Destroy (clone, lifeTime);

     projectile = clone;
 }

}

projectile is the prefab of my bullet , the transform proj = projecile . the problem that instead of tp on the bullet the player tp on the first position of the projectile setted in the prefab wats wrong ?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Oribow · Mar 11, 2015 at 07:47 PM

You asign the position of the bullet to projectile, but teleport the player to proj, wich was never asigned in your code? Rename projectile to proj at:

projectile = clone

Comment
Add comment · Show 2 · Share
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 yakawa · Mar 11, 2015 at 11:17 PM 0
Share

proj is a transform I can't make proj = clone cause clone is a GameObject and in unity a asign the transform proj = projectile

avatar image Oribow · Mar 12, 2015 at 04:47 PM 0
Share

just get the Transform component from your GameObject. It will keep it self updated with the origin Gameobject cause ist just a Referenz

 clone.tranformation
avatar image
0

Answer by yakawa · Mar 13, 2015 at 08:59 PM

it still didn't work . But I changed the script and it worked here the code if you want to know and thanks for the help ^^

public float nextFire; public float fireRate;

 public float nextTp;
 public float tpRate;
 public GameObject projectile;
 public Transform spawn;
 public float maxSpeed = 10f ; 
      bool facingRight = true;
 public float lifeTime = 3f;
 GameObject tmp;

 float projInstanceMouseX;
 float projInstanceMouseY;

 Animator anim ;
 // Use this for initialization
 void Start () {
 
     anim = GetComponent<Animator>();
 }
 
 // Update is called once per frame
 void FixedUpdate () {
 
     float move = Input.GetAxis ("Horizontal");

     anim.SetFloat ("speed", Mathf.Abs (move));
     rigidbody2D.velocity = new Vector3 (move * maxSpeed, rigidbody2D.velocity.y,0);

     if (move < 0 && facingRight)
         Flip ();
     else if (move > 0 && !facingRight)
         Flip ();

 }

 void Update(){    

     if ((Input.GetKey ("q")) && (tmp == null) && (Time.time > nextFire || Time.time < 2f)) {
         Debug.Log("instanciate");
         nextTp = Time.time + tpRate;
         Spawn ();
         nextFire = Time.time + fireRate;
     } else if ((Input.GetKey ("q")) && (Time.time > nextFire && Time.time > nextTp ) && (tmp != null)) {


         gameObject.transform.position = new Vector2 (tmp.transform.position.x, tmp.transform.position.y);
         Destroy(tmp);
         nextFire = Time.time + fireRate;
         Debug.Log("destroy");
     } else if (tmp != null) {
         tmp.rigidbody2D.velocity = new Vector2 (projInstanceMouseX * Time.deltaTime * maxSpeed, projInstanceMouseY * Time.deltaTime * maxSpeed);
     }


 }
 void Flip (){

     facingRight = !facingRight;

     Vector3 theScale = transform.localScale;

     theScale.x *= -1;

     transform.localScale = theScale;



 }

 void Spawn (){
     projInstanceMouseX = Input.mousePosition.x;
     projInstanceMouseY = Input.mousePosition.y;
     
     tmp = (GameObject)Instantiate (projectile, spawn.position, spawn.rotation);
     Destroy (tmp, lifeTime);
 }


}

Comment
Add comment · Share
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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

'parent' is not a member of 'Object'. 1 Answer

Csharp script parsing error 2 Answers

my STFPSC script has errors can anyone help me? 0 Answers

Object reference not set to an instance of an object error. 1 Answer

AddComponent() is adding 64 instances of my script to my GO 1 Answer


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