- Home /
My character not rotate her aim while walking and jumping (I have video)
Hello I'm a just an Artist who wan to make a game by my self, so I learning c# I just follow some tutorial and try to mix their coding. I still a noob in coding. I want to make my character (Main body) can move and jump around while her arm(gun) can rotate and point at the player's mouse position. it's work fine when she stand still... But the problem is her arm(gun) not rotate when she walking or jumping.
I want her can enable to rotate and fire, whenever she jump or walk. please help me. thank you very much. Here is a video of the problem: 2 mb https://vimeo.com/user119508875/review/437895619/4e598ce9ce
Here is the code Player Movement using System.Collections; using System.Collections.Generic; using TMPro; using UnityEditor; using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] private LayerMask platformsLayerMask;
//private Player_Base playerBase;
public Rigidbody2D rigidbody2d;
private CapsuleCollider2D capsuleCollider2d;
//walk (var)
float moveInput;
public float moveSpeed = 17;
//Jump (Var)
public float jumpVelocity = 15f;
private float jumpTimecounter;
public float jumpTime = 0.25f;
private bool isJumping;
private void Awake()
{
//playerBase = gameObject.GetComponent<Player_Base>();
rigidbody2d = transform.GetComponent<Rigidbody2D>();
capsuleCollider2d = transform.GetComponent<CapsuleCollider2D>();
}
private void Update()
{
//Move Left-Right
moveInput = Input.GetAxis("Horizontal");
//Jump
if (IsGrounded() && Input.GetKeyDown(KeyCode.Space))
{
isJumping = true;
jumpTimecounter = jumpTime;
rigidbody2d.velocity = Vector2.up * jumpVelocity;
}
if (Input.GetKey(KeyCode.Space))//more jump
{
if (jumpTimecounter > 0 && isJumping == true)
{
rigidbody2d.velocity = Vector2.up * jumpVelocity;
jumpTimecounter -= Time.deltaTime;
}
else
{
isJumping = false;
}
}
if (Input.GetKeyUp(KeyCode.Space))
{
isJumping = false;
}
}
private void FixedUpdate()
{
//Debug.Log(rigidbody2d.velocity);
//walk
rigidbody2d.velocity = new Vector2(moveInput * moveSpeed, rigidbody2d.velocity.y);
}
//ground checker
private bool IsGrounded()
{
RaycastHit2D raycastHit2d = Physics2D.BoxCast(capsuleCollider2d.bounds.center, capsuleCollider2d.bounds.size, 0f, Vector2.down, 0.1f, platformsLayerMask);
//Debug.Log(raycastHit2d.collider);
return raycastHit2d.collider != null;
}
}
And here is the code for aiming her weapon
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAim : MonoBehaviour
{
//Aim
public Camera cam;
private Vector2 mousePos;
public Rigidbody2D rigidbody2d;
void Update()
{
HandAim();
Shooting();
}
private void HandAim() //Aim
{
mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
Vector2 lookDir = mousePos - rigidbody2d.position;
float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg;
rigidbody2d.rotation = angle;
}
private void Shooting() //Shoot
{
if (Input.GetMouseButtonDown(0)) //0 = left click
{
}
}
}
Well, @Hypnix did you find the mistake? I have a sme problem and i dont know how to correct it