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
1
Question by DaPham · Sep 11, 2018 at 10:29 PM · physicsaddforceflip

Problem with when turning left with angling and the force of the projectile

Hello Unity

I recently picked up Unity and already stumped upon an issue. My project is a 2.5D with the background and platforms being 3D and everything else in 2D.

So far I've been able to flip both the player and the sprite of the projectile as intended. However, when it comes to shooting, I'm experiencing 2 problems:

1) I'm unable to addforce when facing left.

2) Angling - When I shoot facing right, I shoot up to the right (Imagine throwing a stone). So up and to the right. When facing left, it shoots straight downwards and due to problem 1, it's just falling down, when instantiated.

Most of my codes have been handpicked and slightly modified to what I need so far. I have been unable for 2 days to find a solution, so any tips and help would be appreciated, and I apologize in advance, if I am slow to understand any feedback, as I am relatively new to C#.

Below are my scripts


The following is my code for projectile:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Projectile : MonoBehaviour
 {
     public float speed = 150f;
     Player player;
     public bool facingRight = true;

     private void Start()
     {
         player = GameObject.Find("Player").GetComponent<Player>();
 
         if (!player.facingRight && transform.localScale.x > 0)
         {
             Flip();
         }

     }

     private void OnTriggerEnter2D(Collider2D target)
     {
         if (target.gameObject.tag == "FirePoint") GetComponent<Rigidbody2D>().AddForce(transform.right * speed);
     }
 
     public void Flip()
     {
         facingRight = !facingRight;
         transform.localScale = new Vector3(
                   transform.localScale.x * -1,
                   transform.localScale.y,
                   transform.localScale.z);
     }
 
 }
 

The following is where my projectile gets instantiated

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ThrowingAxeSpawn : MonoBehaviour
 {
 
     public GameObject throwingAxe;
     public Transform spawnPoint;
 
 
 
     void FixedUpdate()
     {
         bool shoot = Input.GetKey(KeyCode.Mouse1);
         {
 
             if (shoot) Instantiate(throwingAxe, spawnPoint.position, spawnPoint.rotation);
 
         }
     }
 }
     

