Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Mrfunnylink · May 28, 2020 at 08:06 PM · knockback

,Knockback based on direction object was shot from.

I am rather new to c# and am making a sumo game with unity 3d. The sumo game isn't normal sumo where you wrestle your opponent off the edge, in my version you shoot them with a gun off the edge. There are two scripts, the Target script (the script that goes on the Gameobject being shot at) and the gun script (the script that shoots the raycast). So, want to be able for the gameObject I shoot at to be moved in the opposite direction I shot it in (basically knockback). The problem is, if I shoot it from the side or back, it still moves in the same direction as if I were shooting the front. What is the easiest way I could do this?

My target script:

 using UnityEngine;
 
 public class Target : MonoBehaviour
 {
 //public float health = 50f;
 
 
 public void TakeDamage (float amount)
 {
 transform.position += Vector3.forward * Time.deltaTime * 50f ;
 }
 
 void Die ()
     {
         Destroy(gameObject);
     }
 
 
 }


My gun script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 public class p1gun : MonoBehaviour
 {
 
 public float damage = 10f;
 public float range = 100f;
 public float p1kb = 50;
 
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
 
        
 
        
         if (Input.GetButtonDown("Jump"))
         {
     Shoot();
         }
     }
     
     void Shoot ()
     {
     RaycastHit hit;
     if (Physics.Raycast(transform.position, transform.forward, out hit, range));
     {
          Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
         Debug.Log(hit.transform.name);
 
         Target target = hit.transform.GetComponent<Target>();
         if (target != null)
         {
             target.TakeDamage(damage);
             Debug.DrawRay(transform.position, transform.forward, Color.green);
         }
 
         if (hit.rigidbody != null)
         {
         hit.rigidbody.AddForce(hit.normal * p1kb);
         }
     }
     }
 }




,I am making sort of like a sumo game, although instead of wrestling the other player of the edge, you shoot them off with a gun to win. There are two scripts, a target script (the script that goes on the object being shot) and a gun script (the script that shoots the recast from the actual gun).

So, I want to be able for the gameObject I shoot at to be moved in the opposite direction i shot it in (basically knockback). The problem is, if I shoot it from the side or back, it still moves in the same direction as if I were shooting the front.

My target script:

using UnityEngine;

public class Target : MonoBehaviour { //public float health = 50f;

public void TakeDamage (float amount) { transform.position += Vector3.forward Time.deltaTime 50f ; }

void Die () { Destroy(gameObject); }

}

My gun script:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class p1gun : MonoBehaviour {

public float damage = 10f; public float range = 100f; public float p1kb = 50;

 // Start is called before the first frame update
 void Start()
 {
     
 }

 // Update is called once per frame
 void Update()
 {

    

    
     if (Input.GetButtonDown("Jump"))
     {
 Shoot();
     }
 }
 
 void Shoot ()
 {
 RaycastHit hit;
 if (Physics.Raycast(transform.position, transform.forward, out hit, range));
 {
      Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
     Debug.Log(hit.transform.name);

     Target target = hit.transform.GetComponent<Target>();
     if (target != null)
     {
         target.TakeDamage(damage);
         Debug.DrawRay(transform.position, transform.forward, Color.green);
     }

     if (hit.rigidbody != null)
     {
     hit.rigidbody.AddForce(hit.normal * p1kb);
     }
 }
 }

}

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 Map-Builder · May 28, 2020 at 11:32 PM

I do not find what you say clear. But you use hit.normal, I assume you are using capsule collider or sphere and not box collider or there would be only 4 directions.


For the rest, with what I have, it seems fine. Use drawray or drawline with a certain time with hit.point & hit.normal to make sure the dirction is fine


PS: (re)read your post, it may makes you make it clearer for people to answer. (in case of edit, text+bulk code pasted twice in his huge question)

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.

Follow this Question

Answers Answers and Comments

126 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 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 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 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 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 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

Hitting an Enemy and Pushing them back 2 Answers

2D RPG style knockback on attack 0 Answers

How Do I Add Knockback To Player Once Firing Gun In 2D? 0 Answers

2d platformer combat system 1 Answer

change an object's velocity, while you edit its transform as well? 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