Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 saitenntaisei · Oct 29, 2016 at 03:52 PM · c#unity 5

Why this c# program can not to auto die player

I want to make game of tank but I can not auto die player Ex. if three "Bullet" clash to the player, player was dead Where is wrong?

  using UnityEngine;
  using System.Collections;

  namespace StateMachineSample
  {
      public class PlayerController : MonoBehaviour
      {
          public Transform turret;
          public Transform muzzle;
          public GameObject bulletPrefab;

     private int maxLife = 3;
     private int life;

     private float force = 100f;
     private float maxSpeed = 30f;
     private float maxAngularVelocity = 360f;
     private float attackInterval = 0.8f;

     private int groundLayerMask;
     private float lastAttackTime;

     private void Start()
     {
         groundLayerMask = 1 << LayerMask.NameToLayer("Ground");
     }

     private void Update()
     {
         UpdateTank();
         UpdateTurret();
     }

     

     private void UpdateTank()
     {
         Vector3 velocity = GetComponent<Rigidbody>().velocity;

         life = maxLife;




         if (velocity.sqrMagnitude < 1f)
         {
             return;
         }

         Vector3 direction = new Vector3(velocity.x, 0f, velocity.z);

         Quaternion targetRotation = Quaternion.LookRotation(direction);
         transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, maxAngularVelocity * Time.deltaTime);
     }

     public void TakeDamage()
     {
         life--;
         if (life <= 0)
         {
             Destroy(gameObject);
         }
     }



     private void UpdateTurret()
     {

         life--;
         if (life <= 0)
         {
             Destroy(gameObject);
         }

         // マウス位置を狙う
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
         if (Physics.Raycast(ray, out hit, Mathf.Infinity, groundLayerMask))
         {
             turret.rotation = Quaternion.LookRotation(hit.point - turret.position);
         }

         
         

         // マウス左クリックで発射する
         if (Input.GetMouseButtonDown(0))
         {
             if (Time.time > lastAttackTime + attackInterval)
             {
                 Instantiate(bulletPrefab, muzzle.position, muzzle.rotation);
                 lastAttackTime = Time.time;
             }
         }
     }


     private void FixedUpdate()
     {
         float x = Input.GetAxisRaw("Horizontal");
         float z = Input.GetAxisRaw("Vertical");

         Vector3 direction = new Vector3(x, 0f, z).normalized;

         GetComponent<Rigidbody>().AddForce(direction * force);
         GetComponent<Rigidbody>().velocity = Vector3.ClampMagnitude(GetComponent<Rigidbody>().velocity, maxSpeed);
     }
 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ananasblau · Oct 29, 2016 at 04:49 PM

Where is wrong? indeed. You have both FixedUpdate() and Update(), not sure if that is a great idea.

In UpdateTank() you have life = maxLife;which means with every Update() you are resetting the player's life. Remove the line and you should be good (or rather: dead).

Comment
Add comment · Show 6 · 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 saitenntaisei · Nov 03, 2016 at 01:55 AM 0
Share

can you type me correct program? because I remove the life = maxlife; to start but it still do not work correct (the player vanish but the bullet did not crash)

avatar image ananasblau saitenntaisei · Nov 03, 2016 at 05:46 AM 0
Share

You have to put that line into Start()

avatar image saitenntaisei ananasblau · Nov 03, 2016 at 07:57 AM 0
Share

yes I put it into Start()

Show more comments
Show more comments
avatar image saitenntaisei · Nov 03, 2016 at 12:28 PM 0
Share

can you type me correct program?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Serializable class definition in base class doesn't show in editor 1 Answer

How to make buttons have sound when it is highlighted and clicked? 0 Answers

Updating Unity 2 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