The following is my player script if it has any relevance

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(Controller2D))]
 public class Player : MonoBehaviour
 {
     public bool facingRight = true;
 
     public float maxJumpHeight = 4;
     public float minJumpHeight = 1;
     public float timeToJumpApex = .4f;
     float accelerationTimeAirborne = .2f;
     float accelerationTimeGrounded = .1f;
     float moveSpeed = 6;
 
     public Vector2 wallJumpClimb;
     public Vector2 wallJumpOff;
     public Vector2 wallLeap;
 
     public float wallSlideSpeedMax = 3;
     public float wallStickTime = .25f;
     float timeToWallUnstick;
 
     float gravity;
     float maxJumpVelocity;
     float minJumpVelocity;
     Vector3 velocity;
     float velocityXSmoothing;
 
     Controller2D controller;
 
     Vector2 directionalInput;
     bool wallSliding;
     int wallDirX;
 
 
     void Start()
     {
 
         controller = GetComponent<Controller2D>();
 
         gravity = -(2 * maxJumpHeight) / Mathf.Pow(timeToJumpApex, 2);
         maxJumpVelocity = Mathf.Abs(gravity) * timeToJumpApex;
         minJumpVelocity = Mathf.Sqrt(2 * Mathf.Abs(gravity) * minJumpHeight);
     }
 
     void Update()
     {
         
         CalculateVelocity();
         HandleWallSliding();
 
         controller.Move(velocity * Time.deltaTime, directionalInput);
 
         if (controller.collisions.above || controller.collisions.below)
         {
             if (controller.collisions.slidingDownMaxSlope)
             {
                 velocity.y += controller.collisions.slopeNormal.y * -gravity * Time.deltaTime;
             }
             else
             {
                 velocity.y = 0;
             }
         }
         if (Input.GetAxis("Horizontal") < 0 && facingRight) Flip();
         if (Input.GetAxis("Horizontal") > 0 && !facingRight) Flip();
     }
 
     public void SetDirectionalInput(Vector2 input)
     {
         directionalInput = input;
     }
 
     public void OnJumpInputDown()
     {
         if (wallSliding)
         {
             if (wallDirX == directionalInput.x)
             {
                 velocity.x = -wallDirX * wallJumpClimb.x;
                 velocity.y = wallJumpClimb.y;
             }
             else if (directionalInput.x == 0)
             {
                 velocity.x = -wallDirX * wallJumpOff.x;
                 velocity.y = wallJumpOff.y;
             }
             else
             {
                 velocity.x = -wallDirX * wallLeap.x;
                 velocity.y = wallLeap.y;
             }
         }
         if (controller.collisions.below)
         {
             if (controller.collisions.slidingDownMaxSlope)
             {
                 if (directionalInput.x != -Mathf.Sign(controller.collisions.slopeNormal.x))
                 { // not jumping against max slope
                     velocity.y = maxJumpVelocity * controller.collisions.slopeNormal.y;
                     velocity.x = maxJumpVelocity * controller.collisions.slopeNormal.x;
                 }
             }
             else
             {
                 velocity.y = maxJumpVelocity;
             }
         }
     }
 
     public void OnJumpInputUp()
     {
         if (velocity.y > minJumpVelocity)
         {
             velocity.y = minJumpVelocity;
         }
     }
 
 
     void HandleWallSliding()
     {
         wallDirX = (controller.collisions.left) ? -1 : 1;
         wallSliding = false;
         if ((controller.collisions.left || controller.collisions.right) && !controller.collisions.below && velocity.y < 0)
         {
             wallSliding = true;
 
             if (velocity.y < -wallSlideSpeedMax)
             {
                 velocity.y = -wallSlideSpeedMax;
             }
 
             if (timeToWallUnstick > 0)
             {
                 velocityXSmoothing = 0;
                 velocity.x = 0;
 
                 if (directionalInput.x != wallDirX && directionalInput.x != 0)
                 {
                     timeToWallUnstick -= Time.deltaTime;
                 }
                 else
                 {
                     timeToWallUnstick = wallStickTime;
                 }
             }
             else
             {
                 timeToWallUnstick = wallStickTime;
             }
 
         }
 
     }
     public void Flip()
     {
         facingRight = !facingRight;
 
         transform.localScale = new Vector3(
                   transform.localScale.x * -1,
                   transform.localScale.y,
                   transform.localScale.z);
     }
     void CalculateVelocity()
     {
         float targetVelocityX = directionalInput.x * moveSpeed;
         velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below) ? accelerationTimeGrounded : accelerationTimeAirborne);
         velocity.y += gravity * Time.deltaTime;
     }
 
     
     
 }





Comment
Add comment · Show 1
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 tormentoarmagedoom · Sep 11, 2018 at 10:45 PM 1
Share

This post is "infumable", so long, so much code.... Optimize the info you give.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by IveSpelltItWrong · Sep 12, 2018 at 10:42 AM

first of all, because by default the bullet is facing right, you only need to put !Player.facingRight in the if statement. secondly, in the throwing axe class you can remove the extra pair of curly brackets, because they will probably cause problems and did you assign the spawnPoint transform in the inspector? and you're not applying any force to the axe. if you're using a rigidbody, add this code to your projectile: public rigidBody rb; //set this in the inspector or in the start method void Start() {

 //your other start stuff here

 rb.AddForce(speed * Vector2.forward * playerPointingdirection, ForceMode.Impulse); //player pointing 
                                                                                                                                                            direction is -1 
                                                                                                                                                              for left, 1 for right
 //assuming speed is the axe speed
 

}

or if you're not using rigidbody use this:


Update()
{
    transform.translate(vector2.right * playerPointingDirection * speed * Time.DeltaTime
}

this should help, i really hope it does, if it doesn't i hope it has improved your situation.

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

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

Does AddForce use Time.deltaTime or TIme.fixedDeltaTime or it depends on whether it is being used in Update() or fixedUpdate()? 2 Answers

Why use AddForce rather than modify velocity? 4 Answers

AddForce at mouse position only on table area 0 Answers

Apply A Force to Reach a Specific Height 3 Answers

Applying force to an object without rotating it. 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