- Home /
Question by
Dev_James · Jan 18, 2021 at 10:54 AM ·
scripting problem
Rigidbody 2D addforce is not working
I have a 2D rigidbody which I am trying to add a force to. I have 2 movement systems, and the 1st one works perfectly fine, but when I try to addforce in the 2nd movement script, nothing happens. I used Debug.Log to make sure that there is nothin wrong with the input detection and there was nothing wrong. I copied and pasted the same add force line of code from the 1st script that worked perfectly fine, and I made sure that the 2nd rigidbody is assigned to the same as the 1st and still not working. I aslo tried removing all the code that comes before. I am very confused. Here is my code: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class TouchController : MonoBehaviour { public bool isOnPC = false;
public Transform crosshairs;
private Vector3 Target;
public Rigidbody2D rocket;
public float power;
public float xRotation = 0f;
public ForceMode2D forceMode = ForceMode2D.Force;
public float speed = 1500f;
public float maxSpeed;
private void Update()
{
if (rocket.velocity.magnitude > maxSpeed)
{
rocket.velocity = rocket.velocity.normalized * maxSpeed;
}
Target = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
crosshairs.position = new Vector2(Target.x, Target.y);
Vector3 difference = Target - transform.position;
float roationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(xRotation, 0, roationZ);
if (Input.GetKey(KeyCode.Space))
{
rocket.AddForce(transform.forward * Time.deltaTime * speed * 2, forceMode);
}
}
}
Comment