[ad_1]
I’ve a GameObject referred to as Gun, and a GameObject referred to as Hand which is a baby of different GameObject named Player.
When I decide up the gun, it turns into a baby of the hand.
The drawback is, when the participant rotates (and so, Hand rotates too), the gun’s not rotating, it stays on the identical rotation.
Here’s the Gun script:
utilizing System.Collections.Generic;
utilizing UnityEngine;
public class LaserGun : MonoBehaviour
{
public GameObject Hand;
public Transform LaserSpawnPoint;
public Movement motion;
public Rigidbody2D rb2D;
public GameObject Laser;
public bool IsInHand;
public void Start()
{
Hand = GameObject.Find("Hand");
motion = GameObject.Find("Player").GetComponent<Movement>();
rb2D = this.GetComponent<Rigidbody2D>();
}
public void OnMouseDown()
{
this.remodel.mum or dad = GameObject.Find("Hand").remodel;
this.remodel.place = Hand.remodel.place;
GetComponent<BoxCollider2D>().isTrigger = true;
GetComponent<Rigidbody2D>().isKinematic = true;
IsInHand = true;
}
public void Update()
{
if (Input.GetKeyDown(KeyCode.E) && IsInHand == true)
{
Instantiate(Laser, LaserSpawnPoint.place, remodel.rotation);
}
if (Input.GetKeyDown(KeyCode.Q) && IsInHand == true)
{
this.remodel.mum or dad = null;
GetComponent<BoxCollider2D>().isTrigger = false;
GetComponent<Rigidbody2D>().isKinematic = false;
IsInHand = false;
}
}
}
And this is the Player’s motion script:
utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;
public class Movement : MonoBehaviour
{
public float velocity;
public float jumpForce;
public float transferInput;
public GameObject Hand;
public GameObject HeldObj;
non-public Rigidbody2D rb;
public bool facingRight = true;
non-public bool isGrounded;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;
void Start()
{
rb = GetComponent<Rigidbody2D>();
Hand.remodel.rotation = this.remodel.rotation;
}
void MountedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.place, checkRadius, whatIsGround);
transferInput = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(transferInput * velocity, rb.velocity.y);
if (facingRight == false && transferInput > 0)
{
Flip();
Hand.remodel.Rotate(new Vector3(0, -180, 0));
}
else if (facingRight == true && transferInput < 0)
{
Flip();
Hand.remodel.Rotate(new Vector3(0, 180, 0));
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true)
{
rb.velocity = Vector2.up * jumpForce;
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 Scaler = remodel.localScale;
Scaler.x *= -1;
remodel.localScale = Scaler;
}
}
Some information (may be helpful)
- The recreation is 2D
- The “Hand” object is an empty recreation object
- There is not any animator on participant and the gun objects
I attempted to add the picture of this however for some cause Stack doesn’t let me do that.
Any assist will likely be appreciated!
[ad_2]