- Home /
How to i add force in a angle i have got?
I have an angle and I want to add force in that direction. It is a 2d game here is the code for the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float movementSpeed = 3.0f;
float movement = new float();
public float shootdirection;
public float recoildirection;
public Vector2 flydirection;
Rigidbody2D rb2D;
private void Start()
{
rb2D = GetComponent<Rigidbody2D>();
}
private void Update()
{
}
private void FixedUpdate()
{
setvalues();
}
void setvalues()
{
shootdirection = GameObject.Find("Arrow").GetComponent<PointerController>().z_rotation;
recoildirection = GameObject.Find("Arrow").GetComponent<PointerController>().thrustdirection;
}
void blast()
{
}
}
Answer by maewionn · Aug 23, 2019 at 11:36 PM
Hi, you should have included what angle exactly you mean.
I assume you want to add a force to the player along the recoil direction, no?
I am currently not at my machine, so I can only give you a pointer, as I can't double check the code if it works. Also, I have no idea what a pointer controller is, so I'll just try to find a general purpose solution for you (I can't find that in the documentation (aside from open-VR, which seemed odd considering 2D)).
I'd go with Rigidbody2D.AddForce(new Vector3 (0,0, whateverZRotationYouNeed)).
All in all, I think the issue is in working with said PointerController. While it might be a nice shortcut, anything that is not findable in the documentation is hard to work with.