In vs code I'm getting this error "Argument 2: cannot convert from 'float' to 'UnityEngine.Vector2' [Assembly-CSharp]csharp(CS1503)".
This is the code where the error appears:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HF_001 : MonoBehaviour
{
public enum ForceType { Repulsion = -1, None = 0, Attraction = 1 }
public ForceType m_Type;
public Transform m_Pivot;
public float m_Radius;
public float m_StopRadius;
public float m_Force;
public LayerMask m_Layers;
private void FixedUpdate()
{
Collider2D[] colliders = Physics2D.OverlapArea(m_Pivot.position, m_Radius, m_Layers);
float signal = (float)m_Type;
foreach (var collider in colliders)
{
Rigidbody2D body = collider.GetComponent<Rigidbody2D>();
if (body == null)
continue;
Vector2 direction = m_Pivot.position - body.position;
float distance = direction.magnitude;
direction = direction.normalized;
if (distance < m_StopRadius)
continue;
float forceRate = (m_Force / distance);
body.AddForce(direction * (forceRate / body.mass) * signal);
}
}
}
I tried changing the type of the variable "m_Radius"
As per official document on Physics2D.OverlapArea. It takes second argument Vector2 not a float value.
In the above code public float m_Radius; is type of float but need to be Vector2.
Physics2D.OverlapArea is used to check if a Collider falls within a rectangular area where as if you need to use Radius but not a point then please look at Physics2D.OverlapCircle
Related
I've been coding a top down game and I wrote the basic movement scripts but on line 32 it states that I need a prefix at the end.
How can I fix this ?
Here is the error I get:
Assets\PlayerController.cs(32,71): error CS1003: Syntax error, ','
expected
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed;
private Rigidbody2D rb;
private float x;
private float y;
private Vector2 input;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
GetInput();
}
private void FixedUpdate()
{
rb.velocity = input * moveSpeed;
}
private void GetInput()
{
Vector2 input = new Vector2(input.GetAxisRaw("Horizontal"), 0 input.GetAxisRaw("Vertical"));
input.x = x;
input.Normalize();
}
}
First of all, it is best to understand the similarities and differences between input.GetAxis() and input.GetAxisRaw(). There are two types of incoming parameters: Vertical: Get the value in the vertical direction. Horizontal: Get the value in the horizontal direction. The return value of input.GetAxis() is [-1, 1], and the return value of input.GetAxisRaw() with smooth transition function is {-1, 0, 1}
private void GetInput()
{
float move = Input.GetAxis("Horizontal");
if(move != 0){
Vector2 input = new Vector2(move * Time, rb.velocity.y);
}
}
Hope can help you
I haven't used rotation in a while so I can't remember how to do this. I am trying to make a random trajectory for my bullets and I want to add a random rotation but I don't know how to add the rotation. I commented in the script where the rotation should be added. I have tried adding + transform.rotation * Quaternion.Euler(0,rot, 0) but I got the error error CS0019: Operator '+' cannot be applied to operands of type 'Vector3' and 'Quaternion'. What could I add to make this work?
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class Player : MonoBehaviour
{
public GameObject bulletPrefab;
public Camera playerCamera;
private Coroutine hasCourutineRunYet = null;
public int ammo = 300;
public Text Ammmo;
private float xPos;
private float zPos;
private float yPos;
private float rot;
// Update is called once per frame
void Update()
{
Ammmo.text = "Ammo: " + ammo.ToString();
if(Input.GetMouseButtonDown(0))
{
if (hasCourutineRunYet == null)
hasCourutineRunYet = StartCoroutine(BulletShot());
}
}
IEnumerator BulletShot()
{
xPos = Random.Range(-0.3f, 0.3f);
yPos = Random.Range(-0.3f, 0.3f);
zPos = Random.Range(-0.3f, 0.3f);
rot = Random.Range(-10.0f, 10.0f);
GameObject bulletObject = Instantiate(bulletPrefab);
bulletObject.transform.position = playerCamera.transform.position + playerCamera.transform.forward + new Vector3(xPos, yPos, zPos) ; // add rotation here
bulletObject.transform.forward = playerCamera.transform.forward;
yield return new WaitForSeconds(1.0f);
hasCourutineRunYet = null;
ammo--;
}
}
I would look into the transform.rotation.RotateAround function. With this, you can call your bullet's relative forward, and rotate around itself essentially
Code Below:
using UnityEngine;
//Attach this script to a GameObject to rotate around the target position.
public class Example : MonoBehaviour
{
//Assign a GameObject in the Inspector to rotate around
public GameObject target;
void Update()
{
// Spin the object around the target at 20 degrees/second.
transform.RotateAround(target.transform.position, Vector3.up, 20 * Time.deltaTime);
}
}
Here's a URL to the documentation: https://docs.unity3d.com/ScriptReference/Transform.RotateAround.html
I'm trying to make a game where you drive a little car, and when you press a button, say C, wings come out and you can fly. I'm thinking the best way to do this would have a capsule scaled to look like rounded wings as a child of the car. The Z scale of these wings is normally small enough that you can't see them (0.01), but when you press C, the Z value slowly increases until it reaches a certain point (in my case 0.0435) All I'm getting now is an error saying "Cannot modify the return value of 'Transform.localScale' because it is not a variable."
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RocketorCar : MonoBehaviour
{
public Transform wings;
public Rigidbody rb;
void Update()
{
if(Input.GetKey("c")){
if(wings.localScale.z = 0.01f){
wings.localScale = wings.localScale + new Vector3(0, 0, 0.0335f);
}
}
}
}
Your error is that you use 1 equal sign, but you want 2 equal signs for comparing, e.g. ==:
if (wings.localScale.z == 0.01f) { /* ... */ } // you want to compare
The error is misleading, as you assign with one equal sign, the error is because since localScale is a struct, you must introduce a local variable from it, change its value, then assign it back to localScale:
var localScale = wings.localScale;
localScale.z = 0.01f;
wings.localScale = localScale;
Back to your problem, you'll then get a warning about floating point comparison,
Here's a better approach in comparing them:
if (Mathf.Approximately(wings.localScale.z, 0.01f))
{
// ...
}
EDIT
Scales your wings, change as you see fit:
using UnityEngine;
public class Scaler : MonoBehaviour
{
public Transform[] Wings; // drag your wings here in inspector
public KeyCode WingScaleKeyUp = KeyCode.KeypadPlus; // scale up key
public KeyCode WingScaleKeyUpDown = KeyCode.KeypadMinus; // scale down key
public Vector3 WingScaleFactor = new Vector3(0.1f, 0.1f, 0.1f); // scale step
public Vector3 WingScaleSpeed = new Vector3(0.1f, 0.1f, 0.1f); // scale speed
public Vector3 WingScaleMin = new Vector3(0.1f, 0.1f, 0.1f); // scale min
public Vector3 WingScaleMax = new Vector3(2.0f, 2.0f, 2.0f); // scale max
private void Update()
{
if (Wings == null)
return;
foreach (var target in Wings)
{
var p = Input.GetKey(WingScaleKeyUp) ? +1.0f : Input.GetKey(WingScaleKeyUpDown) ? -1.0f : 0.0f;
var q = target.localScale;
var r = Vector3.Scale(WingScaleFactor, WingScaleSpeed * p); // scale axes independently
var s = q + r;
var x = Mathf.Max(WingScaleMin.x, Mathf.Min(WingScaleMax.x, s.x)); // restrict
var y = Mathf.Max(WingScaleMin.y, Mathf.Min(WingScaleMax.y, s.y));
var z = Mathf.Max(WingScaleMin.z, Mathf.Min(WingScaleMax.z, s.z));
target.localScale = new Vector3(x, y, z);
}
}
}
See if this works:
Vector3 tempScale = wings.localScale;
tempScale.z += 0.0335f;
wings.localScale = tempScale;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateToTarget : MonoBehaviour
{
public Transform target;
public Transform character;
private const float FAC_SPEED = 10f;
private const float FAC_LERP = 0.9f;
private const float ANG_MAX = 80f;
// Start is called before the first frame update
void Start()
{
/* ... */
}
// Update is called once per frame
void Update()
{
float targetAngleFromForward = Vector3.Angle(character.transform.forward, target.position - transform.position);
Vector3 lerpPoint;
if (targetAngleFromForward < ANG_MAX)
{
// Lerp towards the target's direction
// This is not a very good or elegant solution but it demonstrates the idea
lerpPoint = Vector3.Lerp(transform.forward, (target.position - transform.position).normalized * FAC_SPEED, FAC_LERP * Time.deltaTime);
}
else
{
// Lerp towards the forward direction
// Same idea, but to character.transform.forward instead
lerpPoint = Vector3.Lerp(transform.forward, character.transform.forward * FAC_SPEED, FAC_LERP * Time.deltaTime);
}
transform.rotation = Quaternion.LookRotation(lerpPoint);
}
}
I want to add a global public variable to control the lerpPoint in the IF and in the ELSE.
To control the speed of the Lerp towards the target's direction and the Lerp towards the forward direction.
I tried to play with the const variables values but didn't figure it how to control this speeds.
Lerp gives you a single point part of the way between the two inputs, based on the final parameter which should be between 0 and 1. So you need to smoothly increase the final parameter of Lerp from 0 to 1 over time.
Probably the easiest way would be something like this (pseudocode stripping down your example for clarity):
public class RotateToTarget : MonoBehaviour
{
public float secondsToRotate = 2.0f;
private float secondsSoFar = 0.0f;
void Update()
{
secondsSoFar += Time.deltaTime;
float t = secondsSoFar / secondsToRotate;
Vector3 lerpPoint = Vector3.Lerp(start, end, t);
transform.rotation = Quaternion.LookRotation(lerpPoint);
}
}
I'm using an Array to multiply a 3D object randomly in the space, so i'm getting a lot of nice objects floating randomly over the y, x and z axes.
This object has also an Audio Source with a sound attached to it, which means that after applying the random-array-positioning I get different objects with different Audio Sources as well.
The problem is that I'm also changing the scale of those objects, which it's working super well, but the size/scale/radius of the Audio Source it's not changing at all.
How can I change the scale of the objects and change the size/scale/radius of the Audio Source at the same time, to match both equally or proportionally in size?
I'm looking here but I Can't figured out.
https://docs.unity3d.com/ScriptReference/AudioSource.html
This is the code that I'm using for:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class multipleObjectsGrandes : MonoBehaviour {
public GameObject prefabGrandes;
public GameObject[] gos;
public int cantidad;
public float minScaleObj;
public float maxScaleObj;
void Awake()
{
gos = new GameObject[cantidad];
for(int i = 0; i < gos.Length; i++)
{
//Position
Vector3 position = new Vector3(Random.Range(-40.0f, 40.0f), Random.Range(-40.0f, 40.0f), Random.Range(-40.0f, 40.0f));
GameObject clone = (GameObject)Instantiate(prefabGrandes, position, Quaternion.identity);
//Scale
clone.transform.localScale = Vector3.one * Random.Range(minScaleObj, maxScaleObj);
//Rotation
clone.transform.rotation = Quaternion.Euler(Random.Range(0.0f, 360.0f), Random.Range(0.0f, 360.0f), Random.Range(0.0f, 360.0f));
gos[i] = clone;
}
}
}
This is the code that i'm using to multiply objects:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class multipleObjectsGrandes : MonoBehaviour
{
public GameObject prefabGrandes;
public GameObject[] cuerpos;
public int cantidad;
public float minScaleObj;
public float maxScaleObj;
public float escalaMax;
void Awake ()
{
cuerpos = new GameObject[cantidad];
for (int i = 0; i < cuerpos.Length; i++) {
//Position
Vector3 position = new Vector3 (Random.Range (-40.0f, 40.0f), Random.Range (-40.0f, 40.0f), Random.Range (-40.0f, 40.0f));
GameObject clone = (GameObject)Instantiate (prefabGrandes, position, Quaternion.identity);
//Scale
clone.transform.localScale = Vector3.one * Random.Range (minScaleObj, maxScaleObj);
//Rotation
clone.transform.rotation = Quaternion.Euler (Random.Range (0.0f, 360.0f), Random.Range (0.0f, 360.0f), Random.Range (0.0f, 360.0f));
escalaMax = clone.transform.localScale.x;
//Debug.Log(escalaMax);
}
}
}
Everything is ok here when I'm debugging "escalaMax" in the console.
The variable "escalaMax" is the one that i'm passing to the other script which is inside the object that I'm multiplying which has an audioSource attached to it. This is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent (typeof(AudioSource))]
public class AudioRandomPitch : MonoBehaviour
{
public float startingPitch = 1;
public float minPitchRandom = 0;
public float maxPitchRandom = 0;
AudioSource audio;
public float audioScale;
void Start ()
{
startingPitch = Random.Range (minPitchRandom, maxPitchRandom);
audio = GetComponent<AudioSource> ();
audio.pitch = startingPitch;
GameObject theMultiplicator = GameObject.Find ("Multiplicador_deCuerpos");
multipleObjectsGrandes multiplicatorScript = theMultiplicator.GetComponent<multipleObjectsGrandes> ();
multiplicatorScript.escalaMax = audioScale;
//this is not working
audio.maxDistance = audioScale;
Debug.Log(audioScale);
}
}
Here I'm first saving the value coming from the other script escalaMax inside the new variable audioScale.
The problem now is when I try to pass the audioScale value to the audio.maxDistance var.
I'm super close I know, but as you can see, i'm not a really good programmer and I'm probably doing something stupidly wrong... :/
Thanks for the help!