Hi,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Collide_Speed : MonoBehaviour
{
public GameObject gameObject;
public Rigidbody2D player;
public float mass;
void Start()
{
player = GetComponent<Rigidbody2D>();
player.mass = mass;
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.name == "Object")
{
mass == 0.5;
}
if (col.gameObject.name == "Object2")
{
player.speed == 5;
}
}
}
If the player collides with Object, I want to change the mass from the Rigidbody2D component. But I’m getting “Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement” error. How can I go about changing the mass?
Also, the Rigidbody2D doesn’t have speed, so I wrote a player controller script for speed. How should I go about accessing the field Speed to change speed upon collision for Object2?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public float speed;
private Rigidbody2D rigidbody;
public float jumpForce;
void Start()
{
rigidbody = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector2 movement = new Vector2(moveHorizontal, moveVertical);
rigidbody.AddForce(movement * speed);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{ rigidbody.velocity = (Vector2.up * jumpForce); }
}
}
Any help would be greatly appreciated Thank you.